Sep-01-2020, 10:57 PM
As for the previous questions, the only requirement for the start number is that
it is a semi-prime. And I doubt whether the chain lengths would ever get above
20 or 30.
Ok, here is my revised code. I fixed the previous errors. I've added the routine
to pull out the data from the dictionary and create the new numbers. But I don't
really know where to go from here.
it is a semi-prime. And I doubt whether the chain lengths would ever get above
20 or 30.
Ok, here is my revised code. I fixed the previous errors. I've added the routine
to pull out the data from the dictionary and create the new numbers. But I don't
really know where to go from here.
import math sp_dict = {} primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] def semi(): for i in range(9, 101, 2): #for the real thing this range will be much larger divisors = [] for j in primes: if j > math.sqrt(i): continue else: div = i / j if div == int(div): b = div / j if b == int(b) and b > 1: continue else: divisors.append(j) divisors.append(div) if len(divisors) == 2: value = "[" + str(divisors[0]) + ", " + str(int(divisors[1])) + "]" sp_dict[i] = value print() print(sp_dict) print() # checks dictionary for entry. if there, pulls out factors # and assigns to variables def check_if_sp(n): if n in sp_dict: x = sp_dict[n] x = x.strip('[') x = x.strip(']') x = x.split(', ') fact1 = x[0] fact2 = x[1] print(f'{n} = {fact1} * {fact2}') return fact1, fact2 else: print(n,' is not a semi-prime.') a = semi() n = int(input('Input a number: ')) fact1, fact2 = check_if_sp(n) # creates 2 new numbers from the factors to check new1 = int(fact1 + fact2) new2 = int(fact2 + fact1) print(f'New numbers are {new1} and {new2}')Ok, how do I proceed to create the tree as in the image in the first post?