Skip to content

Instantly share code, notes, and snippets.

@wintercn
Created October 20, 2012 15:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wintercn/3923697 to your computer and use it in GitHub Desktop.
Save wintercn/3923697 to your computer and use it in GitHub Desktop.
计算“某个范围内能被最多的数整除的数”
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"));
}
@myst729
Copy link

myst729 commented Oct 21, 2012

看不懂啊 winter 老师

@myst729
Copy link

myst729 commented Oct 21, 2012

用质数序列遍历的组合出不大于给定 n 的所有整数?我理解得对吗 winter 老师……

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