Fibonacci question in Java

0

I have an issue and its resolution, which needs to be with while or do-while . But I did not understand the resolution, could anyone explain? I will comment on the specific doubt of each line.

  

The Fibonacci Sequence is a sequence of integers in which,   each subsequent term corresponds to the sum of the two preceding ones. Being   thus, develop an algorithm that informs a Fibonacci sequence that   list the values starting with 0 through 2584.

Resolution:

public static void main(String[] args) {

    int x1=0;

    int x2=1;  // Por que tem que declarar duas variáveis?

    do{         System.out.print(x1 + " " );  // Por que o "  " depois do x1?

         x2= x2 +x1;   // Também não entendi por quê isso

         x1= x2-x1;    // Nem isso

    }while(x1<2584);}}  // E por que tem que ser do-while.
    
asked by anonymous 09.11.2017 / 20:18

1 answer

3

Actually I think you need a course with the basics of logic and algorithm before you try to understand it.

I have refactored the code because it is quite wrong. I put better names in the variables that help understand

class Fibonacci {
    public static void main(String[] args) {
        int termo1 = 0;
        int termo2 = 1; //se são dois termos, precisa de duas várias para controlar
        do {
            System.out.print(termo1 + " "); //Está mandando imprimir um dos termos
            int temp = termo1 + termo2; //somando os dois últimos termos conforme o enunciado
            termo1 = termo2;  //fazendo o primeiro termo ter o valor do segundo
            termo2 = temp; //fazendo o segundo termo ter o valor somado dos últimos termos
        } while (termo1 <= 2584); //precisa de um laço para ficar repetindo até a condição
    }
}

See running on ideone . And in Coding Ground . Also I placed GitHub for future reference .

  The Fibonacci Sequence is a sequence of integers in which each subsequent term corresponds to the sum of the two preceding ones. Therefore, construct an algorithm that reports a Fibonacci sequence that lists the values started with 0 to the value 2584 .

     

The Fibonacci Sequence is a sequence of integers

If it is a sequence we need a flow control structure that repeats each element of the sequence. And if it is integer we need at least some integer data, it will probably be in variable.

  

Each subsequent term corresponds to the sum of the previous two

We already know how to get subsequent terms. If you need to always add the two previous ones we need to start with two of them, hence we have at least two variables.

  

Values started with 0 through 2584

Then the initial term is 0 and by definition Fibonacci the second is 1.

We also know that the repetition must continue until the term 2584 is reached, so this is the condition that must be true for the loop to continue with the flow within it. One of the ways you can do this is do-while where the first time will always run the command blocks and after the first iteration it decides whether to continue or not. You can do it in other ways. Notice that the condition was wrong, she ignored the last number. The statement gives me the idea that it should be included.

Obviously every time we repeat a term we need to print, so we need to call the function that prints. It would be interesting to give a space between the terms printed.

Again we return to the explanation of how to get the new term and is the sum of the last two terms, so we created a variable to store this sum. At this moment we have 3 terms, and we only need 2. In fact we can say that now the first term passes to have the value of the second, and then we can say that the second has the value of the third that just was generated, then we no longer need a third value. This is necessary to maintain the repeat loop operation pattern, it will always add up to the 1st. and 2o. terms.

Then repeat the new term and start calculating everything again.

Someone might question if you need the temporary variable to calculate the subsequent term. And need.

If you already played the sum of the two terms in the second, you would no longer have the previous value of the second term to play in the first term.

If you copy the second term for the first one, then the sum would go wrong because in practice you would be adding the second term to the first one that already has the value of the second, you would have already lost the value of the first. In the background would be doubling the value and not doing Fibonacci which is always going to add to the sequence.

With array it simplifies a bit because in practice it has several variables, you do not lose the previous terms ever.

    
09.11.2017 / 20:57