Print the first 128 prime numbers without using any reserved words
C, 60 chars
The "no keywords" limitation doesn't matter here. I'm pretty sure that improving it, if possible, won't be done by adding keywords.
n=2;main(m){n<720&&main(m<2?printf("%d ",n),n:n%m?m-1:n++);}
Alternate version:
The output isn't as nice, but I like the printf
abuse.
n=2;main(m){n<720&&main(m<2?printf("%*d",n,n):n%m?m-1:n++);}
The trick in both solutions is to merge two loops (implemented by recursion) into one.
n
is the next potential prime, m
the next potential divisor.
In each recursive call, we either increment n
(while setting m
to its previous value) or decrement m
.
Python, 108 chars
Python was not made for this challenge. Wanna print
? That's reserved. Well, how about we use stdout
? Well, that's gonna cost an import
... you guessed it, reserved. Well... I'm on unix, so I can open up the file descriptor 1, which happens to be stdout. Hack!
Man, and iteration? Nothing but eval
. No loops, of course, but we can't even define a function with def
or lambda
. And to add insult to injury, we can't even use list comprehension! I always look for an excuse to use things like map(p.__mod__,...)
in code golf... comprehension is always better. Until now, that is.
p=1
eval(compile("p+=1;open('/dev/fd/1','w').write('%s '%p*all(map(p.__mod__,range(2,p))));"*720,'','exec'))
Now, you might complain that exec
is a keyword, even though I didn't use the keyword (I didn't even eval
an exec
). Well, here's a 117-character solution which doesn't use 'exec'
.
p=2
s="eval('('+s*(p<720)+')',open('/dev/fd/1','w').write('%s '%p*all(map(p.__mod__,range(2,p)))),{'p':p+1})";eval(s)
JavaScript (80 chars)
eval((s=Array(719)).join("s[j=++n]?j:"+s.join("s[j+=n]=")+"r+=n+' ';"),r="",n=1)
Run in console of your webbrowser.
Used a prime sieve, which turned out to be very condensed.