Author |
Topic: Fibonacci Serious in reverse Order (Read 1983 times) |
|
tuxilogy
Newbie
Posts: 45
|
|
Fibonacci Serious in reverse Order
« on: Jun 10th, 2006, 3:41am » |
Quote Modify
|
Hi can any one suggest me the algo regarding "Printing of the Fibonacci in reverse order " Thanks in advance TUX
|
|
IP Logged |
|
|
|
Barukh
Uberpuzzler
Gender:
Posts: 2276
|
|
Re: Fibonacci Serious in reverse Order
« Reply #1 on: Jun 10th, 2006, 6:04am » |
Quote Modify
|
What do you mean by "in reverse order"? Is a maximal element of the sequence given?
|
|
IP Logged |
|
|
|
tuxilogy
Newbie
Posts: 45
|
|
Re: Fibonacci Serious in reverse Order
« Reply #2 on: Jun 10th, 2006, 6:49am » |
Quote Modify
|
1,1,2,3,5 is the Fibbonacci series for n = 5, reverse means given a number say n, one has to print the series in thsi was ----> " 5, 3, 2, 1,1".. Tux
|
|
IP Logged |
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: Fibonacci Serious in reverse Order
« Reply #3 on: Jun 10th, 2006, 9:40am » |
Quote Modify
|
I'd use recurrence calculate the Kth fibonacci number (ideally, given the (K-1)th and (K-2)th ), call the function again for K+1 if K<N, print the Kth number. But of course you could also use a stack.
|
|
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
Grimbal
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 7527
|
|
Re: Fibonacci Serious in reverse Order
« Reply #4 on: Jun 11th, 2006, 1:19pm » |
Quote Modify
|
Recurence?? Stack?? Surely you're joking, Mr. Towr! void fiborev(int n){ int a=0, b=1; while( n-->0 ) a = (b+=a)-a; while( a ) printf(a?"%d,":"%d\n", b -= (a=b-a)); }
|
|
IP Logged |
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: Fibonacci Serious in reverse Order
« Reply #5 on: Jun 12th, 2006, 12:55am » |
Quote Modify
|
Well sure, you could also do that, if you want to calculate each value twice... Recurrence and stacks is a more general way to print things in reverse though.. So that's why it was the first thing that came to mind. You should see me print 1 to N in reverse
|
|
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
Grimbal
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 7527
|
|
Re: Fibonacci Serious in reverse Order
« Reply #6 on: Jun 12th, 2006, 1:56am » |
Quote Modify
|
on Jun 12th, 2006, 12:55am, towr wrote:Well sure, you could also do that, if you want to calculate each value twice... |
| 4 times actually. I fixed that below. Should be 4 times faster. fiborev(int n){ while( n-- ) printf(n?"%.0lf,":"%.0lf\n", exp(log((sqrt(5)+1)/2)*(n+1))/sqrt(5)); } on Jun 12th, 2006, 12:55am, towr wrote:You should see me print 1 to N in reverse |
| There are 2 schools: printf("N ot 1\n"); and printf("1 to N in reverse\n");
|
|
IP Logged |
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: Fibonacci Serious in reverse Order
« Reply #7 on: Jun 12th, 2006, 2:24am » |
Quote Modify
|
on Jun 12th, 2006, 1:56am, Grimbal wrote:There are 2 schools: printf("N ot 1\n"); and printf("1 to N in reverse\n"); |
| Just two? Why not printf("reve1 to Nrse\n"); ?
|
|
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
Grimbal
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 7527
|
|
Re: Fibonacci Serious in reverse Order
« Reply #8 on: Jun 12th, 2006, 2:49am » |
Quote Modify
|
Absolutely, a classical school. How could I have missed that one?
|
|
IP Logged |
|
|
|
|