-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythogoreanTriples.java
More file actions
41 lines (36 loc) · 1.46 KB
/
PythogoreanTriples.java
File metadata and controls
41 lines (36 loc) · 1.46 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* Created by pranikchainani on 8/20/16.
*/
public class PythogoreanTriples {
public static List<List<Integer>> pythogoreanTriples(int maxNumber) {
List<List<Integer>> pythogoreanTriple = new ArrayList<>();
List<Integer> cValues = IntStream.rangeClosed(1, maxNumber)
.boxed()
.collect(Collectors.toList());
// for (int a = 1; a <= maxNumber; a++)
// {
// for (int b = a; b <= maxNumber; b++)
// {
// for (int c : cValues)
// {
// if (Math.round(Math.pow(a, 2) + Math.pow(b, 2)) == Math.pow(c, 2))
// {
// pythogoreanTriples.add(Arrays.asList(a,b,c));
// }
// }
// }
// }
IntStream.rangeClosed(1, maxNumber)
.forEach(a -> IntStream.rangeClosed(a, maxNumber)
.forEach(b -> cValues.stream()
.filter(c -> a*a + b*b == c*c)
.forEach(c -> pythogoreanTriple.add(Arrays.asList(a, b, c)))));
return pythogoreanTriple;
}
public static void main(String[] args) {
System.out.println(pythogoreanTriples(20));
}
}