Help With Iterative Fibonacci Fuction
Author Message
Help With Iterative Fibonacci Fuction

To Anyone That Can Help Me:

I am trying to write a program in Java which displays the Fibonacci value
for any integer entered in by the user.  The program must call on an
iterative function.  (I've already done the recursive function, which was
much easier!)

The basic algorithm for the problem should be
fold1 = 1;
fold2 = 1;
fnew = fold1 + fold2;

After that, I am to discard fold2, which is no longer needed, and set
fold2 to fold1 and fold1 to fnew.  Then repeat computing fnew for an
appropriate number of times.

I've got the first part; I just have no clue how to do the second part
(everything after "Discard fold2..." etc.)  I am a beginner and would
appreciate a very basic explanation by anyone who is willing.  Could

So far this is what I have:

public class ItFunction {
public static void main (String args[]) {

System.out.println("The Fibonacci Value is: " + fibonacci(number));
}

public static int fibonacci(int number) {

while (number != 0) {

int fold1 = 1;
int fold2 = 1;
int fnew = fold1 + fold2;

//fold2 = fold1;
//fold1 = fnew;
}
return number;
}

Quote:
}

Thank you in advance to anyone who responds.  Please either respond to the

J. Brunetti
- "Failure is not an option.  It is a privilege reserved only for those who try."  Anonymous -

Thu, 18 Apr 2002 02:00:00 GMT
Help With Iterative Fibonacci Fuction
Where you're supposed to discard a value, that just means you're not
using it anymore; the code you had commented out should do what you
want it to.

When you say fold2=fold1, that's where you're discarding fold2; you're
losing the value that was there and setting fold2 to be the same number
as fold1, but that's not a problem because you don't need it anymore.
Then when you say fold1=fnew, you're setting fold2 to fnew, which is
what you want, and then you can give fnew a new value.

There are a few problems with your code, though:
1. fold1 and fold2 should be set to 1 before you enter the loop, since
you use the values you gave them the last time through the loop and not
their initial values of 1 to calculate your next fnew.

2. You're returning number, which is zero by the time you get there;
you should be returning your last fnew, which is the nth Fibonacci
number that you just calculated.  Because of this, fnew should be
declared outside the loop so that it's still in scope when you try to
return it after you exit the loop.

I'm not going to write your code for you, but it should go something
like this:

initialize variables
while(number!=0)
calculate new fnew
shift values appropriately
return last fnew

Hope this helps

dave

Quote:

>To Anyone That Can Help Me:

>I am trying to write a program in Java which displays the Fibonacci value
>for any integer entered in by the user.  The program must call on an
>iterative function.  (I've already done the recursive function, which was
>much easier!)

>The basic algorithm for the problem should be
>fold1 = 1;
>fold2 = 1;
>fnew = fold1 + fold2;

>After that, I am to discard fold2, which is no longer needed, and set
>fold2 to fold1 and fold1 to fnew.  Then repeat computing fnew for an
>appropriate number of times.

>I've got the first part; I just have no clue how to do the second part
>(everything after "Discard fold2..." etc.)  I am a beginner and would
>appreciate a very basic explanation by anyone who is willing.  Could

>So far this is what I have:

>public class ItFunction {
>        public static void main (String args[]) {

>        System.out.println("Please Enter An Integer: ");
>        System.out.println("The Fibonacci Value is: " + fibonacci(number));
>        }

>public static int fibonacci(int number) {

>        while (number != 0) {

>                int fold1 = 1;
>                int fold2 = 1;
>                int fnew = fold1 + fold2;

>                //fold2 = fold1;
>                //fold1 = fnew;
>                }
>                return number;
>        }
>}

>Thank you in advance to anyone who responds.  Please either respond to the

>J. Brunetti
>- "Failure is not an option.  It is a privilege reserved only for those who try."  Anonymous -

Fri, 19 Apr 2002 03:00:00 GMT
Help With Iterative Fibonacci Fuction
Whats a Fibonacci Fuction ?

Fri, 19 Apr 2002 03:00:00 GMT
Help With Iterative Fibonacci Fuction

Quote:
>Whats a Fibonacci Fuction ?

Microsoft Encarta Encyclopaedia says ...

========================================================================
Fibonacci Series, in mathematics; series of numbers in which each member
is the sum of the two preceding numbers. For example, a series beginning
0, 1 ... continues as 1, 2, 3, 5, 8, 13, 21, and so forth. The series
was discovered by the Italian mathematician Leonardo Fibonacci (circa
1170-c. 1240), also called Leonardo of Pisa. Fibonacci numbers have many
interesting properties and are widely used in mathematics. Natural
patterns, such as the spiral growth of leaves on some trees, often
exhibit the Fibonacci series.
========================================================================

NOTE that most mathematicians recognise the first two terms as 1 and 1,
not 0 and 1 as quoted above.

The reference to leaves is the fact that if you look at the growth of
leaves on certain types of trees, or petals on most flowers, you find
that the numbers follow a Fibonacci sequence as they spiral outwards.

