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

added a commented version
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (ES6), 71 bytes

f=p=>p>4?f(p-1)+[[,p+' '][(P=x=>p%--x?P(x):x)(p)*P(p-=~p)*P(p>>=2)]]:''

Try it online!

Commented

f = p => // recursive function taking a prime candidate p
p > 4 ? // if p is greater than 4:
 f(p - 1) + // do a recursive call with p - 1
 [[, p + ' '][ // append p followed by a space iff the following
 // result is 1:
 ( P = x => // helper recursive function taking x
 p % --x ? // decrement x; if x is not a divisor of p:
 P(x) // do recursive calls until it is
 : // else:
 x // return x (smallest proper divisor of p)
 )(p) * // first call with p
 P(p -= ~p) * // second call with 2p + 1
 P(p >>= 2) // third call with floor((2p + 1) / 4) which is
 // (p - 1) / 2 if p is odd
 // the product is 1 iff all calls return 1,
 ]] // i.e. all tested numbers are prime
: // else:
 '' // stop

JavaScript (ES6), 71 bytes

f=p=>p>4?f(p-1)+[[,p+' '][(P=x=>p%--x?P(x):x)(p)*P(p-=~p)*P(p>>=2)]]:''

Try it online!

JavaScript (ES6), 71 bytes

f=p=>p>4?f(p-1)+[[,p+' '][(P=x=>p%--x?P(x):x)(p)*P(p-=~p)*P(p>>=2)]]:''

Try it online!

Commented

f = p => // recursive function taking a prime candidate p
p > 4 ? // if p is greater than 4:
 f(p - 1) + // do a recursive call with p - 1
 [[, p + ' '][ // append p followed by a space iff the following
 // result is 1:
 ( P = x => // helper recursive function taking x
 p % --x ? // decrement x; if x is not a divisor of p:
 P(x) // do recursive calls until it is
 : // else:
 x // return x (smallest proper divisor of p)
 )(p) * // first call with p
 P(p -= ~p) * // second call with 2p + 1
 P(p >>= 2) // third call with floor((2p + 1) / 4) which is
 // (p - 1) / 2 if p is odd
 // the product is 1 iff all calls return 1,
 ]] // i.e. all tested numbers are prime
: // else:
 '' // stop
saved 3 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (ES6), 7471 bytes

f=p=>p>4?f(p-1)+(+[[,p+' '][(P=x=>p%--x?P(x):x<2x)(q=pp)&P*P(p-=~p)&P*P(p>>=2)?q+' ':'')]]:''

Try it online! Try it online!

JavaScript (ES6), 74 bytes

f=p=>p>4?f(p-1)+((P=x=>p%--x?P(x):x<2)(q=p)&P(p-=~p)&P(p>>=2)?q+' ':''):''

Try it online!

JavaScript (ES6), 71 bytes

f=p=>p>4?f(p-1)+[[,p+' '][(P=x=>p%--x?P(x):x)(p)*P(p-=~p)*P(p>>=2)]]:''

Try it online!

saved 4 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (ES6), 7874 bytes

f=p=>p>4?f(P=n=>n%p-1)+((P=x=>p%--x?P(nx):x<2)(x=pq=p)&P(x=pp-~p=~p)&P(x=--p/2p>>=2)?[...f(p),p+1]q+' ':f(p''):[]''

Try it online! Try it online!

JavaScript (ES6), 78 bytes

f=p=>p>4?(P=n=>n%--x?P(n):x<2)(x=p)&P(x=p-~p)&P(x=--p/2)?[...f(p),p+1]:f(p):[]

Try it online!

JavaScript (ES6), 74 bytes

f=p=>p>4?f(p-1)+((P=x=>p%--x?P(x):x<2)(q=p)&P(p-=~p)&P(p>>=2)?q+' ':''):''

Try it online!

Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading

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