R

E

A

C

T

O

xamples 

epeat

ode

pproach

ptimize

est

Prime Factorization

Problem

Any positive integer greater than one can be factored into a product of prime numbers.

Example: 1200 = 2^4 * 3 * 5^2

Write a function that takes an integer n as an argument and returns the prime factorization. The result is an object with each prime factor as a key and its exponent as the value.

Example

   primeFactorization(2) = {'2': 1}

   primeFactorization(20) = {'2': 2, '5': 1}

   primeFactorization(1200) = {'2': 4, '3': 1, '5': 2}

Solution 

                var primeFactorization = function(n) {
                    var result = {};
                    for (var i=2; i <= n; i++){
                	while (n%i === 0){
                	    result[i] = ++result[i] || 1;
                	    n /= i;
                	}
                    }
                    return result;
               }

The End

Prime Factorization

By Ivan Loughman-Pawelko

Prime Factorization

Find the prime factorization of a number

  • 1,288