Solucionando o Problema 1 do Projeto Euler
Descrição do Problema:Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Código:
package br.com.projetoeuler.problema2;
/**
* Problema:
* Each new term in the Fibonacci sequence is generated by adding the previous two terms.
* By starting with 1 and 2, the first 10 terms will be:
* 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
* By considering the terms in the Fibonacci sequence whose values do not exceed four million,
* find the sum of the even-valued terms.
*
* @author Lucas Frizzo
* lucas-frizzo.blogspot.com
*/
public class Problema2 {
private int numero;
public int getNumero() {
return numero;
}
public void setNumero(int numero) {
this.numero = numero;
}
public int somaTermos(Problema2 problema2){
int x=0;
int y=1;
int b = 1;
int soma = 0;
for(int i=0;iproblema2.getNumero()){
System.out.println("soma:"+soma);
break;
}
}return soma;
}
}
package br.com.projetoeuler.problema2;
public class TesteProblema2 {
public static void main(String[] args) {
Problema2 problema2 = new Problema2();
problema2.setNumero(4000000);
problema2.somaTermos(problema2);
}
}

Nenhum comentário:
Postar um comentário