From 08f7d16cdd3c8be31e3ff95765414847281efe63 Mon Sep 17 00:00:00 2001 From: Ivan Munteanu Date: Sun, 3 Jun 2018 21:29:02 -0500 Subject: [PATCH] p2-p4 --- .../java/p050/P002SumOfEvenFibonacci.java | 30 ++++++++++++++ src/main/java/p050/P003MaxPrimeFactor.java | 24 ++++++++++++ src/main/java/p050/P004LargestPalindrom.java | 39 +++++++++++++++++++ src/main/java/p050/P005SmallestMultiple.java | 4 ++ 4 files changed, 97 insertions(+) create mode 100644 src/main/java/p050/P002SumOfEvenFibonacci.java create mode 100644 src/main/java/p050/P003MaxPrimeFactor.java create mode 100644 src/main/java/p050/P004LargestPalindrom.java create mode 100644 src/main/java/p050/P005SmallestMultiple.java diff --git a/src/main/java/p050/P002SumOfEvenFibonacci.java b/src/main/java/p050/P002SumOfEvenFibonacci.java new file mode 100644 index 0000000..228a834 --- /dev/null +++ b/src/main/java/p050/P002SumOfEvenFibonacci.java @@ -0,0 +1,30 @@ +package p050; + +import java.util.ArrayList; +import java.util.List; + +public class P002SumOfEvenFibonacci { + + private static final int LIMIT = 4000000; + + private static List generateFibonacci(int limit) { + List f = new ArrayList<>(); + f.add(1); + f.add(1); + int index = 1; + while (f.get(index) <= limit) { + f.add(f.get(index) + f.get(index - 1)); + index++; + } + return f; + } + + + public static void main(String[] args) { + int sum = generateFibonacci(LIMIT) + .stream() + .filter(it -> it % 2 == 0) + .reduce(0, (a, b) -> a + b); + System.out.println("sum even fibonacci = " + sum); + } +} diff --git a/src/main/java/p050/P003MaxPrimeFactor.java b/src/main/java/p050/P003MaxPrimeFactor.java new file mode 100644 index 0000000..ca4707f --- /dev/null +++ b/src/main/java/p050/P003MaxPrimeFactor.java @@ -0,0 +1,24 @@ +package p050; + +public class P003MaxPrimeFactor { + + private static final long LIMIT = 600851475143L; + + public static void main(String[] args) { + int factor = 2; + int maxPrimeFactor = 1; + + long number = LIMIT; + while (number > 1 || number > factor) { + if (number % factor == 0) { + maxPrimeFactor = factor; + while (number % factor == 0 && number > 1) { + number /= factor; + } + } + factor++; + } + + System.out.println("max prime factor " + maxPrimeFactor); + } +} diff --git a/src/main/java/p050/P004LargestPalindrom.java b/src/main/java/p050/P004LargestPalindrom.java new file mode 100644 index 0000000..48e85b6 --- /dev/null +++ b/src/main/java/p050/P004LargestPalindrom.java @@ -0,0 +1,39 @@ +package p050; + +public class P004LargestPalindrom { + + private static boolean palindrom(int n) { + String a = String.valueOf(n); + + StringBuilder sb = new StringBuilder(); + for (int i = a.length() - 1; i >= 0; i--) { + sb.append(a.charAt(i)); + } + + return a.equals(sb.toString()); + } + + public static void main(String[] args) { + + int maxI = -1; + int maxJ = -1; + int max = -1; + for (int i = 999; i >= 100; i--) { + for (int j = i; j >= 100; j--) { + int p = i * j; + if (palindrom(p)) { + if (max < p) { + max = p; + maxI = i; + maxJ = j; + } + + // no need to iterate j further + break; + } + } + } + + System.out.println("max palindrome = " + max + " (" + maxI + " * " + maxJ + ")"); + } +} diff --git a/src/main/java/p050/P005SmallestMultiple.java b/src/main/java/p050/P005SmallestMultiple.java new file mode 100644 index 0000000..91e2030 --- /dev/null +++ b/src/main/java/p050/P005SmallestMultiple.java @@ -0,0 +1,4 @@ +package p050; + +public class P005SmallestMultiple { +}