-
Notifications
You must be signed in to change notification settings - Fork 0
/
Euler001.java
61 lines (55 loc) · 1.86 KB
/
Euler001.java
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package org.fouda.solutions.euler;
import java.util.*;
public class Euler001 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
TreeSet<Integer> inputTree = new TreeSet<>();
List<Integer> inputList = new ArrayList<>();
Map<Integer,Integer> cache = new HashMap<>();
for (int a0 = 0; a0 < t; a0++) {
int n = in.nextInt();
inputList.add(n);
inputTree.add(n);
}
int max = inputTree.pollLast();
int sum = 0;
for (int y = 1; y < max; y++) {
cache.put(y,sum);
if (y % 3 == 0 || y % 5 == 0) {
sum = sum + y;
}
}
cache.put(max,sum);
inputList.forEach(k -> System.out.println(cache.get(k)));
}
//Idea#1 - Cache results
/*
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
Map<Integer, Integer> cache = new HashMap<>();
for (int a0 = 0; a0 < t; a0++) {
int n = in.nextInt();
int result = 0;
if (cache.containsKey(n)) {
result = cache.get(n);
} else {
int start = cache.keySet().parallelStream().filter(e -> e < n).sorted(Collections.reverseOrder()).findFirst().orElse(1);
int value = cache.getOrDefault(start,0);
result = getSumOfNumbersThatAreMultipliedByThreeOrFive(n,start,value);
}
System.out.println(result);
}
}
public static int getSumOfNumbersThatAreMultipliedByThreeOrFive(int n,int start,int cache) {
int sum = cache;
for (int y = start; y < n; y++) {
if (y % 3 == 0 || y % 5 == 0) {
sum = sum + y;
}
}
return sum;
}
*/
}