terça-feira, 13 de dezembro de 2011

Projeto Euler [Problema 3]

Solucionando o Problema 3 do Projeto Euler
Descrição do Problema: 
Find the largest prime factor of a composite number.
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?


Código:
package br.com.projetoeuler.problema3;

/**
 * Problema: Find the largest prime factor of a composite number.
 * @author Lucas Frizzo
 * lucas-frizzo.blogspot.com
 */
public class Problema3 {
 
 public int fatorPrimo(long num) {
  long aux = num;
  
  for (int i = 1; i <= num; i++) {
   int primo = ehPrimo(i);
      
   if (primo != 0) {
    System.out.println("primo: "+primo);
    
    while(aux%primo == 0){
     long divisao = aux/primo;
     System.out.println("divisao:"+divisao);
     
     if (divisao != 1) {
      aux = divisao;
     }else{
      return primo;
     }
    }
   }   
  }
  return 0;
 }
 
 public int ehPrimo(int num) {
  int primo = num;
  int cont = 0;
  for (int i = 1; i <= num; i++) {
   if (primo%i == 0) {
    cont ++;
   }   
  }
  if (cont==2) {
   return primo;
  }
  return 0;
  
 }
}

package br.com.projetoeuler.problema3;

import java.math.BigInteger;

/**
 * Problema: Find the largest prime factor of a composite number.
 * @author Lucas Frizzo
 * lucas-frizzo.blogspot.com
 */
public class TestProblema3 {
 
 public static void main(String[] args) {
  Problema3 problema3 = new Problema3();
  final long num = 600851475143L;
  //final long num = 13195;
  
  System.out.println("O maior Fator Primo de "+num+" é "+problema3.fatorPrimo(num)); 
 }

}


Resolução: O maior Fator Primo de 600851475143 é 6857




sexta-feira, 2 de dezembro de 2011

Projeto Euler [Problema 2]

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