forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem4.java
More file actions
27 lines (25 loc) · 918 Bytes
/
Problem4.java
File metadata and controls
27 lines (25 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package fj.demo.euler;
import fj.F;
import static fj.Function.flip;
import fj.data.Stream;
import static fj.data.Stream.iterate;
import static fj.function.Integers.multiply;
import static fj.function.Integers.subtract;
import static fj.Equal.charEqual;
import static fj.Equal.streamEqual;
import static fj.Ord.intOrd;
import static fj.Show.intShow;
/**
* Find the largest palindrome made from the product of two 3-digit numbers.
*/
public class Problem4 {
public static void main(final String[] a) {
final Stream<Integer> tdl = iterate(flip(subtract).f(1), 999).takeWhile(intOrd.isGreaterThan(99));
intShow.println(tdl.tails().bind(tdl.zipWith(multiply)).filter(new F<Integer, Boolean>() {
public Boolean f(final Integer i) {
final Stream<Character> s = intShow.show(i);
return streamEqual(charEqual).eq(s.reverse().take(3), s.take(3));
}
}).foldLeft1(intOrd.max));
}
}