3
The challenge:
Given an integer N, 1 < N < 2³¹, write code that uses the C preprocessor
to determine whether N is prime, and produces a "Hello world" program if N is prime,
or an error otherwise. Make the code as short as possible.
It's not very hard, so don't worry that there are so many rules; most
of them are boilerplate.
The following sample code (in example.c
) works for N < 100:
#include <stdio.h>
int main() {
#if (N%2!=0 && N%3!=0 && N%5!=0 && N%7!=0) || (2==N || 3==N || 5==N || 7==N)
printf("Hello world!");
#else
Walkmen;
#endif
}
23 is prime but 99 is not, so:
c:\golf\preprime>gcc -std=c99 -DN=23 example.c
c:\golf\preprime>a
Hello world!
c:\golf\preprime>gcc -std=c99 -DN=99 example.c
example.c: In function 'main':
example.c:6:1: error: 'Walkmen' undeclared (first use in this function)
Walkmen;
^
example.c:6:1: note: each undeclared identifier is reported only once for each function it appears in
c:\golf\preprime>
Rules
- 1 < N < 2147483648
- N will be defined in a macro named
N
.N
will be written using only the characters0123456789
and will not begin with0
. - The code will be compiled with the command
gcc -std=c99 -DN=<number> <filename>.c [-o <outfile>]
- If N is prime, a valid C99 program must be successfully compiled. When executed, the program's output to
stdout
must be preciselyHello world!
followed by zero or more newlines. - If N is composite,
gcc
must emit a compiler error. A linker error is not acceptable. - You may not utilize any GCC extensions.
- You may not assume anything about the name of any file.
- Note: It is not allowed to use implicit types or implicit function declarations.
- Neither
gcc
nor any "Hello world" program may take more than 20 seconds to execute on a typical computer. - Shortest code length in UTF-8 bytes wins.
For reference, my ungolfed implementation is 571 bytes. Happy golfing!
Nice. It's not required to write "Walkmen" :) – feersum – 2014-10-03T23:40:45.463