Created
September 30, 2019 11:06
-
-
Save ChrisLeNeve/6e7507276a4aff931f97a9d85e073a0b to your computer and use it in GitHub Desktop.
projecteuler.net: problem 1
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
import java.util.List; | |
import java.util.Set; | |
import java.util.TreeSet; | |
class Scratch { | |
public static void main(String[] args) { | |
// int max = 10; // 23 | |
int max = 1000; // 233168 | |
int sum = findMultiplesV2(max); | |
System.out.println("Sum is: " + sum); | |
} | |
private static int findMultiples(int max) { | |
int sum = 0; | |
Set<Integer> allMultiples = new TreeSet<>(); | |
for (int i = 0; i < max; i += 3) { | |
allMultiples.add(i); | |
} | |
for (int i = 0; i < max; i += 5) { | |
allMultiples.add(i); | |
} | |
for (int i : allMultiples) { | |
sum += i; | |
} | |
return sum; | |
} | |
private static int findMultiplesV2(int max) { | |
int sum = 0; | |
for (int i = 0; i < max; i++) { | |
if (i % 3 == 0 || i % 5 == 0) { | |
sum += i; | |
} | |
} | |
return sum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment