Fibonacci sequence backward

我与影子孤独终老i 提交于 2019-12-06 04:06:05

问题


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 low, but I can't make this program force to run "backward".


回答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);
}

}




回答2:


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.




回答3:


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.




回答4:


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



回答5:


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].



回答6:


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




回答7:


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.




回答8:


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.



来源:https://stackoverflow.com/questions/5515782/fibonacci-sequence-backward

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!