The "prime ant" is an obstinate animal that navigates the integers and divides them until there are only primes left!
Initially, we have an infinite array A containing all the integers >= 2 : [2,3,4,5,6,.. ]
Let p
be the position of the ant on the array. Initially, p = 0
(array is 0-indexed)
Each turn, the ant will move as follows:
- if
A[p]
is prime, the ant moves to the next position :p ← p+1
- else, if
A[p]
is a composite number, letq
be its smaller divisor > 1. We divideA[p]
byq
, and we addq
toA[p-1]
. The ant moves to the previous position:p ← p-1
Here are the first moves for the ant:
2 3 4 5 6 7 8 9 ...
^
2 3 4 5 6 7 8 9 ...
^
2 3 4 5 6 7 8 9 ...
^
2 5 2 5 6 7 8 9 ...
^
2 5 2 5 6 7 8 9 ...
^
2 5 2 5 6 7 8 9 ...
^
2 5 2 5 6 7 8 9 ...
^
2 5 2 7 3 7 8 9 ...
^
Your program should output the ant's position after n
moves. (you can assume n <= 10000
)
Test cases:
0 => 0
10 => 6
47 => 9
4734 => 274
10000 => 512
Edit. you can also use 1-indexed lists, it's acceptable to display the results 1, 7, 10, 275, 513 for the above test case.
This is code-golf, so the code with the shortest code in bytes wins.
n
(or whether the composite case could ever push the ant to the left of the initial2
). \$\endgroup\$