Subject: Re: [Tutor] Memory error - how to manage large data sets?
Kepala Pening [EMAIL PROTECTED] wrote
def sumEvenFibonacci( limit ):
a, b = 1, 1 # don't waste with a = 0
sum = 0
while b limit:
if b%2 == 0: sum += b
a, b = b, a + b
return sum
print sumEvenFibonacci
Kepala Pening wrote:
def sumEvenFibonacci( limit ):
a, b = 1, 1 # don't waste with a = 0
sum = 0
while b limit:
if b%2 == 0: sum += b
a, b = b, a + b
return sum
print sumEvenFibonacci( 200 )
Every 3rd element in the
.
- Original Message -
From: Alan Gauld [EMAIL PROTECTED]
To: tutor@python.org
Date: Thu, 31 Jul 2008 06:39:32 +0100
Subject: Re: [Tutor] Memory error - how to manage large data sets?
Kepala Pening [EMAIL PROTECTED] wrote
def sumEvenFibonacci( limit ):
a, b = 1, 1 # don't waste with a = 0
The original post was a little ambiguous: I need to find the sum of all
numbers at even positions in the Fibonacci series upto 2 million.
But the project euler page
(http://projecteuler.net/index.php?section=problemsid=2) is clear: Find the
sum of all the even-valued terms in the sequence
Karthik [EMAIL PROTECTED] wrote
I am new to Python programming, I was trying to work out a few
problems in
order to grasp the knowledge gained after going through the basic
chapters
on Python programming. I got stuck with a memory error.
Always show us the full error text, it contains a
Forgot to include the following information,
Platform - win32
Version - 2.5.1
Error message:
Traceback (most recent call last):
File C:\Python25\programs\fibo.py, line 10, in module
if i % 2 == 0:
MemoryError
Code:
fib = []
even = []
def fibonacci(x,y):
return
On Monday 28 July 2008 10:56, Karthik wrote:
Hi,
I am new to Python programming, I was trying to work out a few problems in
order to grasp the knowledge gained after going through the basic chapters
on Python programming. I got stuck with a memory error.
Following is what I did,
1.
+0530
From: Karthik [EMAIL PROTECTED]
Subject: Re: [Tutor] Memory error - how to manage large data sets?
To: tutor@python.org
Message-ID: [EMAIL PROTECTED]
Content-Type: text/plain; charset=us-ascii
Forgot to include the following information,
Platform - win32
Version - 2.5.1
Error
Karthik [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
Forgot to include the following information,
Platform - win32
Version - 2.5.1
Error message:
Traceback (most recent call last):
File C:\Python25\programs\fibo.py, line 10, in module
if i % 2 == 0:
MemoryError
OK, It does
On 29/07/2008, Daniel Sarmiento [EMAIL PROTECTED] wrote:
I tried to run your code and checked (with top) the memory ussage and
it uses more than 2 Gb of memory.
I tried to modify the code a little bit to use less memory and came up
with this:
fib = {0:0,1:1}
even = []
def
There's no need to keep any lists. The sum can be done on the fly, which is
perhaps a bit slower, but takes a constant amount of ram. Even storing every
other element (or every third, which is what he's trying to do: the elements
that are even numbers, not every other element.. See his
Alan Gauld [EMAIL PROTECTED] wrote
were infinite using floats! So you need to calculate the
total as you go without saving the values
I got curious so wrote the following function:
def fibtot(N):
... f0,f1,tot = 0,1,1
... for n in range(N):
... f = f0 + f1
... f0,f1 = f1,f
...
(the solution, of course, is to avoid storing all those numbers in the
first place)
I tried this:
fib = {0:0,1:1}
sum = 0
for j in xrange (2,100):
i = fib[j-1] + fib[j-2]
if i % 2 == 0:
sum += i
fib = {j-1:fib[j-1], j:i}
print sum
I guess it should come up with the
13 matches
Mail list logo