Sep-02-2020, 07:14 PM
You could use recursion to build the tree. The key is to suppose that the problem is solved. Let
We need to define what the root of a tree will be. We'll take a tuple ((factor0, factor1), (subroot0, subroot1)). For this we could use a namedtuple. Here is an algorithm
func()
be a function that takes a number as argument and returns None if it is not a semi-prime or the root of this semi-prime's tree if it is a semi-prime.We need to define what the root of a tree will be. We'll take a tuple ((factor0, factor1), (subroot0, subroot1)). For this we could use a namedtuple. Here is an algorithm
Root = namedtuple('Root', 'factor subroot') def func(number): try: fac0, fac1 = decompose_semi_prime_in_factors(number) except NotSemiPrime: return None root0 = func(int(f'{fac0}{fac1}')) root1 = None if fac0 != fac1: root1 = func(int(f'{fac1}{fac0}')) return Root((fac0, fac1), (root0, root1))