Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Commonmark migration
Source Link

#Javascript, (削除) 306 307 (削除ここまで) 282B

Javascript, (削除) 306 307 (削除ここまで) 282B

function y(r){for(n=r-1,k=1;n;k++)if(p=[],e=[],c=0,P=s=1,!((x=k)%2|1==x)){while(x>1){for(f=x,j=2;j<=Math.sqrt(f);j++)if(f%j==0){f=j;break}f!=p[c-1]?(p.push(f),e.push(2),c++):e[c-1]++,x/=f}for(i=0;c>i;i++){if(p[i]>P+1){s=0;break}P*=(Math.pow(p[i],e[i])-1)/(p[i]-1)}s&&n--}return k-1}

250k in approx. 6s on my laptop.

Commented un-golfed code: http://jsfiddle.net/82xb9/3/ now with better sigma-testing and a better if condition (thank you comments)

Pre-edit versions: http://jsfiddle.net/82xb9/ http://jsfiddle.net/82xb9/1/

#Javascript, (削除) 306 307 (削除ここまで) 282B

function y(r){for(n=r-1,k=1;n;k++)if(p=[],e=[],c=0,P=s=1,!((x=k)%2|1==x)){while(x>1){for(f=x,j=2;j<=Math.sqrt(f);j++)if(f%j==0){f=j;break}f!=p[c-1]?(p.push(f),e.push(2),c++):e[c-1]++,x/=f}for(i=0;c>i;i++){if(p[i]>P+1){s=0;break}P*=(Math.pow(p[i],e[i])-1)/(p[i]-1)}s&&n--}return k-1}

250k in approx. 6s on my laptop.

Commented un-golfed code: http://jsfiddle.net/82xb9/3/ now with better sigma-testing and a better if condition (thank you comments)

Pre-edit versions: http://jsfiddle.net/82xb9/ http://jsfiddle.net/82xb9/1/

Javascript, (削除) 306 307 (削除ここまで) 282B

function y(r){for(n=r-1,k=1;n;k++)if(p=[],e=[],c=0,P=s=1,!((x=k)%2|1==x)){while(x>1){for(f=x,j=2;j<=Math.sqrt(f);j++)if(f%j==0){f=j;break}f!=p[c-1]?(p.push(f),e.push(2),c++):e[c-1]++,x/=f}for(i=0;c>i;i++){if(p[i]>P+1){s=0;break}P*=(Math.pow(p[i],e[i])-1)/(p[i]-1)}s&&n--}return k-1}

250k in approx. 6s on my laptop.

Commented un-golfed code: http://jsfiddle.net/82xb9/3/ now with better sigma-testing and a better if condition (thank you comments)

Pre-edit versions: http://jsfiddle.net/82xb9/ http://jsfiddle.net/82xb9/1/

deleted 2 characters in body
Source Link
alyx-brett
  • 1.6k
  • 8
  • 14

#Javascript, (削除) 306 307 (削除ここまで) 307B282B

function y(Nr){for(n=Nn=r-1,k=1;n;k++){p=[];e=[];c=0;s=1;ifif(p=[],e=[],c=0,P=s=1,!((x=k)%2|x==1%2|1==x)continue;while){while(x>1){f=x;forfor(f=x,j=2;j<=Math.sqrt(f);j++)if(f%j==0){f=j;break}if(f!=p[c-1]){?(p.push(f);e,e.push(2);c++}else ,c++):e[c-1]++;x1]++,x/=f}for(i=1;i<c;i++i=0;c>i;i++){j=i;P=1;whileif(j--p[i]>P+1){s=0;break}P*=(Math.pow(p[j]p[i],e[j]e[i])-1)/(p[j]p[i]-1);if(p[i]>P+1)s=0}if(s)ns&&n--}return k-1;1}

250k in approx. 6s on my laptop.

Commented un-golfed code: http://jsfiddle.net/82xb9/1 ,http://jsfiddle.net/82xb9/3/ now with more enclosing functions, less anonymous functions, and more forbetter sigma-loop cunning.testing and a better if condition (thank you comments)

Pre-edit versionversions: http://jsfiddle.net/82xb9/ http://jsfiddle.net/82xb9/1/

#Javascript, (削除) 306 (削除ここまで) 307B

function y(N){for(n=N-1,k=1;n;k++){p=[];e=[];c=0;s=1;if((x=k)%2|x==1)continue;while(x>1){f=x;for(j=2;j<=Math.sqrt(f);j++)if(f%j==0){f=j;break}if(f!=p[c-1]){p.push(f);e.push(2);c++}else e[c-1]++;x/=f}for(i=1;i<c;i++){j=i;P=1;while(j--)P*=(Math.pow(p[j],e[j])-1)/(p[j]-1);if(p[i]>P+1)s=0}if(s)n--}return k-1;}

