Results 1 to 9 of 9
  1. #1
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Red face Using for-loop and arrays to calculate factorials and fibonacci sequence

    The code is to calculate the fibonacci sequence and factorials, using two types of parameters. My code works for the first parameters of the fibonacci sequence and factorial, but the array part of the fibonacci code and factorial code doesn't work. I know I am very close, here it is:
    Java Code:
    public static long fib(int max) {
           
            long subTotal = 0;
            int a=0,b=1,f=0;
            for (int i = 2; i <= max; i++) {
                
                f=a+b;
                a=b;
                b=f;
                
                System.out.println("Fibonacci " + i + " value " + f);
            }
            return f;
        }
        
        
        public static long fib(long[] arr, int max) {
           
            
            for (int i = 2; i <= max ; i++) {
                if (arr[i] == 0) {                  //this is the part that I can't figure out
                    arr[i] = arr[i-1] + arr[i-2];    
                    System.out.println("Fibonacci[" + i + "] value " + arr[i]);
                }
            }
            return arr[max];
    
        }
        
        
        
        
        public static long factorial(int max) {
            
            long subTotal = 1;
            for (int i = 2; i <= max; i++) {
                subTotal = subTotal*i;
                
                System.out.println("factorial " + i + " value " + subTotal);
            }
            return subTotal;
        }
        
        
        public static long factorial(long[] arr, int max) {
          
            
            for (int i = 1; i <= max ; i++) {
                if (arr[i] == 0) 
                                         //this is the second part that I can't figure out
                	arr[i]= i*arr[i-1];   
                    System.out.println("factorial[" + i + "] value " + arr[i]);
                }
            
            return arr[max];

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What is the input and output supposed to look like, that may help you out a bit, and it will help us help you as well.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Java Code:
    public static long fib(long[] arr, int max) {
           
            
            for (int i = 2; i <= max ; i++) {
                if (arr[i] == 0) {                  //this is the part that I can't figure out
                    arr[i] = arr[i-1] + arr[i-2];    
                    System.out.println("Fibonacci[" + i + "] value " + arr[i]);
                }
            }
            return arr[max];
    
        }
    We know that in the fibonacci sequence the first 2 numbers are 1. So why not insert them into the array before doing anything else. Why bother with the if statement? To calculate every number after the first 2 we add the previous 2 numbers. No if's buts or maybes. Just do it.

  4. #4
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Smile Thank you Junky! (not factorial but an actual exclamation point))

    After reading your righteous advice, I simply initialized arr[1] for both the factorials and fibonacci sequence, the code now works as intended, and here it is:
    Java Code:
     public static long fib(long[] arr, int max) {
          
             for (int i = 2; i <= max ; i++) {
                 arr[1]=1;                          //<----this is the part that did the trick
                 arr[i] = arr[i-1] + arr[i-2];    
                    
                 System.out.println("Fibonacci[" + i + "] value " + arr[i]);
                
            }
            return arr[max];
    
    public static long factorial(long[] arr, int max) {
           
             for (int i = 2; i <= max ; i++) {
               arr[1]=1;  //<---the same initialization as above fixed the code here
               arr[i]= i*arr[i-1];   
                   
                  System.out.println("factorial[" + i + "] value " + arr[i]);
                }
            
            return arr[max];

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Don't do it inside the loop.

  6. #6
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    well the code works fine now, what can happen if it is inside the loop?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Nothing bad can happen. It was just a micro optimisation. You want to assign the value once not everytime around a loop. your code is equivalent of:
    Java Code:
    int x;
    x = 10;
    x = 10;
    x = 10;
    x = 10;
    x = 10;
    x = 10;
    etc
    Pointless huh?

  8. #8
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Alright I gotcha, thanks again.

  9. #9
    SDC
    SDC is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default

    you could do the fib sequence in a for loop without using arrays like this:
    Java Code:
    int i, j=1,k=0,max=20;
    for (i=1;i<=max;i++,k+=j,j=k-j)
    System.out.println("Fibonacci[" + i + "] value " + k);
    Last edited by SDC; 03-10-2011 at 03:52 AM.

Similar Threads

  1. Fibbonaci sequence without arrays
    By sunde887 in forum New To Java
    Replies: 17
    Last Post: 01-06-2011, 06:13 AM
  2. Fibonacci sequence
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 6
    Last Post: 03-25-2010, 06:59 AM
  3. loop problem-arrays
    By ester in forum New To Java
    Replies: 0
    Last Post: 02-02-2010, 09:44 PM
  4. Linked list sequence and array sequence
    By Predz in forum New To Java
    Replies: 1
    Last Post: 12-31-2009, 01:30 AM
  5. Factorials help
    By jhering1986 in forum New To Java
    Replies: 3
    Last Post: 04-06-2009, 01:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •