Created
February 7, 2020 17:51
-
-
Save christian-mann/74a6996c159347b53c9664cb9c8230fa to your computer and use it in GitHub Desktop.
Collatz-like thing (Python 3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import math | |
def factors(n): | |
while n % 2 == 0: | |
yield 2 | |
n //= 2 | |
for i in range(3, int(math.sqrt(n))+1, 2): | |
while n % i == 0: | |
yield i | |
n //= i | |
if n > 2: | |
yield n | |
def printfactors(n): | |
count = 0 | |
factor = 0 | |
s = "" | |
for p in factors(n): | |
if p == factor: | |
count += 1 | |
else: | |
if factor: | |
s += "%3d^%2d " % (factor, count) | |
factor = p | |
count = 1 | |
if factor: | |
s += "%3d^%2d " % (factor, count) | |
return s | |
def highestprimefactor(n): | |
return max(factors(n)) | |
def iterate(n): | |
p = highestprimefactor(n) | |
if n == p: return None | |
return n + p - 1 | |
def getcount(n, thresh=100): | |
m = n | |
count = 0 | |
while m and count < thresh: | |
m = iterate(m) | |
count += 1 | |
if m: | |
return 'INF' | |
else: | |
return count | |
if __name__ == "__main__": | |
for n in range(10, 10000, 2): | |
count = getcount(n) | |
if count != 'INF': | |
print(n, count, ' ', ' '.join(map(str, factors(n)))) | |
#n = int(sys.argv[1]) | |
#while n: | |
# print (n, ' '.join(map(str, factors(n)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
All odd numbers appear to terminate; some of the first terminating even numbers: