https://projecteuler.net/problem=3
En Java.
Implementaremos el método más sencillo, división por tentativa.
1
2 public class EulerSqrtRev_3 {
3
4 public static boolean isPrime(long number) {
5 if (number == 2) return true;
6 if (number < 2 || number % 2 == 0) return false;
7 double max = Math.sqrt(number);
8 for (long i = 3; i <= max; i +=2) {
9 if (number % i == 0) {
10 return false;
11 }
12 }
13 return true;
14 }
15
16 public static void main(String args[]) {
17 final long number = 600851475143L;
18 long max = number/2;
19 for (long i = 2; i < max; i++) {
20 if (number % i == 0 && isPrime(i)) {
21 System.out.println(i);
22 }
23 }
24 }
25
26 }
No hay comentarios:
Publicar un comentario