Ultra Frosty Posted September 13, 2004 Share Posted September 13, 2004 I am not a math wiz, but I need someone to write a program in C that would print out this formula: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ....... The formula is the last 2 numbers in the list are added to make the next number. 1 + 1 = 2 1 + 2 = 3 2 + 3 = 5 ... I don't really know how I can do this myself. I understand what I need to do with the loops and arrays, but I can't get it to work at all. Link to comment Share on other sites More sharing options...
0 WigglesTheFish Posted September 13, 2004 Share Posted September 13, 2004 this for homework? Link to comment Share on other sites More sharing options...
0 Ultra Frosty Posted September 13, 2004 Author Share Posted September 13, 2004 No. I need it for implimenting in a python script. Link to comment Share on other sites More sharing options...
0 hdood Posted September 13, 2004 Share Posted September 13, 2004 (edited) I think the code pretty much speaks for itself... #include <stdio.h> #define START 1 #define END 100 int main(void) { int a, b, c; a = b = START; printf("%li", b); while(b < END) { c = a; b = a+b; a = b; b = c; printf(", %li", b); } return 0; } And by the way.. a google search for "fibonacci series" is probably going to give you more help if you need it. Edited September 13, 2004 by hdood Link to comment Share on other sites More sharing options...
0 _kane81 Posted September 13, 2004 Share Posted September 13, 2004 No. I need it for implimenting in a python script. yeah right...... :whistle: Link to comment Share on other sites More sharing options...
0 Kestrel Posted September 13, 2004 Share Posted September 13, 2004 So, pray tell, what are you going to use a Fibonacci Sequence for inside this Python script of yours? Fib Sequences are classic first year CS... Here's a recursive method for generating it: int fib(int n) { return (n < 2) ? 1 : (fib(n - 1) + fib(n - 2)); } Link to comment Share on other sites More sharing options...
0 Ultra Frosty Posted September 13, 2004 Author Share Posted September 13, 2004 (edited) Acually, here is the exact code I needed: #!C:/Python/python.exe START = 1 END = 16384 a = START b = START i = 1 while b < END: c = a b = a + b a = b b = c print i, b, "=", hex(b) i = i + 1 Edited September 13, 2004 by b0b Link to comment Share on other sites More sharing options...
Question
Ultra Frosty
I am not a math wiz, but I need someone to write a program in C that would print out this formula:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55 .......
The formula is the last 2 numbers in the list are added to make the next number.
1 + 1 = 2
1 + 2 = 3
2 + 3 = 5
...
I don't really know how I can do this myself. I understand what I need to do with the loops and arrays, but I can't get it to work at all.
Link to comment
Share on other sites
6 answers to this question
Recommended Posts