OFFSET
1,2
COMMENTS
Inspired by the Collatz conjecture, I tried to generalize for more divisors than 2. I quickly came up with a formula, and then discovered that Carnielliy had written about it previously in 2015.
LINKS
Carnielliy, Walter. Some Natural Generalizations Of The Collatz Problem, Applied Mathematics E-Notes, 2015, page 208.
PROG
(Python)
def f(n, d):
"""
A Collatz-like function.
When d == 2 this becomes '3x+1' problem exactly.
"""
r = n % d
if r == 0:
return n // d
else:
# Produce a larger number that is divisible by d.
return (d + 1) * n + d - r
def steps(n, d):
"""
Return the number of steps needed to reach 1, or -1 if a 1 is never reached.
"""
count = 0
seen = set([1])
x = n
# Loop until a cycle is detected.
while x not in seen:
seen.add(x)
x = f(x, d)
count += 1
if x == 1:
return count
else:
# There was a cycle
return -1
# Create a bunch of terms for d=5
S = [steps(x, d=5) for x in range(1, 1000)]
print(S)
CROSSREFS
KEYWORD
nonn
AUTHOR
Matt Donahoe, Aug 22 2020
STATUS
approved