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:
Resolução: O maior Fator Primo de 600851475143 é 6857
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
