python overflowerror_Python “OverflowError”

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

I am just starting to learn to code in Python. I am trying to write some code to answer this Project Euler Question:

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

My program works with the test case of 13195, but when I try to enter 600851475143, I get the error: "OverflowError: range() results has too many items" Does anyone know how I can fix this?

Here is my code: class Euler3: "A class to find the largest prime factor of a given number" n = 600851475143 primeFactors = [] for i in range(2,n): if (n%i ==0): primeFactors.append(i) n = n/i i = i -1 #reset i print primeFactors

Any help or suggestions would be much appreciated!

回答1:

The range function creates a list and tries to store it in memory. Creating a list many numbers long is what's causing the OverflowError. You can use xrange instead to get a generator which produces the numbers on demand.

That said, I think you'll find that your algorithm is way too slow for calculating large primes. There are a lot of prime number algorithms, but I might suggest checking out the Sieve of Eratosthenes as a starting point.

EDIT: Properly xrange actually doesn't return a generator, but an xrange object which behaves a lot like a generator. I'm not sure if you care, but it was bugging me that i wasn't precise!

回答2:

I'm guessing you're using python 2 and not python 3 -- range(2,n) actually constructs a list! You don't have enough memory to store 600 billion numbers! xrange should be fine, though.

Also, your idea of i=i-1 doesn't work. For loops don't work like C, and that hack only works in C-style loops. The for loop iterates over range(2,n). If i gets the value 5 at once iteration, then no matter what you do to i, it still gets 6 the next time through.

Also, the list range(2,n) is constructed when you enter the loop. So when you modify n, that doesn't change anything.

You're going to have to rethink your logic a bit.

(if you don't believe me, try using 175 as a test case)

As a last comment, you should probably get in the habit of using the special integer division: n = n // i. Although / and // work the same in python 2, that is really deprecated behavior, and they don't work the same in python 3, where / will give you a floating point number.

回答3:

You can fix the problem by using xrange instead of range

Your next problem will be that the program takes way too long to run because you need to break out of the loop under some condition

A better way to deal with repeat factors is to replace the if with a while while (n%i ==0): primeFactors.append(i) n = n/i

回答4:

n = 600851475143 primeFactors = [] for i in range(2,n):

i think you can optimize the function by noticing that for i in range(2,n):

you can replace range(2,n)

by range(2,int(sqrt(n))+2)

because, you can see wiki...

回答5:

This is the best way to find primes that I've found so far: def isprime(n): #make sure n is a positive integer n = abs(int(n)) #0 and 1 are not primes if n

回答6:

This took me about 5 secs to get the answer. import math def is_prime_number(x): for i in range(int(math.sqrt(x)), 2, -1): if x % i == 0: return False return True def next_prime_number(number): #Returns the next prime number. if number % 2 == 0 and number != 2: number += 1 while not is_prime_number(number): number += 2 return number num = 600851475143 i = 2 while (i

回答7:

xrange won't probably help you(or it may!),but the key thing here is to reliaze that you don't need to find the prime numbers up till 600851475143 or the factors of 600851475143,but it's prime factors,so... Use the good old prime factorization method ,like so: A=600851475143 n=2 fac=[] while(n

This will return the argest prime factor almost instantly

回答8:

I was battling with this Python "OverflowError", as well, working on this project. It was driving me nuts trying to come up with a combination that worked.

However, I did discover a clever, at least I think so :), way to do it.

Here is my code for this problem. def IsNumberPrime(n): bounds = int(math.sqrt(n)) for number in xrange(2,bounds+2): if n % number == 0: return False return True def GetListOfPrimeFactors(n): primes = [] factors = GetListOfFactors(n) if n % 2 == 0: primes.append(2) for entries in factors: if IsNumberPrime(entries): primes.append(entries) return primes GetListOfPrimeFactors(600851475143) def GetListOfFactors(n): factors = [] bounds = int(math.sqrt(n)) startNo = 2 while startNo

What I did was take the factors of the number entered and enter them into a list "factors". After that, I take the list of factors and determine which ones are primes and store them into a list, which is printed.

I hope this helps

-- Mike

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值