From 88a6ed7ce577a55d77d92fe56a27d802a61a802c Mon Sep 17 00:00:00 2001 From: sarthakgupta1907-cell Date: Fri, 6 Nov 2026 10:35:03 +0530 Subject: [PATCH] feat: add prime factorization algorithm --- Maths/PrimeFactorization.js | 29 ++++++++++++++++++ Maths/test/PrimeFactorization.test.js | 43 +++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 Maths/PrimeFactorization.js create mode 100644 Maths/test/PrimeFactorization.test.js diff --git a/Maths/PrimeFactorization.js b/Maths/PrimeFactorization.js new file mode 100644 index 0000000000..bc1a16097e --- /dev/null +++ b/Maths/PrimeFactorization.js @@ -0,0 +1,29 @@ +/** + * @function primeFactorization + * @description Returns all prime factors of a given positive integer. + * Prime factorization is the process of determining which prime numbers + * multiply together to make the original number. + * @param {number} n - A positive integer greater than 1 + * @returns {number[]} - An array of prime factors in ascending order + * @see https://en.wikipedia.org/wiki/Integer_factorization + * @example primeFactorization(12) => [2, 2, 3] + * @example primeFactorization(28) => [2, 2, 7] + * @example primeFactorization(13) => [13] + */ +const primeFactorization = (n) => { + if (!Number.isInteger(n) || n < 2) { + throw new TypeError('Input must be an integer greater than or equal to 2') + } + const factors = [] + let num = n + for (let i = 2; i <= Math.sqrt(num); i++) { + while (num % i === 0) { + factors.push(i) + num = Math.floor(num / i) + } + } + if (num > 1) factors.push(num) + return factors +} + +export { primeFactorization } diff --git a/Maths/test/PrimeFactorization.test.js b/Maths/test/PrimeFactorization.test.js new file mode 100644 index 0000000000..35d0f9b124 --- /dev/null +++ b/Maths/test/PrimeFactorization.test.js @@ -0,0 +1,43 @@ +import { primeFactorization } from '../PrimeFactorization.js' + +describe('primeFactorization', () => { + test('primeFactorization(2) returns [2]', () => { + expect(primeFactorization(2)).toEqual([2]) + }) + + test('primeFactorization(12) returns [2, 2, 3]', () => { + expect(primeFactorization(12)).toEqual([2, 2, 3]) + }) + + test('primeFactorization(28) returns [2, 2, 7]', () => { + expect(primeFactorization(28)).toEqual([2, 2, 7]) + }) + + test('primeFactorization(13) returns [13]', () => { + expect(primeFactorization(13)).toEqual([13]) + }) + + test('primeFactorization(100) returns [2, 2, 5, 5]', () => { + expect(primeFactorization(100)).toEqual([2, 2, 5, 5]) + }) + + test('primeFactorization(360) returns [2, 2, 2, 3, 3, 5]', () => { + expect(primeFactorization(360)).toEqual([2, 2, 2, 3, 3, 5]) + }) + + test('primeFactorization of a prime returns that prime', () => { + expect(primeFactorization(97)).toEqual([97]) + }) + + test('throws TypeError for 1', () => { + expect(() => primeFactorization(1)).toThrow(TypeError) + }) + + test('throws TypeError for negative input', () => { + expect(() => primeFactorization(-12)).toThrow(TypeError) + }) + + test('throws TypeError for float input', () => { + expect(() => primeFactorization(12.5)).toThrow(TypeError) + }) +})