Sep-01-2020, 03:54 PM
First the problem.
1. Start with a semi-prime (eg, 15 = 3 * 5).
2. Take the factors and concatenate them both forwards and backwards to give 2 new numbers. (Ie, 35 and 53)
3. Test for semi-prime-ness. If semi-prime, go to step 2. If not, end that branch.
The process ends when no further semi-primes are generated.
This creates a tree of numbers.
The tree is built as you go from the top down.
See image for the tree of 15. [attachment=978]
For 15 the length of the chain of semi-primes is 7.
The object is to find the start number with the longest chain of semi-primes.
I "think" this might be a good place to use recursion. However, I've never done
recursion and only have a hazy grasp of it. I've seen a few videos about it
but they either don't show an example or the examples they give are for
factorials or Fibonacci. Neither of which seems to be of help with this
problem.
To get the semi-primes themselves and their factors I thought I could either
calculate them on the fly or pre-compute them and put them into a list or
dictionary or something. But as for the recursion part how would this problem
be coded in Python? TIA
1. Start with a semi-prime (eg, 15 = 3 * 5).
2. Take the factors and concatenate them both forwards and backwards to give 2 new numbers. (Ie, 35 and 53)
3. Test for semi-prime-ness. If semi-prime, go to step 2. If not, end that branch.
The process ends when no further semi-primes are generated.
This creates a tree of numbers.
The tree is built as you go from the top down.
See image for the tree of 15. [attachment=978]
For 15 the length of the chain of semi-primes is 7.
The object is to find the start number with the longest chain of semi-primes.
I "think" this might be a good place to use recursion. However, I've never done
recursion and only have a hazy grasp of it. I've seen a few videos about it
but they either don't show an example or the examples they give are for
factorials or Fibonacci. Neither of which seems to be of help with this
problem.
To get the semi-primes themselves and their factors I thought I could either
calculate them on the fly or pre-compute them and put them into a list or
dictionary or something. But as for the recursion part how would this problem
be coded in Python? TIA