OFFSET
0,3
COMMENTS
a(n) gives the position after the n-th step on the infinite sequence (2, 3, 4, ...). If the value at the ant's position is prime, the ant steps to the right. If the value is composite, the ant divides the number by its least prime divisor, steps to the left, and adds the divisor to the new position.
LINKS
Peter Kagey, Table of n, a(n) for n = 0..10000
Code Golf Stack Exchange user Super Chafouin, The Prime Ant.
EXAMPLE
a(0) = 0:
2 3 4 5 6 7 ...
^ (prime; step to the right)
a(1) = 1:
2 3 4 5 6 7 ...
^ (prime; step to the right)
a(2) = 2:
2 3 4 5 6 7 ...
^ (composite; divide by 2, step to the left, add 2)
a(3) = 1:
2 5 2 5 6 7 ...
^ (prime; step to the right)
a(4) = 2:
2 5 2 5 6 7 ...
^
MATHEMATICA
NestWhileList[If[PrimeQ[#2[[#1]] ], {#1 + 1, #2}, {#1 - 1, ReplacePart[#2, {#1 - 1 -> #2[[#1 - 1]] + #3, #1 -> #2[[#1]]/#3}]} & @@ {#1, #2, FactorInteger[#2[[#1]] ][[1, 1]]}] & @@ # &, {2, Range[10^2]}, First@ # < Length@ Last@ # &, 1, 78][[All, 1]] - 2 (* Michael De Vlieger, Oct 15 2017 *)
CROSSREFS
KEYWORD
AUTHOR
Peter Kagey, Oct 14 2017
STATUS
approved