Skip to content

Instantly share code, notes, and snippets.

@neizod
Forked from 140bytes/LICENSE.txt
Created October 5, 2011 15:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neizod/1264785 to your computer and use it in GitHub Desktop.
Save neizod/1264785 to your computer and use it in GitHub Desktop.
Factorization

Factorization

117b. Find the prime factorization of number.

How to Use

Invoke function using 1 argument.

1st arg (number) ... The number to factorized, can accept any integer (positive, negative, zero).
function(
n, // input number.
// placeholder.
i, // latest divisor.
r // array of result.
){
r=[]; // create an empty array.
if(n<0){ // check if n is negative number.
n=-n; // if n is negative, set it to positive.
r[0]=-1} // and set its 1st factor as -1.
for(i=2; // set divisor to 2.
i<=n;i++) // loop through all i <= n.
if(n%i==0){ // check if i is a divisor of n?
r.push(i); // if i is a divisor, save it to r.
n/=i--} // set new n and reset i for loop back.
r[0]=n? // on exit loop (only n==0|n==1 to exit loop), check if n==0?
r[0]? // if n!=0, check if some factor does exist?
r[0]: // if there already some factor, do nothing.
1: // if there are no any factor, set it to 1 (and only).
0; // if n==0, set the factor to 0 (and only).
return r // return the result.
}
function(n,i,r){r=[];if(n<0){n=-n;r[0]=-1}for(i=2;i<=n;i++)if(n%i==0){r.push(i);n/=i--}r[0]=n?r[0]?r[0]:1:0;return r}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Nattawut Phetmak <http://about.me/neizod>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "factorization",
"description": "factorization the integer.",
"keywords": [
"factorization",
"math",
"number",
]
}
<!DOCTYPE html>
<title>Factorization</title>
<div>Expected value: <b>2,2,2,2,2,3,3,3,,-1,2,3,7,,0</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var factor = function(n,i,r){r=[];if(n<0){n=-n;r[0]=-1}for(i=2;i<=n;i++)if(n%i==0){r.push(i);n/=i--}r[0]=n?r[0]?r[0]:1:0;return r}
document.getElementById( "ret" ).innerHTML = [factor(864),,
factor(-42),,
factor(0)];
</script>
@123jimin
Copy link

104b!

function(n,i,r){for(r=n<0?(n=-n,[-1]):[],i=2;i<=n;i++)n%i||(r.push(i),n/=i--);r[0]=n*(r[0]||1);return r}

@Zugr
Copy link

Zugr commented Jul 15, 2014

Sieve of Eratosthenes with linear time work (115bytes)

var r=[2];function factor(N){for(var i=0,x=2;x*x<=N&&N%x;x=r[i])if(!r[++i])while(factor(r[i]=++x));return N%x?0:x}
var r=[2]; // Sieve of Eratosthenes 
function factor(N)
{
  for(var i=0,x=2;x*x<=N&&N%x;x=r[i]) // Loop numbers of sieve
    if(!r[++i]) // if sieve finished
      while(factor(r[i]=++x)); // fill sieve new values
  return N%x?0:x // return divider or zero
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment