The obvious solution would be to just print them as a string, but is it possible to write a shorter code?


  1. No input should be processed, and the output should be in the form of 2 3 5 7 11 13 ... etc.
  2. No reserved words in the language are used at all
  3. The language should at least allow structured programming, and have reserved words (otherwise point 2 would be moot).

first I had exclusively C/C++ in mind, but extended the question while still trying to prevent cheating

  • 1
    \$\begingroup\$ Unfortunately for me, Tcl has no reserved words. \$\endgroup\$ – Johannes Kuhn Nov 10 '13 at 23:04

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.


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.

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'.

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)
  • 1
    \$\begingroup\$ print isn't reserved in Python3 :) you can use __import__, but that's gonna cost characters \$\endgroup\$ – gnibbler Jul 12 '12 at 1:22

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.


C, 183 chars

#define q j*j<k?i%p[j++]||(i++,j=0):printf("%d\n",p[j=0,k++]=i++)
#define A q;q;q;q;q;q;q;q
#define B A;A;A;A;A;A;A;A
#define C B;B;B;B;B;B;B
main(){int i=2,j=0,k=0,p[999];C;C;C;C;C;}

Well, here's a quick first attempt. I believe this should satisfy the requirements. I'm using simple trial division to find the primes and an unrolled loop constructed using the preprocessor to iterate it until I've found enough of them. The number of repetitions has been tweaked so that exactly 128 primes are printed.


C, 87 chars


(I tried to write it in a more functional style, but my inability to use return kind of killed that plan.)


C, 134 chars

Here's an alternate solution that tries to avoid using words as much as possible, reserved or otherwise:


All it uses is printf and main with a single argument.


Mathematica 50 characters

I'm not sure how to interpret "reserved words" for Mathematica but I want to play so I'll take it to mean doing without built-in functions to generate primes or test for primality.


Haskell, 72 characters

main=putStrLn.unwords$take 128[show p|p<-[2..],all((>0).mod p)[2..p-1]]

Admittedly, avoiding keywords isn't too difficult in Haskell.


K (ngn/k), 31 bytes


Try it online!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.