UNPKG

everyutil

Version:

A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.

24 lines (23 loc) 706 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.primeFactorization = void 0; /** * Returns the prime factorization of a number as an array of prime factors. * For example, primeFactorization(60) returns [2, 2, 3, 5]. * @author @dailker * @param {number} n - The number to factorize. * @returns {number[]} An array of prime factors. */ function primeFactorization(n) { const result = []; for (let i = 2; i <= Math.sqrt(n); i++) { while (n % i === 0) { result.push(i); n /= i; } } if (n > 1) result.push(n); return result; } exports.primeFactorization = primeFactorization;