Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Kindle
Message
From
23/07/2011 11:57:50
 
 
General information
Forum:
Books
Category:
Other
Title:
Re: Kindle
Miscellaneous
Thread ID:
01518646
Message ID:
01518991
Views:
74
>>>>>
>>>>>Off topic but since I think you enjoy obscure code I just wrote this (purely as a mockup):
static IEnumerable<int> GetSequence()
>>>>>        {
>>>>>            bool b = false;
>>>>>            int x1 = 0;
>>>>>            int x2 = 1;
>>>>>
>>>>>            while (true)
>>>>>            {
>>>>>                b=!b;
>>>>>                yield return b ? x1 += x2 : x2 += x1;
>>>>>            }
>>>>>        ]
>>>>
>>>>working my way through it with a pencil
>>>>
>>>>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
>>>>
>>>>
>>>>If we number them as n0, n1, n2, n3, n4, ....
>>>>
>>>>then n[i] = n[i-1] + n[i-2]
>>>
>>>yep. Next in the series is the sum of the previous two items. It's called the Fibonacci series - but I actually got it wrong. It should be:
>>>0,1,1,2,3,5,8 etc
>>
>>
>>Take -1 as starting value for x1
>>
>>
>>  int x1 = -1;
>>  int x2 = 1;
>>
>>
>>As an alternative, you can do the calculation after the yield return
>
>Or maybe just:
int x1 = -1;
>int x2 = 1;
>while (true)
>      {
>           yield return x1 += x2;
>           yield return x2 += x1;
>       }
Excellent - no test any more
Gregory
Previous
Reply
Map
View

Click here to load this message in the networking platform