The "prime frog" is a strange animal that jumps between integers, until it arrives on 3 or 19...
Your program should accept an integer n
as input and output the result of the below algorithm (3
or 19
).
For a given integer n >= 2
:
- Let
f
be the position of the frog. It is initially set ton
- if
f = 3
orf = 19
: the frog stops jumping - halt the program and outputf
. - if
f
is prime : the frog jumps to the position2×f-1
. Go back to step 2. - if
f
is composite : letd
bef
's biggest prime divisor. The frog jumps to the positionf-d
. Go back to step 2.
Examples:
An example with n = 5
:
5 > 9 > 6 > 3 stop
The program should output 3
.
Another example with n = 23
:
23 > 45 > 40 > 35 > 28 > 21 > 14 > 7 > 13 > 25 > 20 > 15 > 10 > 5 > 9 > 6 > 3 stop
Again, the program should output 3
.
Test cases:
10 => 3
74 => 19
94 => 3
417 => 3
991 => 19
9983 => 19
You can assume 1 < n < 1000000
(I have checked the program ends for these values).
3
or19
, we could change item 2. in the algorithm to say that if the frog has entered any loop (encountered a position it has seen before), then it ceases the jumping and returns the smallest member of that loop. \$\endgroup\$