简体   繁体   English

如何检查素因数列表以确定原始数字是否可以表示为 Python 中两个 3 位数字的乘积?

[英]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.

相关问题 检查一个数是否可​​以表示为 Python 中两个半质数之和 - Check whether a number can be expressed as a sum of two semi-prime numbers in Python 在Python中找到两个三位数乘积的最大回文 - Finding the largest palindrome of the product of two 3-digit numbers in Python 来自两个3位数的乘积的回文 - Palindrome from the product of two 3-digit numbers 我有一个数字的素因子的Python列表。 我如何(pythonically)找到所有因素? - I have a Python list of the prime factors of a number. How do I (pythonically) find all the factors? 如何生成3位数字的列表? - how to generate a list of 3-digit numbers? 欧拉计划:两个三位数数字的最大回文产品 - Project Euler: Largest palindrome product of two 3-digit number Python-检查列表中的数字是否为数字的因数 - Python - Check if numbers in list are factors of a number 如何比较 Python 中素数最多的数字? - How to compare numbers for highest number of prime factors in Python? 在我的计算中,要从两个3位数的乘积中得到最大的回文数,在哪里出错? - where am I going going wrong in my calculations to get largest palindrome made from the product of two 3-digit numbers? 如何生成一个 3 位数字的列表,它们的数字总和等于 17? - how to generate a list of 3-digit numbers the sum of their digits equal 17?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM