Confused??? Is one of these codes more efficient than the others?

The question:

In terms of the number of mathematical calculations required, is your new implementation more or less efficient that the ones you used in Chapter 4?

:confused: to my mind there does not seem to be a great deal of difference in the amount of calculations being done... Have i done this wrong?

the chapter four codes print more stuff on the screen. but aren't the calculations virtually the same. is the efficiency anything to do with whats being put on the screen? or is it like the question suggests to do with the calculations being done?

heres the code i did for chapter 4 exercise 9:

Code:

`import acm.program.*;`

public class FibonacciSeq extends ConsoleProgram {

private static final int LAST = 15;

public void run(){

int f0 = 0;

int f1 = 1;

for(int i = 0 ; i <= LAST; i++){

println("F"+ i +" : "+f0);

f0+=f1;

i++;

println("F"+ i +" : "+f1);

f1+=f0;

}

}

}

and this is Exercise 10 similar but with a while loop

Code:

`import acm.program.*;`

public class FibonacciSeqWhile extends ConsoleProgram {

private static final int LAST = 10000;

public void run(){

int f0 = 0;

int f1 = 1;

int i = 0;

while(true){

if ( f0 > LAST) break;

println ("F"+ i +" : "+f0);

f0 += f1;

i++;

if (f1 > LAST) break;

println ("F"+ i +" : "+f1);

f1 += f0;

i++;

}

}

}

The Task for chapter 5 is:

Rewrite the fibonnacci programs from chapter 4, changing the implementation so that your program calls a method to calculate the nth Fibonacci number.

here is my code:

Code:

`import acm.program.*;`

public class FibonacciMethod extends ConsoleProgram {

public void run(){

enterVariable();

}

private void enterVariable() {

int n = readInt(" Enter an integer value ");

println ("F"+n+" : "+ fibonacci(0, 1, n));

}

private int fibonacci(int f0, int f1, int n) {

for(int i=0; i <= n; i++){

if ( i == n ) return f1;

f0 += f1;

i++;

if ( i == n ) return f0;

f1 += f0;

}

return n;

}

}

So which is most efficient? have i missed something or done it wrong?

EDIT:

or is the answer that neither is more efficient it is simply about style??

.. i think i need a beer :confused:

OKay the code is wrong but only slightly

the index has to start at 1 otherwise, if 1 is entered it gives a result F0 = 1 and the sequence is wrong,

edit:

scrap that actually i just reverse my return statements

i added a while loop to make it easier to test.

i now have this but still no closer to anwering the question unless there is another way.

Code:

`import acm.program.*;`

public class FibonacciMethod extends ConsoleProgram {

public void run(){

enterVariable();

}

private void enterVariable() {

while(true){

int n = readInt(" Enter an integer value ");

println ("F"+n+" : "+ fibonacci(0, 1, n));

}

}

private int fibonacci(int f0, int f1, int n) {

for(int i=0; i <=n; i++){

[COLOR="red"]if (i==n)return f0;[/COLOR]

f0+=f1;

i++;

[COLOR="Red"]if (i==n)return f1;[/COLOR]

f1+=f0;

}

return n;

}

}

TA DA! even though i see Jos beat me to it :-)

TA DA! even though i see Jos beat me to it :-)

i must admit i found this really tough, i had to go and read about fibbonacci sequence and recurrence relation on wikipedia and math sites and stuff,,

and my brain hurts. all that reading to work out just a few lines!! AND ITS NOT EVEN MORE EFFICIENT

edit:

but some times recursion is more efficient see this http://www.java-forums.org/new-java/...recursion.html

Code:

` private void enterVariable() {`

while(true){

int n = readInt (" Enter an integer value ");

println ("F"+n+" : "+ fibbonaci ( n ));

}

}

private int fibbonaci (int n){

if (n == 1 || n == 0)

return n;

else

return (fibbonaci (n - 1) + fibbonaci (n - 2));

}

}

it kind of does it backwards meaning its not very efficient at all for large numbers.. jos explains it better...

but the point here is iteration (either for or while) is usually better.

and taking Erangas point from earlier the choice depends on whether you know how many iterations or untill a condition is met.

Quote:

You forgot to define variable 'spareN';

:confused::confused:

Code:

`private int fibonacci (int lastN, int nextN, int n){`

for (int i = 0; i <= n; ++i){

[COLOR="Red"] int spareN = nextN + lastN;[/COLOR]

lastN = nextN;

nextN = spareN;

}

return nextN;

}

}

do you mean define or initialise?