Good eh ?

--
Alan Silver

Fri, 19 Apr 2002 03:00:00 GMT
Help With Iterative Fibonacci Fuction
Your iterative function is not right, it should be called by itself
and therefore it must state clearly, at the beginning of the
function, the condition to leave the iteration. This class
'Fibonacci' shows you how an iterative function works. I have
mutilated it enough to make it impossible to pass it as your
homework assignment :-)

import java.io.*;

public class Fibonacci
{

//constructor
Fibonacci()
{
System.out.println("Fibonacci(7) = " + getFibonacci(7));
}

//getFibonacci
int getFibonacci(int a)
{
if (a < 1)
{
System.out.println("ERROR: getFibonacci(" + a + ") invalid
input");
return -1;
}
if (a == 1) return 1;
if (a == 2) return 1;
return getFibonacci(a - 1) + getFibonacci(a - 2);
}

//main
public static void main (String args[])
{
new Fibonacci();

Quote:
}}

Succes, Martijn

Quote:
> To Anyone That Can Help Me:

> I am trying to write a program in Java which displays the
Fibonacci value
> for any integer entered in by the user.  The program must call on
an
> iterative function.  (I've already done the recursive function,
which was
> much easier!)

> The basic algorithm for the problem should be
> fold1 = 1;
> fold2 = 1;
> fnew = fold1 + fold2;

> After that, I am to discard fold2, which is no longer needed, and
set
> fold2 to fold1 and fold1 to fnew.  Then repeat computing fnew for
an
> appropriate number of times.

> I've got the first part; I just have no clue how to do the second
part
> (everything after "Discard fold2..." etc.)  I am a beginner and
would
> appreciate a very basic explanation by anyone who is willing.
Could

> So far this is what I have:

> public class ItFunction {
>         public static void main (String args[]) {

>         System.out.println("Please Enter An Integer: ");
>         System.out.println("The Fibonacci Value is: " +
fibonacci(number));
>         }

> public static int fibonacci(int number) {

>         while (number != 0) {

>                 int fold1 = 1;
>                 int fold2 = 1;
>                 int fnew = fold1 + fold2;

>                 //fold2 = fold1;
>                 //fold1 = fnew;
>                 }
>                 return number;
>         }
> }

respond to the

> J. Brunetti
> - "Failure is not an option.  It is a privilege reserved only for

those who try."  Anonymous -

- Show quoted text -

Fri, 19 Apr 2002 03:00:00 GMT
Help With Iterative Fibonacci Fuction

Quote:

> I am trying to write a program in Java which displays the Fibonacci value
> for any integer entered in by the user.  The program must call on an
> iterative function.  (I've already done the recursive function, which was
> much easier!)

I would recommend that you talk to your tutor for your introductory
programming class and ask for their help.

--

`-_-'   Ar rug t barrg ar do mhactre inniu?
'U`    <pfy> Sigs with pfy's markov chain.
-- "pfy", a markov chain IRC bot.

Fri, 19 Apr 2002 03:00:00 GMT
Help With Iterative Fibonacci Fuction

Quote:

>Your iterative function is not right, it should be called by itself
>and therefore it must state clearly, at the beginning of the
>function, the condition to leave the iteration. This class
>'Fibonacci' shows you how an iterative function works. I have
>mutilated it enough to make it impossible to pass it as your
>homework assignment :-)

Your code is correct, but either you didn't read the original post very
carefully or you need to go back over the difference between iteration
and recursion:

Quote:
>> I am trying to write a program in Java which displays the Fibonacci
>> value for any integer entered in by the user.  The program must call on
>> an iterative function.  (I've already done the recursive function,

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Quote:
>> which was much easier!)
>import java.io.*;

>public class Fibonacci
>{

> //constructor
> Fibonacci()
> {
>  System.out.println("Fibonacci(7) = " + getFibonacci(7));
> }

> //getFibonacci
> int getFibonacci(int a)
> {
>  if (a < 1)
>  {
>   System.out.println("ERROR: getFibonacci(" + a + ") invalid
>input");
>   return -1;
>  }
>  if (a == 1) return 1;
>  if (a == 2) return 1;
>  return getFibonacci(a - 1) + getFibonacci(a - 2);

This calls itself; therefore by definition it's recursive and isn't any
help to somebody looking for an interative solution.

- Show quoted text -

Quote:
> }

> //main
> public static void main (String args[])
> {
>  new Fibonacci();
>}}

>Succes, Martijn

Fri, 19 Apr 2002 03:00:00 GMT
Help With Iterative Fibonacci Fuction

| Whats a Fibonacci Fuction ?

A Fibonacci series is this - 0, 1, 1, 2, 3, 5, 8, 13, 21, ...

And there's a simple algorithm to generate the series.

int a = 0;
int b = 1;

int stopAt = 1000; /* replace this with the number where you want to stop */

while(b < stopAt)
{
System.out.println(a);
System.out.println(b);
a = a + b;
b = a + b;

Quote:
}

OUTPUT:
0
1
1
2
3
5
8
13
...
...
...

--

- Manish Jethani.

My personal web site -
http://mjweb.hypermart.net/personal/

Fri, 26 Apr 2002 03:00:00 GMT
Help With Iterative Fibonacci Fuction
On Mon, 8 Nov 1999 14:26:53 -0800,

Quote:
>int a = 0;
>int b = 1;

>int stopAt = 1000; /* replace this with the number where you want to stop */

>while(b < stopAt)
>{
>    System.out.println(a);
>    System.out.println(b);
>    a = a + b;
>    b = a + b;
>}

Ooooh, fun! Is it going to be another one of those "let's see how my language
can do it?" sessions?

I'm willing to contribute a python example (copied from my head which copied
from the tutorial):

