Sep-02-2020, 07:14 PM
(This post was last modified: Sep-02-2020, 07:16 PM by Gribouillis.)
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
1 2 3 4 5 6 7 8 9 10 11 12 |
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)) |