wu :: forums (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi)
riddles >> cs >> Fibonacci Serious in reverse Order
(Message started by: tuxilogy on Jun 10th, 2006, 3:41am)

Title: Fibonacci Serious in reverse Order
Post by tuxilogy on Jun 10th, 2006, 3:41am
Hi can any one suggest me the algo regarding "Printing of the Fibonacci in reverse order "
Thanks in advance
TUX

Title: Re: Fibonacci Serious in reverse Order
Post by Barukh on Jun 10th, 2006, 6:04am
What do you mean by "in reverse order"? Is a maximal element of the sequence given?

Title: Re: Fibonacci Serious in reverse Order
Post by tuxilogy on Jun 10th, 2006, 6:49am
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

Title: Re: Fibonacci Serious in reverse Order
Post by towr on Jun 10th, 2006, 9:40am
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.

Title: Re: Fibonacci Serious in reverse Order
Post by Grimbal on Jun 11th, 2006, 1:19pm
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));
}

Title: Re: Fibonacci Serious in reverse Order
Post by towr on Jun 12th, 2006, 12:55am
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 ;)

Title: Re: Fibonacci Serious in reverse Order
Post by Grimbal on Jun 12th, 2006, 1:56am

on 06/12/06 at 00:55:52, 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 06/12/06 at 00:55:52, 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");


Title: Re: Fibonacci Serious in reverse Order
Post by towr on Jun 12th, 2006, 2:24am

on 06/12/06 at 01:56:39, 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");
?

Title: Re: Fibonacci Serious in reverse Order
Post by Grimbal on Jun 12th, 2006, 2:49am
Absolutely, a classical school.  How could I have missed that one?   ;D



Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board