Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 23rd, 2013, 03:02 PM
Wickham80 Wickham80 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 Wickham80 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Does anyone have any ideas please???

Reply With Quote
  #2  
Old January 23rd, 2013, 08:16 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,357 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 10 m 17 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

Reply With Quote
  #3  
Old January 24th, 2013, 07:42 AM
Wickham80 Wickham80 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 Wickham80 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 17 m 28 sec
Reputation Power: 0
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???

Reply With Quote
  #4  
Old January 24th, 2013, 08:32 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,357 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 10 m 17 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

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 ...

Reply With Quote
  #5  
Old January 24th, 2013, 08:55 AM
Wickham80 Wickham80 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 Wickham80 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old January 24th, 2013, 09:44 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,357 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 10 m 17 sec
Reputation Power: 383
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.

Reply With Quote
  #7  
Old January 24th, 2013, 11:19 AM
dwblas dwblas is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 291 dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 18 h 27 m 55 sec
Reputation Power: 7
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.
Comments on this post
Dietrich agrees: nicely said

Last edited by dwblas : January 24th, 2013 at 11:28 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Help needed please

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap