1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | function pn(count){ prs=[2]; pns=[6]; nprs=0; for(i=2;nprs<=count;i++){ isPrime=true; for(j=0;j<prs.length;j++){ if(i%prs[j]==0){ isPrime=false; break; } } if(isPrime){ prs.push(i); nprs=(Math.pow(2,i-1)*(Math.pow(2,i)-1)) if(nprs<=count) //確保輸出的結果不會有任何大於 count 的數 pns.push(nprs); } } return pns; } t=pn(1000); //alert(t.toString()) |
Direct link: https://paste.plurk.com/show/1372410