7
2
Background Story
Bob was handed the following assignment for his programming class. Please note: this is not the actual challenge.
Homework assignment 11: Twin primes
Input: an integer
n ≥ 8
, taken from STDIN.Output: a message to STDOUT, depending on
n
:
- If
n
is not a prime number, outputNot prime.
- If
n
is a prime number, but neither ofn - 2
andn + 2
is, outputIsolated prime.
- If
n
andn + 2
are both prime numbers, outputLower twin prime.
- If
n
andn - 2
are both prime numbers, outputUpper twin prime.
Bob tried hard to solve the assignment, but his program kept crashing, and in the end, he gave up and submitted it in hope of partial credit. Nevertheless, Bob's program has the curious property that even though it never does what it's supposed to, the input is still classified correctly depending on the program's behavior: non-primes result in no output, isolated primes in a crash, lower twin primes in an infinite loop with no output, and upper twin primes in an infinite loop that keeps printing the correct message.
The Task
Your task is to replicate the behavior or Bob's solution. In other words, write a program that reads a single integer n ≥ 8
from STDIN or closest equivalent, and does the following:
- If
n
is not a prime number, output nothing and exit gracefully. - If
n
is a prime number, but none ofn - 2
orn + 2
is, produce a runtime error of some kind. Stack overflows count as runtime errors. - If
n
andn + 2
are both prime numbers, output nothing, but keep running until the program is killed manually (or until you run out of memory). - If
n
andn - 2
are both prime numbers, repeatedly outputUpper twin prime.
, with or without a trailing newline, to STDOUT or closest equivalent until the program is killed manually (or until you run out of memory).
Incorrect inputs can be ignored.
Scoring
The idea is that your program should appear correct, but contain hidden bugs that result in the above behavior. Your program should also be short (since programming assignments are graded by hand), but popularity is more important. Thus, your score is number of upvotes - (byte count / 100), higher score being better. Note that your score can be negative.
10Ughh, you ruined the question in the last paragraph :/ – Optimizer – 2015-02-11T15:14:58.617
@Optimizer I wanted to have a go at something else than a code-golf for once, and I though of this. I'm still open to making it a pure golf, though, since there are no answers yet. – Zgarb – 2015-02-11T15:17:28.777
Its my personal liking towards code-golf. Don't change the scoring based on it :) – Optimizer – 2015-02-11T15:19:35.470
I would have preferred a pure code golf, but I still like the backstory. I'm wondering how a clear-cut golf solution would score in terms of popularity. – John Dvorak – 2015-02-11T17:51:26.790
2What should happen for
n = 5
? – Théophile – 2015-02-11T18:32:40.9571@Théophile
n = 5
is an incorrect input, and can be ignored. – Zgarb – 2015-02-11T19:08:37.7004Do you get extra points if the program crashes your computer? Or perhaps emails your credit card information to someone? – Alex A. – 2015-02-11T19:29:41.753
6@Alex I prefer not to give a bonus for crashing the OS, since it would annoy people who may want to actually test the submissions. But if your program emails your credit card info to me, I'll give you a $20 bonus. </joke> – Zgarb – 2015-02-11T19:57:36.547
3
I'm voting to close this question as off-topic because underhanded challenges are no longer on-topic on this site. http://meta.codegolf.stackexchange.com/a/8326/20469
– cat – 2016-04-18T15:29:10.207