0

This is my source code:

public class Fibbonaci {

static int count;
static int size;

public static int Fibbonaci(int n) {
    count++;

    if (n == 0) {
        return 1;
    }
    if (n % 10 == 0) {
        return (Fibbonaci(n / 2) + Fibbonaci(n / 5));
    } else if (n % 6 == 0) {
        return (Fibbonaci(n / 2) + Fibbonaci(n / 3));
    } else {
        return (Fibbonaci(n / 3) + Fibbonaci(n / 5));
    }

}

public static void main(String[] args) {
    int[] lim = new int[1024];

    for (int n = 1; n <= lim.length - 1; n++) {
        count = 0;
        Fibbonaci(n);
        size = (int) Math.floor(Math.log(n) / Math.log(2)) + 1;

        System.out.print("T(" + size + ") = ");
        System.out.println("" + count);
    }
}

}

Some of my current output is this:

T(1) = 3
T(2) = 3
T(2) = 5
T(3) = 5
T(3) = 7
T(3) = 9
T(3) = 7
T(4) = 7
T(4) = 9
T(4) = 11
T(4) = 9
T(4) = 15

How can i print the max value for the size and count where i can get an output of similar to T(1) = 3 T(2) = 5 T(3) = 9 T(4) = 15?

Abner
  • 29
  • 7

2 Answers2

1

Store the list of values for each size (in a Map preferably), sort this list and print the biggest element.

EDIT Even simpler - overwrite the value for each size in a Map<Integer, Integer> and as the latest result is the biggest, the last value in the Map is the biggest for this size.

Smutje
  • 17,733
  • 4
  • 24
  • 41
0

You could store the max value in a variable and compare it inside your loop

maxValue = 0;
if ( X > maxValue ){
    maxValue = X
}

Also, you could use a collection and order it, use comparators. But just so you know, there are much better implementations for Fibonacci (just google for it)

Using list to order: how to get maximum value from the List/ArrayList

Using comparators http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html

Community
  • 1
  • 1
eduardohl
  • 1,176
  • 2
  • 10
  • 23