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 Revisions

2 of 2
Commonmark migration

PHP, 53 bytes

for($i=$n=$argv[1];--$i&&$n%$i;);echo!($i-1|$n+1&$n);

takes command line argument; prints 1 for Mersenne prime, empty string else. Run with -r.

breakdown

for($i=$n=$argv[1];--$i&&$n%$i;); // loop $i down from $n-1 until $i divides $n
 // If $n is prime, loop ends with $i=1. ($n=1 -> $i=0)
echo!($i-1|$n+1&$n); // If $i!=1, $n is not prime. If ($n+1&$n)>0, $n is not Mersenne.
 // If either $i-1 or $n+1&$n is truthy, the negation will be false.
Titus
  • 14.9k
  • 1
  • 25
  • 41

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