24
1
Recently I read the novel "The Solitude of Prime Numbers" where the main characters are somewhat compared to twin prime numbers ("always together, but never touching").
A twin prime is a prime number that is either 2 less or 2 more than another prime number —for example, the twin prime pair (41, 43). In other words, a twin prime is a prime that has a prime gap of two. Sometimes the term twin prime is used for a pair of twin primes; an alternative name for this is prime twin or prime pair. Wikipedia
Although I didn't like much the depressing novel, and since I have fallen into PPCG lately, that raised a question in my mind...
Task:
Given a positive integer number N > 4, find the lonely prime numbers (AKA isolated prime numbers) between the closest couples of twin primes.
Please note that in this case with the term lonely prime numbers, I mean all the prime numbers that are not twin primes and between couples of twin primes. That's why N > 4 because the first two couples of prime numbers are (3, 5) and (5, 7).
Example:
- N = 90.
- Find the first two couples of twin primes < N and > N. They are: (71, 73) and (101, 103).
- Find the lonely primes in the range > 73 and < 101.
- They are: 79, 83, 89, 97.
Special cases:
- If N is in between two twin prime numbers, find the closest couples of twin primes > N+1 and < N-1. Example: N=72, find the closest couples of twin primes > 73 and < 71 then exclude from the list 71 and 73 because they are not lonely primes. So for N = 72 the expected result is: 67,
71,73, 79, 83, 89, 97 - If N belongs to a couple of twin primes, for example N = 73, the closest couples of twin primes are (71, 73) and (101, 103). If N = 71, the closest couples of twin primes are (59, 61) and (71, 73).
Test cases:
N = 70 > Lonely primes are: 67
N = 71 > Lonely primes are: 67
N = 72 > Lonely primes are: 67, 79, 83, 89, 97 (not the twins 71 and 73)
N = 73 > Lonely primes are: 79, 83, 89, 97
N = 90 > Lonely primes are: 79, 83, 89, 97
N = 201 > Lonely primes are: 211, 223
N = 499 > Lonely primes are: 467, 479, 487, 491, 499, 503, 509
Rules:
- Write a full program or function that will take the number N from standard input.
- Output the list of lonely primes in a readable format as csv, list, array, etc.
- Shortest code wins.
- Please include (when possible) a testable online fiddle.
4What is the expected output for inputs like 71, 72 or 73? – Martin Ender – 2016-09-27T16:39:33.753
1Lonely Prime AKA Isolated Prime – Digital Trauma – 2016-09-27T17:32:15.627
@MartinEnder I extended my question with special cases. Thanks for the clarification. – Mario – 2016-09-27T17:56:49.997
@DigitalTrauma I used Lonely Prime (in italics) on purpose to follow the novel's title. I added anyway the proposed specification. – Mario – 2016-09-27T18:03:19.370
1I find the special cases spoil the challenge a little (and were added when some answers had already been posted) – Luis Mendo – 2016-09-27T22:25:52.770
1@JonathanAllan Yes you can consider N > 4 because the first two couples of twin prime numbers are (3, 5) and (5, 7). I addedd the specification to make it clear to everyone. – Mario – 2016-09-28T06:22:46.740
@LuisMendo I added the special cases as clarification for the expected results, expecially in the case when N is in between a couple of twin primes. – Mario – 2016-09-28T06:39:01.203
Note: There are no prime triplets or quadruplets without an uneven non-prime between them. Triplets always have a number divisble by 3 between them, and Quadruplets always a number divisible by 15. – Titus – 2016-09-28T10:10:41.133
What is the expected result for
N=5
? – Titus – 2016-09-28T10:56:50.117@Titus for N=5 the result is empty output because between the first two couples of twin primes there are no lonely primes. – Mario – 2016-09-28T11:04:22.417
5
is twin to3
and7
... until now it was undefined which twin should count. Apparently7
. Thanks; this renders regarding5
as even more special obsolete. – Titus – 2016-09-28T11:14:18.753Do the printed primes need to be in any particular order? – AdmBorkBork – 2016-09-28T19:05:30.403
@TimmyD no particular order just a list of them – Mario – 2016-09-28T19:47:47.350