Jump to content

Some Professional eyes to look over my code for "The Fibonnaci series"

- - - - -

  • Please log in to reply
21 replies to this topic

#1
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Hi All

I am Looking for you guys to look over my code for "The Fibonnaci series" and tell me what you think, have i added to much code and do i need the if statement inside the for loop?

Any newbies want to use it work away!

Here is the Question and the code :



/* Question: The Fibonnaci series is as follows;

	0, 1, 1, 2, 3, 5, 8, 13, 21, 34.......

	After the first 2 numbers, each number is the sum of the two previous numbers.

	Write a program to compute the nth term in the series. Have your program to print

	the series to the nth term. */

// Gman

// 25/11/2011


public class FibonacciQ5Part2

	{

	

			public static void main(String[] args)

			{

				// declare variable

					int termRequired = 11;

					int nthTerm = 0;

					int twoPrevious = 0;

					int onePrevious = 1;

					

					// calculate fibonnaci						

					for (int i = 2; i < termRequired; i++)

					{

						// if statement so i can get the zero and other 1 to print on screen

						if (nthTerm < 2)

						{

							System.out.println("Current:\t" + nthTerm + " ");

						}

							

							nthTerm = onePrevious + twoPrevious;

							System.out.println("Current:\t" + nthTerm + " ");

							twoPrevious = onePrevious;

							onePrevious = nthTerm;

						

					}

						// output / display on screen

						System.out.println("Position " + termRequired + " has a value of " + nthTerm);


			

			}

	

	}

	



Don't forget i am only new to java and only learned for loop this week! Go easy!!

Gman

Edited by Gman, 25 November 2011 - 02:04 PM.
spell check :)


#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
I don't know Java, but I can tell what you're trying to do; I think the algorithm, overall, looks good, save for nth terms for which n is less than or equal to 2.

My suggestion would be to, instead of doing 'for ... if ... ' , have 'if ... for ... ' .

I mean, something like this:
if (termRequired > 2){ 

  [I]... do the for loop you're doing ... [/I]

} else { 

  if (termRequired == 0) nthTerm= 0;  

  else nthTerm= 1; 

} 


EDIT: Oh, I forgot about the thing that you also have to display what number you're currently at; well, I think you could add that in too.

Edited by RhetoricalRuvim, 25 November 2011 - 04:26 PM.


#3
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Looks ok!
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Generally, the output should be: 1, 1, 2, 3, 5, 8, 13, ...

Did I miss something, or are you adding a few leading terms?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts

WingedPanther said:

Generally, the output should be: 1, 1, 2, 3, 5, 8, 13, ...

Did I miss something, or are you adding a few leading terms?
I prints that, but with a leading zero.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Shortest possible way:

        System.out.print("0, 1, ");

        for(int i=0, j=1, sum=i+j; i<50 ; i=j, j=sum, sum = i+j){

            System.out.print(sum + ", ");

        } 

How less readable that is, I leave up to you. I don't think it's that bad.

#7
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Thanks RhetoricalRuvim , I will try what you said about if outside loop.

---------- Post added at 05:02 AM ---------- Previous post was at 04:53 AM ----------

WingedPanther said:

Generally, the output should be: 1, 1, 2, 3, 5, 8, 13, ...

Did I miss something, or are you adding a few leading terms?

Hi WingedPanther thanks for looking at my code, i have added the zero because the question has it in it. Is that what you mean?

---------- Post added at 05:40 AM ---------- Previous post was at 05:02 AM ----------

wim DC said:

Shortest possible way:

        System.out.print("0, 1, ");

        for(int i=0, j=1, sum=i+j; i<50 ; i=j, j=sum, sum = i+j){

            System.out.print(sum + ", ");

        } 

How less readable that is, I leave up to you. I don't think it's that bad.

Thanks wim DC your code is a lot shorter and working fine, just got to understand it now. if you had time you might explain what your code is doing for me and also for other that will be looking at the two different ways it has been done, This is why i put the thread up cause i couldn't find anythreads on "The Fibonnaci series" i could understand and i know a few of my class mates will be reading this as they have joined.

Here is your code:

public class FibonacciQ5Part3

	{

	

			public static void main(String[] args)

			{

				      // declare & calculate fibonnaci						

				System.out.print("0, 1, ");

        			for(int i=0, j=1, sum=i+j; i<50 ; i=j, j=sum, sum = i+j)

					{

            		        System.out.print(sum + ", ");

       			}

				

					

			}

	

	}



Here is my code:

public class FibonacciQ5Part2

	{

			public static void main(String[] args)

			{

						// declare variable

					int termRequired = 12;

					int nthTerm = 0;

					int twoPrevious = 0;

					int onePrevious = 1;

					

						// calculate fibonnaci						

					for (int i = 2; i < termRequired; i++)

					{

							// if statement so i can get the zero and other 1 to print on screen

							if (nthTerm < 2)

							{

								System.out.print(nthTerm + ", ");

							}

							

							nthTerm = onePrevious + twoPrevious;

							System.out.print(nthTerm + ", ");

							twoPrevious = onePrevious;

							onePrevious = nthTerm;

					}

			}

	

	}



As you can see and RUN, the two pieces of code do the same only the first one has a lot less code!
I was going about it the right way, just the long way :)

I hope this helps other as it has me thanks to all the Professional in here.

Gman

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
If the problem asks for it, put it in :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Okay, you should know that every for-loop can always be written as a while loop. I'll do so here, as I think it may be more clear:
for(int i=0 ; i<10 ; i++) { 

 //code

}



//is the same as


int i=0;

while(i<10){

  //code

  i++;

}

So for the fibonacci code:

for(int i=0, j=1, sum=i+j; i<50 ; i=j, j=sum, sum = i+j){

    System.out.print(sum + ", ");

}


//is the same as:

int i=0, j=1, sum=i+j;

while(i<50){

  System.out.print(sum + ", ");

  i=j;

  j=sum;

  sum=i+j;

}


//is the same as:

int i=0; 

int j=1;

int sum= i+j;


while(i<50){

  System.out.print(sum + ", ");

  i=j;

  j=sum;

  sum=i+j;

}



#10
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Lose the comments.

// declare variable
Does that really give you any additional information?

It is generally considered a good idea to write self-commenting code, so you almost never comment anything, if it's not necessarily needed to explain why programmer decided to use weird solutions instead of obvious one, etc.
instead of
// calculate fibonnaci

you can make a method calculateFibonacci();

which is self explanatory.

PS. Above doesn't apply to documenting, which is always a good idea.
.

#11
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US

Sinipull said:

Lose the comments.

// declare variable
Does that really give you any additional information?

...

It does. That way the code is split into sections; the comments don't have to say much for that.

Sinipull said:

It is generally considered a good idea to write self-commenting code, so you almost never comment anything, if it's not necessarily needed to explain why programmer decided to use weird solutions instead of obvious one, etc.
instead of
// calculate fibonnaci

you can make a method calculateFibonacci();

which is self explanatory.

It is a good idea to make a seperate function, fib (), that calculates the nth term of the fibonacci sequence. That way you can reuse that code. But it might still help to comment, at least how the OP did, inside the fib () function.

* * *

Maybe it is possible to build a cache for the fib () function, as well, like for the last 16 terms that were calculated; that way you can speed things up by at least a little, I think, if you can check to see if the nth term is in the cache. Associative arrays could be helpful in such cases (if Java allows those).



EDIT: By the way, he needs to output every term, up to the nth term, as the function approaches n. For that, the fib () function should have two parameters, I think, one for n, and one for the boolean value of whether or not to be verbose.

#12
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I agree with everything Sinipull said about commenting.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users