-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + ")"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package p050; | ||
|
||
public class P005SmallestMultiple { | ||
} |