Created
September 17, 2012 13:34
-
-
Save viig99/3737301 to your computer and use it in GitHub Desktop.
gremlin_2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function FIB(n) { | |
phi = (1 + Math.sqrt(5))/2 | |
eta = (1-phi) | |
return (Math.pow(phi,n) - Math.pow(eta,n)) / Math.sqrt(5) | |
} | |
var j = 0 | |
for (var i = 5;i < 500;i++) { | |
var num = FIB(i) | |
if (num >= 227000 && isPrime(Math.round(num))) { | |
j = Math.round(num); | |
break; | |
} | |
} | |
function isPrime(n) { | |
if (isNaN(n) || !isFinite(n) || n%1 || n<2) return false; | |
if (n==leastFactor(n)) return true; | |
return false; | |
} | |
x = primeFactorList(j+1).reduce(function(a,b) { | |
return a+b | |
},0); | |
console.log(x); | |
// leastFactor(n) | |
// returns the smallest prime that divides n | |
// NaN if n is NaN or Infinity | |
// 0 if n=0 | |
// 1 if n=1, n=-1, or n is not an integer | |
function leastFactor(n){ | |
if (isNaN(n) || !isFinite(n)) return NaN; | |
if (n==0) return 0; | |
if (n%1 || n*n<2) return 1; | |
if (n%2==0) return 2; | |
if (n%3==0) return 3; | |
if (n%5==0) return 5; | |
var m = Math.sqrt(n); | |
for (var i=7;i<=m;i+=30) { | |
if (n%i==0) return i; | |
if (n%(i+4)==0) return i+4; | |
if (n%(i+6)==0) return i+6; | |
if (n%(i+10)==0) return i+10; | |
if (n%(i+12)==0) return i+12; | |
if (n%(i+16)==0) return i+16; | |
if (n%(i+22)==0) return i+22; | |
if (n%(i+24)==0) return i+24; | |
} | |
return n; | |
} | |
function primeFactorList(n) { | |
if (n < 1) | |
throw "Argument error"; | |
var result = []; | |
while (n != 1) { | |
var factor = leastFactor(n); | |
result.push(factor); | |
n /= factor; | |
} | |
return result; | |
} | |
/* | |
* Returns the prime factorization as a list of factor-power pairs, from the given factor list. The given list must be in ascending order. | |
* Examples: | |
* toFactorPowerList([2, 2, 2]) = [[2, 3]] | |
* toFactorPowerList([3, 5]) = [[3, 1], [5, 1]] | |
*/ | |
function toFactorPowerList(factors) { | |
var result = []; | |
var factor = factors[0]; | |
var count = 1; | |
for (var i = 1; i < factors.length; i++) { | |
if (factors[i] == factor) { | |
count++; | |
} else { | |
result.push([factor, count]); | |
factor = factors[i]; | |
count = 1; | |
} | |
} | |
result.push([factor, count]); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment