I've started solving Project Euler problems. Is the code completely optimized?
Question:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below N.
My solution:
import java.util.LinkedList;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testCases = sc.nextInt();
for (int i = 0; i < testCases; i++) {
int input = sc.nextInt();
System.out.println(calculate(input));
}
}
public static boolean isMulThree(int input) {
if (input % 3 == 0)
return true;
else
return false;
}
public static boolean isMulFive(int input) {
int val = input % 10;
if (val == 5 || val == 0)
return true;
else
return false;
}
public static int calculate(int input) {
LinkedList<Integer> al = new LinkedList<>();
int sums = 0;
for (int i = 1; i < input; i++) {
if (isMulThree(i) || isMulFive(i)) {
al.add(i);
}
}
for (int count : al) {
sums = sums + count;
}
return sums;
}
}