Created
October 20, 2012 15:53
-
-
Save wintercn/3923697 to your computer and use it in GitHub Desktop.
计算“某个范围内能被最多的数整除的数”
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
var primes = [2,3,5,7,11,13,17]; | |
function find(n){ | |
var table = new Array(n+1); | |
table[1] = [0,0,0,0,0,0,0]; | |
for(var i = 1;i<n;i++) { | |
if(!table[i]) { | |
table[i] = [0,0,0,0,0,0,0]; | |
continue; | |
} | |
for(var j = 0;j<primes.length;j++) { | |
if(i*primes[j]>n) continue; | |
table[i*primes[j]] = table[i].slice(); | |
table[i*primes[j]][j] ++; | |
} | |
} | |
function cal(x) { | |
var r = 1; | |
for(var j = 0;j<x.length;j++) { | |
if(x[j]>0) r*=x[j]+1; | |
} | |
return r; | |
} | |
table.sort(function(a,b){ | |
return cal(b)-cal(a); | |
}); | |
table = table.map(function(x){ | |
var r = 1; | |
for(var j = 0;j<x.length;j++) { | |
if(x[j]>0) r*= Math.pow(primes[j],x[j]); | |
} | |
return r; | |
}) | |
console.log(table.join("\n")); | |
} |
用质数序列遍历的组合出不大于给定 n 的所有整数?我理解得对吗 winter 老师……
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
看不懂啊 winter 老师