Sinisterly
[Golfing] First 20 Mersenne primes - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: Coding (https://sinister.li/Forum-Coding--71)
+--- Thread: [Golfing] First 20 Mersenne primes (/Thread-Golfing-First-20-Mersenne-primes)



[Golfing] First 20 Mersenne primes - Inori - 02-10-2017

I haven't written a code golf challenge forever, and since the obfuscation thread seems to be popular, I thought I might keep that theme going. If you're unfamiliar with code golfing, it's a competition for the shortest possible version of a program.

For this challenge, write a program to calculate the first 20 Mersenne prime numbers. Since M20 has a whopping 1332 digits, print the exponential equation (2^p-1) instead of its value.

I'm working on a solution in Ruby, will post when it's finished.

Edit: I'll give anyone who can make it fit in a signature (<=242 characters with code tags) 20 NSP.


RE: [Golfing] First 20 Mersenne primes - Inori - 02-10-2017

Ruby, 89 bytes. It's really inefficient, but it works perfectly. I'm gonna try and do more with it after work.

Code:
->f,n=1{puts"2^#{f-=1;n}-1"if->s{(0..n+=1).find{3>2|s=(s*s-2)%(2**n-1)}}[4]while f>0}[20]



RE: [Golfing] First 20 Mersenne primes - Mr.Kurd - 02-10-2017

Didn't understand yet? What is this?!


RE: [Golfing] First 20 Mersenne primes - Inori - 02-10-2017

(02-10-2017, 07:13 PM)Mr.Kurd Wrote: Didn't understand yet? What is this?!

The task is to calculate the first 20 Mersenne primes (see link in OP) in as few characters as possible. Essentially, write a program to do it normally, then refactor it to the point where it can't get any smaller.


RE: [Golfing] First 20 Mersenne primes - omega12 - 02-11-2017

67 Bytes Mathematica
Always choose the best tool for the job
Code:
For[i=0,i<4424,i++,If[PrimeQ[2^i-1],Print[StringForm["2^``-1",i]]]]



RE: [Golfing] First 20 Mersenne primes - Inori - 02-11-2017

(02-11-2017, 01:38 AM)omega12 Wrote: 67 Bytes Mathematica
Always choose the best tool for the job
Code:
For[i=0,i<4424,i++,If[PrimeQ[2^i-1],Print[StringForm["2^``-1",i]]]]

If plugging in 4424 as a constant is legit, I can get mine down to 77. Would be better to treat it as unknown, imo.
Code:
4244.times{|n|puts"2^#{n}-1"if->s{(0..n+=1).find{3>2|s=(s*s-2)%(2**n-1)}}[4]}



RE: [Golfing] First 20 Mersenne primes - omega12 - 02-11-2017

Code:
Do[Print[StringForm["2^``-1",MersennePrimeExponent[n]]],{n,20}]
63 bytes
Why do function names need to be so long?


RE: [Golfing] First 20 Mersenne primes - Inori - 02-11-2017

(02-11-2017, 05:28 AM)omega12 Wrote:
Code:
Do[Print[StringForm["2^``-1",MersennePrimeExponent[n]]],{n,20}]
63 bytes
Why do function names need to be so long?

For real though, I implemented a function for it in about as many characters. Still better than Java's namespacing addiction.