Skip to content

Commit

Permalink
p2-p4
Browse files Browse the repository at this point in the history
  • Loading branch information
zverbatim committed Jun 4, 2018
1 parent 39f9d58 commit 08f7d16
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/main/java/p050/P002SumOfEvenFibonacci.java
Original file line number Diff line number Diff line change
@@ -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<Integer> generateFibonacci(int limit) {
List<Integer> 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);
}
}
24 changes: 24 additions & 0 deletions src/main/java/p050/P003MaxPrimeFactor.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
39 changes: 39 additions & 0 deletions src/main/java/p050/P004LargestPalindrom.java
Original file line number Diff line number Diff line change
@@ -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 + ")");
}
}
4 changes: 4 additions & 0 deletions src/main/java/p050/P005SmallestMultiple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package p050;

public class P005SmallestMultiple {
}

0 comments on commit 08f7d16

Please sign in to comment.