utomated testing is "A Good Thing" :-)
Alan G.
- 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
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 Fibo
, 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
> > sum = 0
> > w
"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( 200 )
Does it work for limit = 2?
Alan G.
__
s Fuller <[EMAIL PROTECTED]>
To: tutor@python.org
Date: Mon, 28 Jul 2008 12:27:58 -0500
Subject: Re: [Tutor] Memory error - how to manage large data sets?
> On Monday 28 July 2008 10:56, Karthik wrote:
> > Hi,
> >
> >
> >
> > I am new to Python programming, I was tr
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=problems&id=2) is clear: "Find the
sum of all the even-valued terms in the sequence
>(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 ri
"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 =
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 exampl
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 = []
>
>
"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
if i % 2 == 0:
MemoryError
OK, It does look
:08 +0530
> From: "Karthik" <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] Memory error - how to manage large data sets?
> To:
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset="us-ascii"
>
> Forgot to include the following i
> 1. I need to find the sum of all numbers at even positions in the
> Fibonacci series upto 2 million.
>
> 2. I have used lists to achieve this.
I see. You may want to produce a "sequence" or "iterator" of
fibonacci numbers rather than an explicit list. As it is, your
machine does not ha
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 di
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
if i % 2 == 0:
MemoryError
Code:
fib = []
even = []
def fibonacci(x,y):
return x+y
"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
16 matches
Mail list logo