Python - 检查列表中的数字是否是数字的因子

发布于 2024-08-25 06:28:48 字数 397 浏览 4 评论 0原文

我有一个数字(整数)的列表(例如,从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海蓝天 2024-09-01 06:28:48

要检查是否存在任何剩余数字猜测的因素,您可以使用any()

hasfactors = any(guess % n == 0 for n in numbers)

要检查所有剩余数字是否都是质数,all() 可以使用。 (既然你说你已经阻止用户输入素数,我假设你有某种 isprime() 函数):

onlyprimes = all(isprime(n) for n in numbers)

To check if there are any factors of the number guess remaining you can use any():

hasfactors = any(guess % n == 0 for n in numbers)

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 of isprime() function):

onlyprimes = all(isprime(n) for n in numbers)
儭儭莪哋寶赑 2024-09-01 06:28:48

对于第一个问题,您可以使用列表推导式构建一个新列表,其中每个元素不是所选数字,也不是所选数字的因子(请参阅代码)。将其与您的原始列表进行比较。

$ python
>>> selected_number = 6
>>> [x for x in range(1,11) if selected_number % x]
[4, 5, 7, 8, 9, 10]

对于第二个问题,检查每个元素是否是素数。如果不是,检查没有因数的数字;对于每个元素,您可以对原始列表进行 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.

$ python
>>> selected_number = 6
>>> [x for x in range(1,11) if selected_number % x]
[4, 5, 7, 8, 9, 10]

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.

相思碎 2024-09-01 06:28:48

如果 L 是非零数字的列表,则作为数字 N 的因数的列表为:

factors = [x for x in L if N % x == 0]

当然,如果 N 在 L 中没有因数,则该列表将简单地为空。

我不确定你所说的“没有因数的数字”是什么意思,除非你的意思是“素数”(?)——在Python中检查素数有几个SO问题和答案,我会使用gmpy.is_prime (来自我的扩展 gmpy),但我当然有偏见; -)。

如果你的意思是“所有在 L 中没有因数的数字”,那么,它们的数量是无限多的,所以很难将它们全部列出来。为他们提供一个无限的生成器:

import itertools

def nofactorsinlist(L):
  for i in itertools.count():
    if any(x for x in L if i % x == 0):
      continue
    yield i

一些优化是可能的,但这一个非常简单,我不愿意在不准确理解你所追求的是什么的情况下添加复杂的优化!-)

If L is a list of non-zero numbers, the list of those which are factors of a number N is:

factors = [x for x in L if N % x == 0]

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:

import itertools

def nofactorsinlist(L):
  for i in itertools.count():
    if any(x for x in L if i % x == 0):
      continue
    yield i

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!-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文