stop_at=int(argv[1])
a, b = 0, 1
print a,
while b<stop_at:
print b,
a, b = b, a+b
print

Notice:
1. Mine is shorter
2. Mine is a complete working script. No need to wrap in a class.
3. Mine uses the command line arguments.

Fri, 26 Apr 2002 03:00:00 GMT
Help With Iterative Fibonacci Fuction

Quote:

> On Mon, 8 Nov 1999 14:26:53 -0800,
> <snipped discussion about fibonacci series>
> >int a = 0;
> >int b = 1;

> >int stopAt = 1000; /* replace this with the number where you want to stop */

> >while(b < stopAt)
> >{
> >    System.out.println(a);
> >    System.out.println(b);
> >    a = a + b;
> >    b = a + b;
> >}

> Ooooh, fun! Is it going to be another one of those "let's see how my language
> can do it?" sessions?

> I'm willing to contribute a Python example (copied from my head which copied
> from the tutorial):

> stop_at=int(argv[1])
> a, b = 0, 1
> print a,
> while b<stop_at:
>         print b,
>         a, b = b, a+b
> print

> Notice:
> 1. Mine is shorter
> 2. Mine is a complete working script. No need to wrap in a class.
> 3. Mine uses the command line arguments.

\$a = 0; \$b = 1; while (\$b < \$ARGV[0]) { print (\$a ."\n" . \$b . "\n"); \$a
= \$a+\$b; \$b = \$a + \$b; }

No "use strict" or shebang, but hey! One line perl version.  And it uses
the command line too!

Alright, it's cheating.  It should be on more than one line.  :)

JimC

Fri, 26 Apr 2002 03:00:00 GMT
Help With Iterative Fibonacci Fuction

Quote:
> \$a = 0; \$b = 1; while (\$b < \$ARGV[0]) { print (\$a ."\n" . \$b . "\n"); \$a
> = \$a+\$b; \$b = \$a + \$b; }

Another Perl version:

for (\$a=0,\$b=1; \$b<\$ARGV[0]; \$a+=\$b,\$b+=\$a) { print "\$a\n\$b\n"; }

And back to java:

class Fibo {
public static void main(String[] args) {
int stopAt = args.length > 0 ? Integer.parseInt(args[0],10) : 0;
for (int a=0, b=1; b<stopAt; a+=b, b+=a) {
System.out.println(a);
System.out.println(b);
}
}

Quote:
}

Is it not true that the ratio between two consecutive Fibonacci numbers
converges towards what is known as the "golden section" (0.618...)?

--
Aksel

Sat, 27 Apr 2002 03:00:00 GMT
Help With Iterative Fibonacci Fuction

Quote:

> On Mon, 8 Nov 1999 14:26:53 -0800,
> <snipped discussion about fibonacci series>
> >int a = 0;
> >int b = 1;

> >int stopAt = 1000; /* replace this with the number where you want to stop */

> >while(b < stopAt)
> >{
> >    System.out.println(a);
> >    System.out.println(b);
> >    a = a + b;
> >    b = a + b;
> >}

> Ooooh, fun! Is it going to be another one of those "let's see how my language
> can do it?" sessions?

> I'm willing to contribute a Python example (copied from my head which copied
> from the tutorial):

> stop_at=int(argv[1])
> a, b = 0, 1
> print a,
> while b<stop_at:
>    print b,
>    a, b = b, a+b
> print

> Notice:
> 1. Mine is shorter
> 2. Mine is a complete working script. No need to wrap in a class.
> 3. Mine uses the command line arguments.

Everybody knows Perl is better for you.  I didn't have time to
obfuscate, but here's a good start (note that "(\$a,\$b)=(0,0)" could be
replaced by "\$b++;", to make it easier to understand).

Notice:
1. Mine is shorter (could be made shorter by moving the calculation
out of the print statement, but that'd be too easy to read).
2. Mine is a complete working script (yes, Perl has classes).
3. Mine uses the command line arguments.
4. Mine can't be submitted as a homework solution.

Followups set.

"Perl -- the programming language which discourages cheating!\n"
--
Ariel Scolnicov

Sat, 27 Apr 2002 03:00:00 GMT

 Page 1 of 2 [ 26 post ] Go to page: [1] [2]

Relevant Pages