Fibonacci sequence

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 output numbers back. Therefore, I want not only the "i" step from the last to the first, but also the number.

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

I tried to change the high and low, but I can not get this program to work "back".

+4
source share
8 answers

Yup .. just like other people say. I would save it in collections, then sort and print

I just changed your example ... ran it and looked to 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); } 

}

0
source

There is no Java, but the 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 a note in a note, The Golden Ratio is one of those wonderful all-around numbers you'll find in nature, science, and art.

You can find the gold ratio from Sea Shells to the Parthenon.

+6
source

Can you insert them into an array as you move, and then just cancel the array and print them? Not quite effective, but easy to do.

+2
source

There are two possibilities:

  • Save the numbers instead of printing them, and at the end print them in the reverse order.
  • Run the algorithm forward to find the last two numbers, and then produce and print the inverse series r "on the fly", noting that r[i]=r[i-2]-r[i-1] .
+2
source
 int high = 8; int low = 5; while (low > 0) { System.out.println(high); int temp = low; low = high - low; high = temp; } 
+2
source

One option is to save the outputs in the array along the way, and then move the array back.

0
source

You can save all the elements in the data structure and then print them back due to the nature of the Fibonacci sequence, since each value (except the first and second) depends on the sum of the two previous values.

0
source

I would also just execute the sequence (i.e. not vice versa) and save the results in a collection (probably ArrayList). But you donโ€™t need to sort after or even cross the list in the reverse order, you can simply add each new โ€œrecordโ€ in the sequence to position 0 in the list as you use it:

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

This ensures that the list keeps the sequence in the reverse order.

This is another possible solution.

0
source

Source: https://habr.com/ru/post/1346257/


All Articles