Problem with Fibonacci

Hello my problem is that I tried to write a program that sum up the Fibonacci number. It is a iterative solution. The problem is that the highest number the 47 Fibonacci number is. And I don't know why.

Here is my source code:

int n = 47;
if(n==1||n==2)
{
digitalWrite(ledr,LOW);
digitalWrite(ledg,LOW);
delay(2000);
}
if(n>=3)
{
float fib[n];
fib[0] = 1;
fib[1] = 1;
for(int i = 2; i<n; i++)
{
fib*=fib[i-2]+fib[i-1];*
_ Serial.println(fib*);*_
Thank you :wink:

Hi,

Does your code compile without errors? The section of it you've copied and pasted here is incomplete so certainly wouldn't. Can you post the whole of your program, and use the [ code ] [ /code ] tags around it (it's the button with the # on it).

Geoff

Here is the whole source code :slight_smile:

'

int ledr = 8;
int ledg = 3;

void setup()
{
  pinMode(ledr, OUTPUT);
  pinMode(ledg, OUTPUT);
  Serial.begin(9600); 
}

void loop()
{
  int n = 47;
  digitalWrite(ledr,LOW);
  digitalWrite(ledg, LOW);
  delay(2000);
  if(n==1||n==2)
  {
    digitalWrite(ledr,LOW);
    digitalWrite(ledg,LOW);
    delay(2000);
  }
  if(n>=3)
  {
    float fib[n];
    fib[0] = 1;
    fib[1] = 1;
    for(int i = 2; i<n; i++)
    {
      fib[i]=fib[i-2]+fib[i-1];
      Serial.println(fib[i]);
       
    }
    digitalWrite(ledr,LOW);
    digitalWrite(ledg,HIGH);
    delay(5000);
  }
  digitalWrite(ledr,HIGH);
  digitalWrite(ledg,LOW);
  delay(2000);
}

'

Moderator edit: tags added, italics removed.

Why are you using a float? it is integer math so would be faster to use an 'unsigned long', plus would keep its accuracy. A float may be able to hold a bigger number, but it can't hold the same precision, and wont show you the correct value from the 41st onward (7dp using standard form give a maximum of 99,999,999)

Also, this line:
fib=fib[i-2]+fib[i-1];
should be:
fib[i ] = fib[i-2] + fib[i-1];

otherwise the value isn't saved anywhere. EDIT: I think you may have used that but because you didn't put it in `` markers, it took the [i ] to mean italic font.

Just as a side note, the seed numbers for the Fibonacci series are [0,1], not [1,1].
0,1,1,2,3,5,8,...

What happens if n>47?

I see from the reference that floats can handle numbers bigger than you need (3.4E38), but that they are subject to limited precision, of 6-7 digits.

If you use unsigned long, you get:

2
3
5
8
13
21
...
...
...
1836311903
2971215073

which is indeed correct.

Beyond that (n>47), the number becomes larger than 32bits, so cant be stored in an unsigned long, and certainly can't be stored in a float with full precision.

If you want larger than this, you would need to use a 64bit variable. I think arduino supports "unsigned long long"? But Serial.print() doesnt.

Hi,

Making the change to the seed numbers, as mentioned by Tom above...

int ledr = 8;
int ledg = 3;

void setup()
{
  pinMode(ledr, OUTPUT);
  pinMode(ledg, OUTPUT);
  Serial.begin(9600); 
}

void loop()
{
  int n = 47;
  digitalWrite(ledr,LOW);
  digitalWrite(ledg, LOW);
  delay(2000);
  if(n==1||n==2)
  {
    digitalWrite(ledr,LOW);
    digitalWrite(ledg,LOW);
    delay(2000);
  }
  if(n>=3)
  {
    float fib[n];
    fib[0] = 0;  // changed from 1
    fib[1] = 1;
    for(int i = 2; i<n; i++)
    {
      fib[i]=fib[i-2]+fib[i-1];
      Serial.println(fib[i]);

    }
    digitalWrite(ledr,LOW);
    digitalWrite(ledg,HIGH);
    delay(5000);
  }
  digitalWrite(ledr,HIGH);
  digitalWrite(ledg,LOW);
  delay(2000);
}

Results in this :

1.00
2.00
3.00
5.00
8.00
13.00
21.00
34.00
55.00
89.00
144.00
233.00
377.00
610.00
987.00
1597.00
2584.00
4181.00
6765.00
10946.00
17711.00
28657.00
46368.00
75025.00
121393.00
196418.00
317811.00
514229.00
832040.00
1346269.00
2178309.00
3524578.00
5702887.00
9227465.00
14930352.00
24157816.00
39088168.00
63245984.00
102334152.00
165580128.00
267914272.00
433494400.00
701408640.00
1134903040.00
1836311680.00

You're not too far off.

Cheers ! Geoff

Hey thank you for your comments and for your help!

The problem is that the function 'Serial.println()' doesn't work, because the highest number that 'println()' can describe is 2³².

Yours faithfully!
Niklas

Mammoth code. Can go up to 94. Beyond that you overflow even an unsigned long long (64bit). Now you'll be asking for an "unsigned long long long" (128bit) type, but they dont exist :frowning:

int ledr = 8;
int ledg = 3;

void setup()
{
  pinMode(ledr, OUTPUT);
  pinMode(ledg, OUTPUT);
  Serial.begin(9600); 
}

void loop()
{
  int n = 94;
  digitalWrite(ledr,LOW);
  digitalWrite(ledg, LOW);
  delay(2000);
  if(n==1||n==2)
  {
    digitalWrite(ledr,LOW);
    digitalWrite(ledg,LOW);
    delay(2000);
  }
  if(n>=3)
  {
    unsigned long long fib[n];
    fib[0] = 0;  // changed from 1
    fib[1] = 1;
    for(int i = 2; i<n; i++)
    {
      fib[i]=fib[i-2]+fib[i-1];
      char string[22];
      if(fib[i]/1000000000000000000ULL){
        unsigned long long temp = fib[i] % 1000000000000000000ULL;
        sprintf(string, "%lu", fib[i]/1000000000000000000ULL); 
        Serial.print(string);  
        sprintf(string, "%09lu", temp/1000000000UL); 
        Serial.print(string);  
        sprintf(string, "%09lu", fib[i]%1000000000UL); 
        Serial.println(string);
      } else if(fib[i]/1000000000UL){
        sprintf(string, "%lu", fib[i]/1000000000UL); 
        Serial.print(string);  
        sprintf(string, "%09lu", fib[i]%1000000000UL); 
        Serial.println(string);
      } else {
        sprintf(string, "%lu", fib[i]%1000000000UL); 
        Serial.println(string);
      }
    }
    digitalWrite(ledr,LOW);
    digitalWrite(ledg,HIGH);
    delay(5000);
  }
  digitalWrite(ledr,HIGH);
  digitalWrite(ledg,LOW);
  delay(2000);
}

The problem is that the function 'Serial.println()' doesn't work, because the highest number that 'println()' can describe is 2³².

So, how does that translate to "Serial.println() doesn't work"? It works as advertised. That it does something unexpected when you misuse it doesn't mean that it doesn't work.

I think what he means is println(unsigned long long) doesn't exist. Neither does %ull is sprintf. In order to go larger you have to do two or even three print()'s in order to convey the numbers.

I am Truly Curious as to why a Fib series is of any use to the OP beyond an Extensive math lesson... I found the whole thread truly fascinating from all the posters opinions and facts about math, the real value of floating point numbers and the limitations imposed by using an 8 Bit Chip as it is heroically being used here. My side thought train was 'interesting' to observe... A great lesson well worth reading.

Doc

because the highest number that 'println()' can describe is 2³².

Close, 2³² - 1. :wink:

1niklas1:
Hello my problem is that I tried to write a program that sum up the Fibonacci number. It is a iterative solution. The problem is that the highest number the 47 Fibonacci number is. And I don't know why.

Why are you doing this on an Arduino? For fun? Anyway, this is how to do it...

#include "BigNumber.h"

// function to display a big number and free it afterwards
void printBignum (BigNumber & n)
{
  char * s = n.toString ();
  Serial.println (s);
  free (s);
}  // end of printBignum

void setup()
{
  Serial.begin(115200); 
  BigNumber::begin ();  // initialize library
} // end of setup

void loop()
  {
  int n = 2;
  BigNumber a = 0;
  BigNumber b = 1;
  BigNumber result;
  while (n <= 1000)
    {
    result = a + b; 
    Serial.print (n++);
    Serial.print (" = ");
    printBignum (result);
    a = b;
    b = result;  
    }  // end of while
  }  // end of loop

Output:

3 = 2
4 = 3
5 = 5
6 = 8
7 = 13
8 = 21
9 = 34
10 = 55
11 = 89
12 = 144
13 = 233
14 = 377
15 = 610
16 = 987
17 = 1597
18 = 2584
19 = 4181
20 = 6765
21 = 10946
22 = 17711
23 = 28657
24 = 46368
25 = 75025
26 = 121393
27 = 196418
28 = 317811
29 = 514229
30 = 832040
31 = 1346269
32 = 2178309
33 = 3524578
34 = 5702887
35 = 9227465
36 = 14930352
37 = 24157817
38 = 39088169
39 = 63245986
40 = 102334155
41 = 165580141
42 = 267914296
43 = 433494437
44 = 701408733
45 = 1134903170
46 = 1836311903
47 = 2971215073
48 = 4807526976
49 = 7778742049
50 = 12586269025
51 = 20365011074
52 = 32951280099
53 = 53316291173
54 = 86267571272
55 = 139583862445
56 = 225851433717
57 = 365435296162
58 = 591286729879
59 = 956722026041
60 = 1548008755920
61 = 2504730781961
62 = 4052739537881
63 = 6557470319842
64 = 10610209857723
65 = 17167680177565
66 = 27777890035288
67 = 44945570212853
68 = 72723460248141
69 = 117669030460994
70 = 190392490709135
71 = 308061521170129
72 = 498454011879264
73 = 806515533049393
74 = 1304969544928657
75 = 2111485077978050
76 = 3416454622906707
77 = 5527939700884757
78 = 8944394323791464
79 = 14472334024676221
80 = 23416728348467685
81 = 37889062373143906
82 = 61305790721611591
83 = 99194853094755497
84 = 160500643816367088
85 = 259695496911122585
86 = 420196140727489673
87 = 679891637638612258
88 = 1100087778366101931
89 = 1779979416004714189
90 = 2880067194370816120
91 = 4660046610375530309
92 = 7540113804746346429
93 = 12200160415121876738
94 = 19740274219868223167
95 = 31940434634990099905
96 = 51680708854858323072
97 = 83621143489848422977
98 = 135301852344706746049
99 = 218922995834555169026
100 = 354224848179261915075
101 = 573147844013817084101
102 = 927372692193078999176
103 = 1500520536206896083277
104 = 2427893228399975082453
105 = 3928413764606871165730
106 = 6356306993006846248183
107 = 10284720757613717413913
108 = 16641027750620563662096
109 = 26925748508234281076009
110 = 43566776258854844738105
111 = 70492524767089125814114
112 = 114059301025943970552219
113 = 184551825793033096366333
114 = 298611126818977066918552
115 = 483162952612010163284885
116 = 781774079430987230203437
117 = 1264937032042997393488322
118 = 2046711111473984623691759
119 = 3311648143516982017180081
120 = 5358359254990966640871840
121 = 8670007398507948658051921
122 = 14028366653498915298923761
123 = 22698374052006863956975682
124 = 36726740705505779255899443
125 = 59425114757512643212875125
126 = 96151855463018422468774568
127 = 155576970220531065681649693
128 = 251728825683549488150424261
129 = 407305795904080553832073954
130 = 659034621587630041982498215
131 = 1066340417491710595814572169
132 = 1725375039079340637797070384
133 = 2791715456571051233611642553
134 = 4517090495650391871408712937
135 = 7308805952221443105020355490
136 = 11825896447871834976429068427
137 = 19134702400093278081449423917
138 = 30960598847965113057878492344
139 = 50095301248058391139327916261
140 = 81055900096023504197206408605
141 = 131151201344081895336534324866
142 = 212207101440105399533740733471
143 = 343358302784187294870275058337
144 = 555565404224292694404015791808
145 = 898923707008479989274290850145
146 = 1454489111232772683678306641953
147 = 2353412818241252672952597492098
148 = 3807901929474025356630904134051
149 = 6161314747715278029583501626149
150 = 9969216677189303386214405760200
151 = 16130531424904581415797907386349
152 = 26099748102093884802012313146549
153 = 42230279526998466217810220532898
154 = 68330027629092351019822533679447
155 = 110560307156090817237632754212345
156 = 178890334785183168257455287891792
157 = 289450641941273985495088042104137
158 = 468340976726457153752543329995929
159 = 757791618667731139247631372100066
160 = 1226132595394188293000174702095995
161 = 1983924214061919432247806074196061
162 = 3210056809456107725247980776292056
163 = 5193981023518027157495786850488117
164 = 8404037832974134882743767626780173
165 = 13598018856492162040239554477268290
166 = 22002056689466296922983322104048463
167 = 35600075545958458963222876581316753
168 = 57602132235424755886206198685365216
169 = 93202207781383214849429075266681969
170 = 150804340016807970735635273952047185
171 = 244006547798191185585064349218729154
172 = 394810887814999156320699623170776339
173 = 638817435613190341905763972389505493
174 = 1033628323428189498226463595560281832
175 = 1672445759041379840132227567949787325
176 = 2706074082469569338358691163510069157
177 = 4378519841510949178490918731459856482
178 = 7084593923980518516849609894969925639
179 = 11463113765491467695340528626429782121
180 = 18547707689471986212190138521399707760
...
990 = 353410009178752575339944833520459068284945046358154977604109175253890696634271360121583566110064725510836075851584985143412396868586425109102723291106570618750075392710633321729992106743321640281356794177320
991 = 571829406815633979529643697006273045106845980748991112071673038743714031497887739023091610769764627307772654802298784361803421747114571265690519449915873452164193174293407940201977897716937097604164288130909
992 = 925239415994386554869588530526732113391791027107146089675782213997604728132159099144675176879829352818608730653883769505215818615700996374793242741022444070914268567004041261931970004460258737885521082308229
993 = 1497068822810020534399232227533005158498637007856137201747455252741318759630046838167766787649593980126381385456182553867019240362815567640483762190938317523078461741297449202133947902177195835489685370439138
994 = 2422308238804407089268820758059737271890428034963283291423237466738923487762205937312441964529423332944990116110066323372235058978516564015277004931960761593992730308301490464065917906637454573375206452747367
995 = 3919377061614427623668052985592742430389065042819420493170692719480242247392252775480208752179017313071371501566248877239254299341332131655760767122899079117071192049598939666199865808814650408864891823186505
996 = 6341685300418834712936873743652479702279493077782703784593930186219165735154458712792650716708440646016361617676315200611489358319848695671037772054859840711063922357900430130265783715452104982240098275933872
997 = 10261062362033262336604926729245222132668558120602124277764622905699407982546711488272859468887457959087733119242564077850743657661180827326798539177758919828135114407499369796465649524266755391104990099120377
998 = 16602747662452097049541800472897701834948051198384828062358553091918573717701170201065510185595898605104094736918879278462233015981029522997836311232618760539199036765399799926731433239718860373345088375054249
999 = 26863810024485359386146727202142923967616609318986952340123175997617981700247881689338369654483356564191827856161443356312976673642210350324634850410377680367334151172899169723197082763985615764450078474174626
1000 = 43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875

Arrays are a bad idea. You don't have that much memory.

Anyway, I got to 1000. (You can go higher, but not all that much). Hope that helps.

The problem is that the function 'Serial.println()' doesn't work, because the highest number that 'println()' can describe is 2³².

This is an 8-bit processor. Give it a bit of a break. To output long longs would make programs somewhat larger.

This is an 8-bit processor. Give it a bit of a break. To output long longs would make programs somewhat larger.

No larger than doing it manually.

The issue is that the println() method is overloaded. It has version that print ints, bytes, chars, floats, doubles, etc. It does not have a version that prints long longs.

Adding such a version would be simple.

Bitching that it does not exist benefits no one.

Very well... print and println with an "unsigned long long", and "long long" variant.

Print.zip (2.85 KB)