Commented un-golfed code: http://jsfiddle.net/82xb9/1 , now with more enclosing functions, less anonymous functions, and more for-loop cunning.

Pre-edit version: http://jsfiddle.net/82xb9/

#Javascript, (削除) 306 307 (削除ここまで) 282B

function y(r){for(n=r-1,k=1;n;k++)if(p=[],e=[],c=0,P=s=1,!((x=k)%2|1==x)){while(x>1){for(f=x,j=2;j<=Math.sqrt(f);j++)if(f%j==0){f=j;break}f!=p[c-1]?(p.push(f),e.push(2),c++):e[c-1]++,x/=f}for(i=0;c>i;i++){if(p[i]>P+1){s=0;break}P*=(Math.pow(p[i],e[i])-1)/(p[i]-1)}s&&n--}return k-1}

250k in approx. 6s on my laptop.

Commented un-golfed code: http://jsfiddle.net/82xb9/3/ now with better sigma-testing and a better if condition (thank you comments)

Pre-edit versions: http://jsfiddle.net/82xb9/ http://jsfiddle.net/82xb9/1/

deleted 88 characters in body
Source Link
alyx-brett
  • 1.6k
  • 8
  • 14

#Javascript, 306B(削除) 306 (削除ここまで) 307B

N=1000;
k=1;N--;whilefunction y(N)if((function{for(xn=N-1,k=1;n;k++){p=[];e=[];c=0;ifp=[];e=[];c=0;s=1;if(x%2|x==1(x=k)return;while%2|x==1)continue;while(x>1){f=x;for(j=2;j<Mathj=2;j<=Math.sqrt(f);j++)if(f%j==0){f=j;break;f=j;break}if(!k|ff!=p[c-1]){p.push(f);e.push(2);c++}else e[c-1]++;x/=f}for(i=1;i<c;i++){j=i;P=1;while(j--)P*=(Math.pow(p[j],e[j])-1)/(p[j]-1);if(p[i]>=P+2p[i]>P+1)return;}return 1s=0})if(k++)s)N-n-;k-}return k-;1;}

The Nth Practical Number is stored asCommented un-golfed code: khttp://jsfiddle.net/82xb9/1 , now with more enclosing functions, less anonymous functions, and more for-loop cunning.

I'm not counting the first line; commented unPre-golfed codeedit version: http://jsfiddle.net/82xb9/

I'm really curious to see if anyone can make this JS shorter apart from by removing the Math.sqrt which makes it go over the time limit...

#Javascript, 306B

N=1000;
k=1;N--;while(N)if((function(x){p=[];e=[];c=0;if(x%2|x==1)return;while(x>1){f=x;for(j=2;j<Math.sqrt(f);j++)if(f%j==0){f=j;break;}if(!k|f!=p[c-1]){p.push(f);e.push(2);c++}else e[c-1]++;x/=f}for(i=1;i<c;i++){j=i;P=1;while(j--)P*=(Math.pow(p[j],e[j])-1)/(p[j]-1);if(p[i]>=P+2)return;}return 1})(k++))N--;k--;

The Nth Practical Number is stored as k.

I'm not counting the first line; commented un-golfed code: http://jsfiddle.net/82xb9/

I'm really curious to see if anyone can make this JS shorter apart from by removing the Math.sqrt which makes it go over the time limit...

#Javascript, (削除) 306 (削除ここまで) 307B

function y(N){for(n=N-1,k=1;n;k++){p=[];e=[];c=0;s=1;if((x=k)%2|x==1)continue;while(x>1){f=x;for(j=2;j<=Math.sqrt(f);j++)if(f%j==0){f=j;break}if(f!=p[c-1]){p.push(f);e.push(2);c++}else e[c-1]++;x/=f}for(i=1;i<c;i++){j=i;P=1;while(j--)P*=(Math.pow(p[j],e[j])-1)/(p[j]-1);if(p[i]>P+1)s=0}if(s)n--}return k-1;}

Commented un-golfed code: http://jsfiddle.net/82xb9/1 , now with more enclosing functions, less anonymous functions, and more for-loop cunning.

Pre-edit version: http://jsfiddle.net/82xb9/

Source Link
alyx-brett
  • 1.6k
  • 8
  • 14
Loading

AltStyle によって変換されたページ (->オリジナル) /