18
2
Specifically, Conway's PRIMEGAME.
This is an algorithm devised by John H. Conway to generate primes using a sequence of 14 rational numbers:
A B C D E F G H I J K L M N
17 78 19 23 29 77 95 77 1 11 13 15 15 55
-- -- -- -- -- -- -- -- -- -- -- -- -- --
91 85 51 38 33 29 23 19 17 13 11 14 2 1
For example, F is the fraction 77/29
.
So here's how the algorithm finds the prime numbers. Starting with the number 2
, find the first entry in the sequence that when multiplied together produces an integer. Here it's M
, 15/2
, which produces 15
. Then, for that integer 15
, find the first entry in the sequence that when multiplied produces an integer. That is the last one, N
, or 55/1
, which yields 825
. Write down the corresponding sequence. (The astute among you may recognize this as a FRACTRAN program.)
After some iterations, you'll get the following:
2, 15, 825, 725, 1925, 2275, 425, 390, 330, 290, 770, 910, 170, 156, 132, 116, 308, 364, 68, 4 ...
Note that the last item listed is 4
, or 2^2
. Behold our first prime number (the 2
exponent) generated with this algorithm! Eventually, the sequence will look like the following:
2 ... 2^2 ... 2^3 ... 2^5 ... 2^7 ... etc.
Thus, yielding the prime numbers. This is OEIS A007542.
The Challenge
Given an input number n
, either zero- or one-indexed (your choice), either output the first n
numbers of this sequence, or output the n
th number of this sequence.
Examples
The below examples are outputting the n
th term of the zero-indexed sequence.
n output
5 2275
19 4
40 408
Rules
- If applicable, you can assume that the input/output will fit in your language's native Integer type.
- The input and output can be given by any convenient method.
- Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
- Standard loopholes are forbidden.
- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
11Perhaps Conway's prime game would be a more descriptive name for this challenge than Let's Play a Game. That would make it easier to find this challenge back in the future. – Lynn – 2018-04-19T21:54:07.540
Can the output be a float?
408.0
instead of408
for example. – dylnan – 2018-04-19T23:09:29.753Unfortunately we don't have a (canonical) "Interpret Fractran" challenge. The one on Stack Overflow is locked.
– user202729 – 2018-04-20T01:33:38.807@dylnan Sure, that's fine. – AdmBorkBork – 2018-04-20T12:29:29.633