package margusmartseppcode.From_40_to_49; public class Problem_47 { static int unique_factors(int num, int[] primes, int[] factors) { int max = (int) Math.sqrt(num); for (int i = 0; primes[i] <= max; i++) if ((num % primes[i]) == 0) { do { num /= primes[i]; } while ((num % primes[i]) == 0); return num == 1 ? 1 : factors[num] + 1; } return 0; } // creates primes and factors on fly public static void main(String[] args) { int n = 0, ps = 1, max = 200000; int primes[] = new int[max], factors[] = new int[max]; for (n = 3, primes[0] = 2; n < max; n++) if ((factors[n] = unique_factors(n, primes, factors)) == 0) { factors[n] = 1; primes[ps++] = n; } else if ((factors[n] == 4) && (factors[n - 1] == 4) && (factors[n - 2] == 4) && (factors[n - 3] == 4)) break; System.out.println(n - 3); } }
Sunday, September 13, 2009
Euler Problem 47 solution
Time (s): ~0.067
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment