Print the first 128 prime numbers without using any reserved words

13

1

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

Requirements:

  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

vsz

Posted 2012-05-12T22:25:45.947

Reputation: 7 963

1Unfortunately for me, Tcl has no reserved words. – Johannes Kuhn – 2013-11-10T23:04:16.230

Answers

14

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.

ugoren

Posted 2012-05-12T22:25:45.947

Reputation: 16 527

7

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)

boothby

Posted 2012-05-12T22:25:45.947

Reputation: 9 038

1print isn't reserved in Python3 :) you can use __import__, but that's gonna cost characters – gnibbler – 2012-07-12T01:22:03.213

6

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.

copy

Posted 2012-05-12T22:25:45.947

Reputation: 6 466

4

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.

Ilmari Karonen

Posted 2012-05-12T22:25:45.947

Reputation: 19 513

4

C, 87 chars

d;p(n){--d<2||n%d&&p(n);}r(n){p(d=n);d<2&&printf("%d\n",n);++n<720&&r(n);}main(){r(2);}

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

breadbox

Posted 2012-05-12T22:25:45.947

Reputation: 6 893

3

C, 134 chars

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

main(i){i<9?main(2953216):i>4097?--i&4094?i/4096%(i%4096)?main(i):
main((i/4096-1)*4097):printf("%d\n",i>>12,main((i/4096-1)*4097)):0;}

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

breadbox

Posted 2012-05-12T22:25:45.947

Reputation: 6 893

3

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.

Fold[#2Cases[#/#2,1|_Rational]&,#,#]&@Range[2,719]

Mr.Wizard

Posted 2012-05-12T22:25:45.947

Reputation: 2 481

2

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.

hammar

Posted 2012-05-12T22:25:45.947

Reputation: 4 011

1

K (ngn/k), 31 bytes

x,1_&&/720#'!'x:24\234925135391

Try it online!

scrawl

Posted 2012-05-12T22:25:45.947

Reputation: 1 079