Fabonicseries Program required

H

hiwa

sirusha said:
Fibonacci series is:
1 2 3 5 8 13 21 34 55 ...... (N(n) = N(n-1)+N(n-2))
You could write Java code yourself.
Ask help from your kindergarten brother.
 
J

JanTheKing

Sirusha,

I have written the following method to generate the first 100 numbers
in the fibonacci series. As Hiwa says, writing a Fibonacci program
should be fairly simple and request you to try your own approach.

void printFibonacciSeries()
{
int x=0, y =0, z=0;

for (int i = 0; i < 100; i++)
{
z=x+y;
System.out.print(z+" ");
y=x;
x=z;
if(x==0)
{
x=1;
}
}
}
 
F

Furious George

No problem.

fibonacci ( )
{
while(true)
{
System.out.println("I am a brainless twit.");
}
}
 
J

John W. Kennedy

Furious said:
No problem.

fibonacci ( )
{
while(true)
{
System.out.println("I am a brainless twit.");
}
}

Come now. Everyone knows that the best way is:

static int fibonacci (int i) {
if (i <= 1)
return 1;
else
return fibonacci (i - 1) + fibonacci (i - 2);
}
 
T

Tom Forsmo

JanTheKing said:
Sirusha,

I have written the following method to generate the first 100 numbers
in the fibonacci series. As Hiwa says, writing a Fibonacci program
should be fairly simple and request you to try your own approach.

Don't give code to people demanding we do their work. its better they
try to understand it themselves. Its coursework, so he should learn it
himself.

tom
 
F

Furious George

John said:
Come now. Everyone knows that the best way is:

static int fibonacci (int i) {
if (i <= 1)
return 1;
else
return fibonacci (i - 1) + fibonacci (i - 2);
}

The OP didn't. Now you have deprived him/her of the pleasure of doing
his/her own homework.
 
J

John W. Kennedy

Furious said:
The OP didn't. Now you have deprived him/her of the pleasure of doing
his/her own homework.

I'd like to see the teacher's face when he hands it in.
 
F

Furious George

John said:
I'd like to see the teacher's face when he hands it in.

I see you are a vicious and cruel bastard. It is doubtless you torture
puppies in your spare time.

You gave the OP a program that was so close to being correct that even
I did not see the error at first. You expected the OP to blindly copy
your program and lose all face and all marks when the teacher exposes
the OP's stupidity.

You are fiendishly clever. Your program gives the correct fibonacci
sequence for the first 44 numbers and then starting with the 45th
number it produces nothing but incorrect results. Obviously, you
anticipated that the OP would not check much beyond the 10th number.

Hint to OP: To save face, figure out why this heartless bastard's
program fails for numbers larger than 44.
 
E

Eric Sosman

Martin said:
Teachers throughout the whole world use this one to explain recursion :D

That has always puzzled me, because (as John Kennedy's code
above shows) the problem is poorly suited to a recursive solution.
The other common example is computing factorials, and recursion is
a poor choice of method for that one, too. Is it any wonder that
understanding of recursion proves difficult, when the teachers
offer such bad examples?
 
J

JanTheKing

Hey Furious George,

Your mail was really hilarious. Thanks man.

I had knowingly coded the program like that - so that Sirusha puts some
original effort.

HINT to OP: Using a BigDecimal would solve the issue.

Cheers,
Jan The King
 
M

Mike

One of the better examples given to me in school were the recursive
solutions to the various sort methods bubble sort, breadth first, etc.
The mathematical ones were just to get us started I supposed. But it
works.
 
S

Simon Brooke

Eric Sosman said:
That has always puzzled me, because (as John Kennedy's code
above shows) the problem is poorly suited to a recursive solution.
The other common example is computing factorials, and recursion is
a poor choice of method for that one, too. Is it any wonder that
understanding of recursion proves difficult, when the teachers
offer such bad examples?

On the contrary, it is a beautifully simple piece of recursion. In a proper
grown up language

(defun fibonacci (n)
(assert (integerp n) nil "N must be an integer")
(cond ((<= n 1) 1)
(t (+ (fibonacci (- n 1))
(fibonacci (- n 2))))))

You cannot come up with a non-recursive definition of the Fibonacci series
which is simpler or more expressive than that. Computational efficiency?
Pah!
 
K

Karl Uppiano

JanTheKing said:
Hey Furious George,

Your mail was really hilarious. Thanks man.

I had knowingly coded the program like that - so that Sirusha puts some
original effort.

HINT to OP: Using a BigDecimal would solve the issue.

And if you want to compute f(100) in less than 5 minutes, consider how you
might use a Map to improve efficiency.
 
D

Daniel Dyer

On the contrary, it is a beautifully simple piece of recursion. In a
proper
grown up language

(defun fibonacci (n)
(assert (integerp n) nil "N must be an integer")
(cond ((<= n 1) 1)
(t (+ (fibonacci (- n 1))
(fibonacci (- n 2))))))

You cannot come up with a non-recursive definition of the Fibonacci
series
which is simpler or more expressive than that. Computational efficiency?
Pah!

I prefer the Haskell/Miranda formulation (not non-recursive though):

fib 0 = 0
fib 1 = 1
fib (n + 2) = fib (n) + fib (n + 1)

I found this page of benchmarks for Fibonacci numbers in different
languages using different algorithms:

http://www.cubbi.org/serious/fibonacci.html

It seems that all the links to source code are broken, so the OP is still
going to have to write their own code.

Dan.
 
D

Daniel Pitts

Karl said:
And if you want to compute f(100) in less than 5 minutes, consider how you
might use a Map to improve efficiency.

Don't you think memoization is too advanced a topic for the OP?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top