开发者

Fibonacci sequence backward

Here is the code:

class Fibonacci {
    static final int MIN_INDEX = 1;
    public static void main (String[] args){
        int high = 1;
        int low = 1;
        String jel;
        System.out.println("9: " + high);

    for (int i = 8; i >= MIN_INDEX; i--){
        if (high % 2 == 0)
            jel = " *";
        else 
            jel = " ";
        System.out.println(i + ": " + high + jel);
        high = low + high;
        low = high - low;


    }
}
}

I want to make this program, to write the output numbers backward. So I want that not only the ' i ' step from the last to the first, but the numbers too.

In this example, the output is: 1, 1, 2, 3, 5, 8 , eg... But I want to show it in the sequence looks like: eg... , 8, 5, 3, 2, 1, 1.

I tried to change the high and l开发者_运维问答ow, but I can't make this program force to run "backward".


No Java here, but Fibonacci numbers have an explicit closed form:

f[n_] := N@(GoldenRatio^n - (1 - GoldenRatio)^n)/Sqrt[5];  

Where

GoldenRatio = (1 + Sqrt[5])/2

So you can do:

For[i = 10, i > 0, i--,
     Print[f[i]];
  ];  

Output:

55.
34.
21.
13.
8.
5.
3.
2.
1.
1.

Edit

As an aside note The Golden Ratio is one of those wonderful pervasive numbers that you'll find in nature, science and the arts.

You may find the golden ratio from Sea Shells to the Parthenon.


You could insert them into an array as you go along, then just reverse the array and print them out? Not exactly efficient, but it is easy to do.


int high = 8;
int low = 5;
while (low > 0) {
  System.out.println(high);
  int temp = low;
  low = high - low;
  high = temp;
}


There are two possibilities:

  1. Store the numbers instead of printing them and at the end print them out in reverse.
  2. Run the algorithm forward to discover the last two numbers, and then produce and print the reverse series r on the fly by noting that r[i]=r[i-2]-r[i-1].


Yup.. just like the other folks are saying.. i would store in a collections, then sort and print

i just modified your example... run it and see if this is the behavior you expect.

class Fibonacci {
static final int MIN_INDEX = 1;

public static void main(String[] args) {
    int high = 1;
    int low = 1;
    String jel;
    List<String> numbers = new ArrayList<String>();
    numbers.add("9: " + high);

    for (int i = 8; i >= MIN_INDEX; i--) {
        if (high % 2 == 0) {
            jel = " *";
        }
        else {
            jel = " ";
        }
        numbers.add(i + ": " + high + jel);
        high = low + high;
        low = high - low;
    }

    Collections.sort(numbers);
    System.out.println(numbers);
}

}


One option would be to store the outputs in an array as you go and then traverse the array backwards.


You can store all of the elements into a data structure then print them out backwards because of the nature of the Fibonacci sequence, since the each value (except for the first and second) depends on the sum of the two previous values.


I too would just run through the sequence normally (i.e. not in reverse) and store the results in a collection (probably an ArrayList). But no need to sort after or even traverse the list in reverse order, you could just add each new "entry" in the sequence into position 0 in the list as you go using:

list.add(0, i + ": " + high + jel);

This will ensure that the list stores the sequence in reverse order.

That is just another possible solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