Python - 检查列表中的数字是否是数字的因子
我有一个数字(整数
)的列表
(例如,从1到10)。
它们不一定是连续的,但按升序排列。
我已多次提示用户输入可用号码的选择。输入该数字后,它会连同可能存在的任何因素一起从列表中删除。
我已经阻止用户选择素数。然而,在某个时间点,那里可能存在没有剩余因子的非素数。
我对 Python 比较陌生,所以我在实现时遇到了麻烦:
检查所选数字是否没有剩余因子(即使它不是质数)。
检查是否仅保留素数,或不保留素数 因子。
我正在考虑使用 for
语句,但我不确定如何实现它们。任何人都可以提供建议或代码吗?提前致谢...
I have a list
of numbers (integers
) (say, from 1 to 10).
They're not necessarily consecutive, but they are in ascending order.
I've prompted the user multiple times to enter a choice of the available numbers. When that number is entered, it is removed from the list along with any of its factors that may be there.
I've prevented the user from selecting prime numbers. However, at some point in time, there may be non-prime numbers there, which have no factors remaining.
I'm relatively new to Python, so I'm having trouble implementing:
Checking if the number selected has no factors remaining (even if it is not prime).
Checking if only prime numbers remain, or numbers without
factors.
I'm thinking of using for
statements, but I'm not sure exactly how to implement them. Can anyone offer advice, or code? Thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要检查是否存在任何剩余数字
猜测
的因素,您可以使用any()
:要检查所有剩余数字是否都是质数,
all()
可以使用。 (既然你说你已经阻止用户输入素数,我假设你有某种isprime()
函数):To check if there are any factors of the number
guess
remaining you can useany()
:To check if all the remaining numbers are prime,
all()
can be used. (Since you say you already prevented the user from inputting prime numbers I assume you have some kind ofisprime()
function):对于第一个问题,您可以使用列表推导式构建一个新列表,其中每个元素不是所选数字,也不是所选数字的因子(请参阅代码)。将其与您的原始列表进行比较。
对于第二个问题,检查每个元素是否是素数。如果不是,检查没有因数的数字;对于每个元素,您可以对原始列表进行
mod
并检查它是否是一个由零组成的列表。不过,我确信有更快的方法。For the first problem, you could use list comprehensions to build a new list where each element is not the number selected and not a factor of the number selected (see code). Compare this with your original list.
For the second problem, check if each element is prime. If not, check for numbers without factors; for each element, you might
mod
over the original list and check if it's a list of zeros. I'm sure there's a faster way, though.如果 L 是非零数字的列表,则作为数字 N 的因数的列表为:
当然,如果 N 在 L 中没有因数,则该列表将简单地为空。
我不确定你所说的“没有因数的数字”是什么意思,除非你的意思是“素数”(?)——在Python中检查素数有几个SO问题和答案,我会使用gmpy.is_prime (来自我的扩展 gmpy),但我当然有偏见; -)。
如果你的意思是“所有在 L 中没有因数的数字”,那么,它们的数量是无限多的,所以很难将它们全部列出来。为他们提供一个无限的生成器:
一些优化是可能的,但这一个非常简单,我不愿意在不准确理解你所追求的是什么的情况下添加复杂的优化!-)
If L is a list of non-zero numbers, the list of those which are factors of a number N is:
The list will simply be empty if N has no factors in L, of course.
I'm not sure what you mean by "numbers without factors", unless you mean "primes" (?) -- there have been several SO questions and answers on checking primality in Python, I'd use
gmpy.is_prime
(from my extension gmpy) but then of course I'm biased;-).If you mean, "all numbers that have no factors in L", well, there's infinitely many of them, so it's kind of hard to make a list of them all. An unbounded generator for them:
Some optimizations would be possible, but this one is really simple and I'm loath to add complicated optimizations without understanding exactly what it is that you're after!-)