I'm trying to determine if a 6 digit number can be expressed as the product of two three digit numbers.
I've factored the 6 digit number down a list of it's primes in order from smallest to greatest, with a break statement that shut's the process down if one of those primes exceeds the three digit size I'm interested in.
I can't seem to formulate an algorithm that recombines the primes into all possible paired factors. I can check each pair for three digit-ness easily enough.
I'm working in python if that makes a difference. If someone can psuedo code or otherwise help outline the logical steps.
This is just the last step in a larger problem. If you're interested in the full context it's Euler Project Problem #4... https://projecteuler.net/problem=4
CodePudding user response:
For a very large amount of factors, as @Ken Y-N put in comments, your probably better off looping through all 3-digit numbers.
In your question, you seem to be asking how to generate subsets: I can't seem to formulate an algorithm that recombines the primes into all possible paired factors
.
We can do this easily enough utilizing binary:
If you have a list of (not necessarily distinct) factors, you can take the length, and generate all numbers from 0 to 2^length in its binary representation, making sure to save leading 0s.
Then, you can loop through each string, and if a character is 0, it is not included in a factor, and if its 1, it is. Then, we can easily generate the other factor using division. Next, we easily check the length.