为什么素数的代码不适用于数字 10?
我正在尝试用 python 编写代码来查找数字的所有素数。我的问题是,使用这行代码无法返回 10 的质数,列表仅返回 2。现在我从此页面改编了此代码 https://www.geeksforgeeks.org/prime-factor/ 因为我想让我的因素以列表的形式出现。
该网站的代码适用于数字 10,但是我不明白为什么当我运行自己的稍微修改过的版本时它不适用于数字 10。
我尝试在范围函数末尾添加+10,而不是+1,这确实解决了问题,但是我仍然不确定为什么我一开始就遇到问题。其次,+10 是否适用于所有数字而不会出现错误?理论上它应该是因为我应该只有 n 的平方根的因子,但我又不确定了。最后,如果 +10 确实有效,这不会使代码运行速度变慢,因为它会迭代不需要的循环,我怎样才能提高速度?
这是我使用的代码。
import math
def primefact():
n = int(input('What is your number?:'))
prime_factors = []
while n % 2 == 0: # Checks if number is divisible by 2
prime_factors.append(2) #repeats it until n is no longer divisible by 2
n = n / 2
for i in range(3, int(math.sqrt(n)) + 1, 2): # Testing for odd factors
while n % i == 0:
prime_factors.append(i)
n = n / i
print(prime_factors)
return
primefact()
I am trying to write a code in python to find all prime numbers of a number. My problem is that with this line of code it does not work to return the prime numbers of 10, the list only returns 2. Now I adapted this code from this page https://www.geeksforgeeks.org/prime-factor/ as I want to make my factors come out as a list.
The code from that website works for the number 10, however I do not understand why it does not work for number 10 when I run my own slightly modified version of it.
I have tried to +10 at the end of my range function, instead of a +1 and this does solve the problem, however I am still unsure as to why I even have a problem in the first place. Secondly, will the +10 work for all numbers with no error? In theory it should as I should only have factors unto square root of n, but I am not sure again. Lastly, if if the +10 does work, won't that make the code run slower as it will iterate through unneeded loops, how can I improve the speed?
This is my code that I used.
import math
def primefact():
n = int(input('What is your number?:'))
prime_factors = []
while n % 2 == 0: # Checks if number is divisible by 2
prime_factors.append(2) #repeats it until n is no longer divisible by 2
n = n / 2
for i in range(3, int(math.sqrt(n)) + 1, 2): # Testing for odd factors
while n % i == 0:
prime_factors.append(i)
n = n / i
print(prime_factors)
return
primefact()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我写的一段代码:
想象一下 func = findFactors(n)
func.factors 将返回一个包含数字 n 的所有因子的列表,
func.extendedPrimeFactors 将返回一个包含数字素数分解的列表,
func.primeFactors将返回一个列表,其中素数仅出现一次而不是 x 次
,那里有一个非常快的素数检查器。
(主要检查器用法:
素数(n).isPrime
)
Here's a piece of code that I wrote:
Imagine func = findFactors(n)
func.factors will returns a list with all the factors of the number n,
func.extendedPrimeFactors will return a list with the prime factorization of the number,
func.primeFactors will return a list with the primes appearing only once instead of x times
also, there's a really fast prime checker down there.
(Prime checker usage:
prime(n).isPrime
)
嗨,你刚刚忘记了方程的最后一部分
条件如果n是素数
这里是所有阶乘素数的列表,与素数不同
https://en.m.wikipedia.org/wiki/Table_of_prime_factors
Hi you just forgot the last part of the equation
Condition if n is a prime
Here Is the list of all factorial prime numbers, is not the same as prime numbers
https://en.m.wikipedia.org/wiki/Table_of_prime_factors