The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

My high school assignment

Posted by Matt Gerrans on October 26, 2001 at 2:03 AM

Yee gads! Soon as I posted the note, I noticed that my little fibonacci calculator was not calculating fibonacci, but instead gerronacci numbers (which is an increasing sequence of numbers that future astrologers and numerologists will find to be uncanny predictors of personality traits, foibles and tendencies, based on the day of month and hour of birth combined with the biorythm of the victi-uh, client). This is probably punishment for my slight of ja(va)rule (sorry, but my sensibilities were somewhat inflamed by his racist comment about Chin Loong).

Anyway, I think the following will give a closer approximation of actual Fibonacci numbers (or else the Fibo-nazis out there will be quick to let me know otherwise, I'm sure).


public static long getNthFibonacci( int n )
{
if( n < 1 || n > END )
throw new IndexOutOfBoundsException();
else if( n == 1 )
return 0;

long fibonacci = 1, current = 1, prev = 0;

for( long i = 2; i < n; i++ )
{
fibonacci = prev + current;
prev = current;
current = fibonacci;
}

return fibonacci;
}

- mfg



Replies:
  • : ) Chin Loong October 26, 2001 at 4:12 AM (0)

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us