Possibly quit your job with a polyglot
Python 2 / Python 3 - 128
n=int(input())
s="Isf tthhies ,b oIs sq ufiitn.d"
print(["not "*any([n%a<1for a in range(2,n)])+"prime",s[::2]+s[1::2]][1/2>0])
Identifies prime numbers with Python 2, get's you into trouble with Python 3.
Bonus
__help__ = """In the forloop I'm setting the values i to n.
To be a "notPrimes", it's n % i == 0 if: 1 < i, i < n. (We
do tests i<1/2 i==1, too). Then, prints resulting, prime text.
In case i==n: quit. Just if it aborts: try."""
# read integer from command line
n=int(input())
try:
# primes = True
notPrimes = False
# try each i to n
for i in range(n):
# ignore 0 or 1
if i < 1 / 2 or i == 1:
continue
# test divisibility
if n % i == 0:
notPrimes = True
# print result
if notPrimes:
print("not prime")
else:
print("prime")
except:
# if program aborts: print help and error code
print(__help__ [::7])
Try it with Python 2 or Python 3! (In contrast to the golf version above the roles changed: Python 3 is the prime number identifier. Python 2 contains the Easter egg.)
Please, excuse my bad English in the help text! ;)
And I do use the word "quit". But somehow I need to describe when my program terminates. ;)
Bonus submission (C / C++11)
Primality testing using the usual naive method is so mainstream. That's why I have invented a brand-new randomized naive method! This test is as follows:
- Choose any integer d at random. It must not be smaller than 2 and larger than a bit more than
sqrt(n)
. - If d is a divisor of n, output
not prime
. - If we made this test
20sqrt(n)
times, outputprime
, else repeat.
If the number is composite, there is only very little probability (about 10-9) that it doesn't work. Of course, I don't believe the C/C++ pseudorandom number generator is powerful enough. That's why I use my own 256-bit LFSR generator!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* A 256-bit linear feedback shift register generating pseudorandom
* numbers (its period is 2^256 - 1).
*/
struct LFSRGenerator {
unsigned seed[8];
};
void lfsr_init_generator(struct LFSRGenerator *gen){
gen->seed[0] = 0xE840CC92; gen->seed[1] = 0xC440CAD0;
gen->seed[2] = 0x40E6E6DE; gen->seed[3] = 0xC8DCD2CC;
gen->seed[4] = 0xD0E840E6; gen->seed[5] = 0x4058E6D2;
gen->seed[6] = 0xEAE24092; gen->seed[7] = 0x145CE8D2;
}
void lfsr_proceed(struct LFSRGenerator *gen){
// LFSR taps are x^256, x^254, x^251 and x^246
unsigned new_bit =
((gen->seed[7]>>0)^(gen->seed[7]>>2)^
(gen->seed[7]>>5)^(gen->seed[7]>>10)) & 1;
// shift seed right
gen->seed[7] >>= 1;
int cell;
for(cell = 6; cell >= 0; cell--){
gen->seed[cell+1] |= ((gen->seed[cell]&1)<<31);
gen->seed[cell] >>= 1;
}
gen->seed[0] |= (new_bit<<31); // put new bit
}
void lfsr_error(struct LFSRGenerator *gen){
fprintf(stderr, "Error! Developer info:\n");
int cell;
for(cell = 0; cell < 8; cell++){
unsigned val = gen->seed[cell];
putc((char)(val&0xFF), stderr); val >>= 8;
putc((char)(val&0xFF), stderr); val >>= 8;
putc((char)(val&0xFF), stderr); val >>= 8;
putc((char)(val&0xFF), stderr);
}
putc('\n', stderr);
exit(1);
}
int lfsr_get_num(struct LFSRGenerator *gen, int min_val, int max_val){
lfsr_proceed(gen);
int mod_num = max_val-min_val+1; // = number of possible results
if(mod_num <= 0)
lfsr_error(gen);
// take 6 first cells and compute them mod 'modNum'
unsigned long long result = 0;
int cell;
for(cell = 5; cell >= 0; cell--){
result = ((result << 32) | gen->seed[cell]) % mod_num;
}
return (int)result + min_val;
}
/**********************************************************************/
void end_not_prime(){
printf("not prime\n");
exit(0);
}
void end_prime(){
printf("prime\n");
exit(0);
}
int main(){
int number;
struct LFSRGenerator gen;
lfsr_init_generator(&gen);
printf("Provide a number to check its primality: ");
scanf("%d", &number);
if(number <= 1){
end_not_prime();
}
if(number == 2){
end_prime();
}
// just to make sure:
// * make 20*sqrt(n) tests
// * generate random divisors from 2 to 111111/100000 * sqrt(n)
// (in case max range doesn't include sqrt(n)
auto num_checks = (int)floor(sqrt(number)*20);
auto max_range = sqrt(number);
max_range /= 100000;
max_range *= 111111;
max_range = floor(max_range+0.5);
while(num_checks--){
int rnd_div = lfsr_get_num(&gen, 2, max_range);
if(number % rnd_div == 0){
end_not_prime();
}
}
end_prime();
}
C++11 works correctly. However, C compiler seems to be outputting a faulty program for n > 2...
Note: remember that C needs -lm
option (link math library) to compile successfully.
Look at
max_range
variable. C++11 keywordauto
resolves to a "matching type" - in this casedouble
. However, in C it's defined as a variable modifier (asstatic
is) - it doesn't define the type. Thusmax_range
type is a default C type, that isint
. When we "try" to multiply this variable by 1.11111, in C it gets "unintenionally" zeroed during division by 100000. We get an incorrect interval of random numbers to be generated and LFSR after proceeding its internal state generates an error, outputting the binary dump of the seed. That's "accidentally" The message If the boss finds this, I quit.\n
If you find the following faulty output:
Error! Developer info:
If the boss finds this, I quit.
incorrect, just remove the appropriate fprintf
line.
Mathematica/Brainfuck, 260
If[PrimeQ[Input[]],"prime","not prime"](*++++++++++[>+++>++++>+++++++>++++++++++>+++++++++++<<<<<-]>>>+++.>++.<<<++.>>>>++++++.<++.---.<<<.>>>---.>-----.++++..<<<<.>>>++++.+++.>-----.<-----.>+++++.<<<<.>>>>+.<++++.+.>-.<<<++++.<.>>.<<.>>>>--.++++.<.>-.<<<++.*)