import java.util.*; public class euler { public static void main(String[] args) { System.out.println("Euler question 1: " + question1()); System.out.println("Euler question 2: " + question2()); System.out.println("Euler question 4: " + question4()); System.out.println("Euler question 5: " + question5()); System.out.println("Euler question 6: " + question6()); System.out.println("Euler question 7: " + question7()); System.out.println("Euler question 9: " + question9()); System.out.println("Euler question 10: " + question10()); } public static int question1() { int a = 0; for(int i = 0; i < 1000; i++){ if ((i % 3 == 0) || (i % 5 == 0 )) a += i; } return a; } public static int question2() { int a = 0; for (int i = 0; fib(i) < 4000000; i++) { if (fib(i) % 2 == 0) a += fib(i); } return a; } public static long fib(long i){ if (i > 1) return fib(i - 1) + fib(i - 2); return i; } public static int question4() { int answer = 0; for(int i = 100; i < 1000; i++) { for(int j = 100; j < 1000; j++) { if (isPalindrome(i*j)) { if (i*j > answer) answer = i*j; } } } return answer; } public static boolean isPalindrome(int l) { if(l == Integer.parseInt(getReverse(Integer.toString(l)))) return true; return false; } public static String getReverse(String arg) { String tmp = null; if (arg.length() == 1) { return arg; } else { String lastChar = arg.substring(arg.length()-1,arg.length()); String remainingString = arg.substring(0, arg.length() -1); tmp = lastChar + getReverse(remainingString); return tmp; } } public static long question5() { int count = 0, i = 1; for (i = 1; count != 20; i++) { for (int factor = 1; factor < 21; factor++) { if (i % factor == 0) count++; } if (count != 20) count = 0; } return i - 1; } public static long question6() { int a = 0, b = 0; for (int i = 0; i <= 100; i++) { a += (i * i); } for (int i = 0; i <= 100; i++) { b += i; } return (b * b) - a; } public static long question7() { int k = 0, i = 2; for (i = 2; k < 10001; i++) { if (isPrime(i)) k++; } return i - 1; } public static boolean isPrime(long n) { for (long i = 2; i < n; i++) { if(n % i == 0) return false; } return true; } public static long question9() { for (int a = 1; a < 1000; a++) { //a < 1000 is just ... good enough. for (int b = 1; b < 1000; b++) { //same here. if (isTriple(a, b)) { int c = (int)Math.sqrt((a * a) + (b * b)); if (a + b + c == 1000) return a * b * c; } } } return 0; } public static boolean isTriple(int c, int d) { if (Math.sqrt((c * c) + (d * d)) == (int)Math.sqrt((c * c) + (d * d))) return true; return false; } public static long question10() { int answer = 0; for (int i = 0; i < 2000000; i++) { if (isPrime(i)) answer += i; } return answer; } }