Discuss Help needed please in the Python Programming forum on Dev Shed. Help needed please Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
Posts: 3
Time spent in forums: 2 h 17 m 28 sec
Reputation Power: 0
Help needed please
Hi
I have written the following code to answer this question:
Each new term in the Fibonacci sequence is generated by adding the previous two terms.
By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
The code I have written is:
total = 0
old = 1
prev = 2
while 10 == 10:
current = old + prev
if current > 4000000:
break
if current % 2 == 0:
total = current + total
old = prev
prev = current
print total
total = 0
old = 1
prev = 2
while 10 == 10:
current = old + prev
if current > 4000000:
break
if current % 2 == 0
total = current + total
old = prev
prev = current
print total
Im now trying to amend the code to show the total of every fifth Fibonacci number less than 20,000,000.
I know its the if current % 2 == 0 line that needs to be amended but can not work it out. I found that if you start the series with 0,1,1 etc then every 5th number is divisable by 5. I can not see a pattern when you start the series 1,2,3 etc.
Posts: 3,348
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 35 m 46 sec
Reputation Power: 383
Code:
NB. solution
NB. www.jsoftware.com
Filter =: (#~`)(`:6)
F =: ((, [: +/_2&{.)^:70)0 1x NB. F is the first 72 Fibonacci numbers
+/((0 = 5&|@i.@#)Filter)(<&20e6 Filter) F NB. sum of every 5th
10141965
(,:~ i.@#)20{.F NB. numbering system used to decide "every 5th"
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
agrees with python
Code:
total = 0
old = 0
prev = 1
i = 0
while old < 20000000:
current = old + prev
if not (i % 5):
total += old
(old,prev,) = (prev,current,)
i += 1
print total
__________________
[code]Code tags[/code] are essential for python code!
Last edited by b49P23TIvg : January 23rd, 2013 at 08:21 PM.
Reason: shorter but wider
Posts: 3,348
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 35 m 46 sec
Reputation Power: 383
Why would you need some sort of relationship other than "every 5th" if all you need is to sum? The code I posted has a counter, i, which you could test. If the remainder after integer division by 5 is 2 then you'd have every 5th term with an offset. I'd say this sequence of least significant digits base 10 repeats forever in the Fibonacci series. Don't think it helps.
0 1 1 2 3 5 8 3 1 4 5 9 4 3 7 0 7 7 4 1 5 6 1 7 8 5 3 8 1 9 0 9 9 8 7 5 2 7 9 6 5 1 6 7 3 0 3 3 6 9 5 4 9 3 2 5 7 2 9 1
Posts: 3
Time spent in forums: 2 h 17 m 28 sec
Reputation Power: 0
hi
sorry im new to all this. i was trying to work it in excel first to see how you could do it. wasnt sure if it had something to do with the golden rule. if you look at every 5th number they are a mix of odd and even numbers so not divisbale by 2 or 5...
if you do it manually in excel the answer is 1,479,692 .
i cant get to that with anything i have tried and your code.
Posts: 291
Time spent in forums: 3 Days 18 h 19 m 46 sec
Reputation Power: 6
A quick glance shows a relationship for the 5th column number multiplied by 11. Multiply the previous 5th column number by 11 and subtract the multiplied amount from this number and see if the subtracted result has any significance. If you don't see any relationship then you are on your own.
Quote:
wasnt sure if it had something to do with the golden rule
Do unto others as you would want them to do unto you? Or is it the golden ratio ~ 1.62 which is the relationship between any two Fib numbers in succession. You can't get good answers unless you ask good questions.
Last edited by dwblas : January 24th, 2013 at 11:28 AM.