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.Nwill be written using only the characters0123456789and 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
stdoutmust be preciselyHello world!followed by zero or more newlines. - If N is composite,
gccmust 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
gccnor 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!