January 23rd, 2013, 03:02 PM

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 evenvalued 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.
Does anyone have any ideas please???
January 23rd, 2013, 08:16 PM

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
Last edited by b49P23TIvg; January 23rd, 2013 at 08:21 PM.
Reason: shorter but wider
[code]
Code tags[/code] are essential for python code and Makefiles!
January 24th, 2013, 07:42 AM

thank you for your help.
I have this version but have been asked to work it out starting FIB series with 1 and 2 and ignoring 0 and first 1.
I can not seem to find any relationship between the 5th numbers in the series if you change the starting numbers???
January 24th, 2013, 08:32 AM

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
Again, in j (www.jsoftware.com
Code:
0,10{:"1({:,+/)^:(<300)0 1x
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 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 0 1 1 2 3 5 8 3 ...
[code]
Code tags[/code] are essential for python code and Makefiles!
January 24th, 2013, 08:55 AM

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.
January 24th, 2013, 09:44 AM

If I solved the problem manually with excel I'd be institutionalized before I got an answer.
Code:
2}.(<&20e6 Filter) F NB. Fibonacci numbers less than 20 million, starting with 1 2 3
1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352
_5 [\ 2}.(<&20e6 Filter) F NB. Same numbers, arranged in to 5 columns
1 2 3 5 8
13 21 34 55 89
144 233 377 610 987
1597 2584 4181 6765 10946
17711 28657 46368 75025 121393
196418 317811 514229 832040 1346269
2178309 3524578 5702887 9227465 14930352
+/ _5 [\ 2}.(<&20e6 Filter) F NB. Sum of columns
2394193 3873886 6268079 10141965 16410044
The smallest sum exceeds the sum you computed.
[code]
Code tags[/code] are essential for python code and Makefiles!
January 24th, 2013, 11:19 AM

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.
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.
Comments on this post
Last edited by dwblas; January 24th, 2013 at 11:28 AM.