[英]How do I check a list of prime factors to determine if the original number can be expressed the product of two 3-digit numbers, in Python?
I'm trying to determine if a 6 digit number can be expressed as the product of two three digit numbers.我试图确定一个 6 位数字是否可以表示为两个三位数字的乘积。
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.我已将 6 位数字按从小到大的顺序分解为素数列表,如果其中一个素数超过我感兴趣的三位数大小,则使用 break 语句关闭进程。
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.我在 python 工作,如果这有什么不同的话。 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
如果您对完整的上下文感兴趣,那就是 Euler Project Problem #4... https://projecteuler.net/problem=4
For a very large amount of factors, as @Ken YN put in comments, your probably better off looping through all 3-digit numbers.正如@Ken YN 在评论中所说,对于大量因素,您最好循环遍历所有 3 位数字。
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
.在您的问题中,您似乎在问如何生成子集:
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.如果您有一个(不一定是不同的)因子列表,您可以获取长度,并以二进制表示形式生成从 0 到 2^length 的所有数字,确保保存前导 0。
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.然后,您可以循环遍历每个字符串,如果一个字符为 0,则不包含在因子中,如果为 1,则包含。 Then, we can easily generate the other factor using division.
然后,我们可以很容易地使用除法生成另一个因子。 Next, we easily check the length.
接下来,我们很容易检查长度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.