So I'm doing the Project Euler challenge and I'm stuck at the first one, I'm using Java as pl. for example if we have to 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. We have to find the sum of all the multiples of 3 or 5 below N.
My code works on Eclipse but I get "Nice try, but you did not pass this test case." with stdout : No Response and when I submit the code I get Wrong Answer on all test cases, here's the code:
public class Solution {
public static void main(String[] args) {
for (int j = 0; j < args.length; j++) {
int N = Integer.parseInt(args[j]);
if (Somme(N) != 0) {
System.out.println(Somme(N));
}
}
}
public static int Somme(int Nn) {
int s = 0;
for (int i = 0; i < Nn; i++) {
if (((i % 3) == 0) || ((i % 5) == 0)
&& !(((i % 3) == 0) && ((i % 5) == 0))) {
s = s + i;
}
}
return (s);
}
}
UPDATE : So, I looked more and it turns out that this is how it's supposed to be done:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int Nbr = Integer.parseInt(line);
for(int j=0; j<Nbr;j++)
{
BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
String line2 = br2.readLine();
String[] numbers = new String[Nbr];
numbers[j]= line2;
System.out.println(Somme(Long.parseLong(numbers[j])));
}
}
public static long Somme(long Nn) {
long s = 0;
for (int i = 0; i < Nn; i++) {
if (((i % 3) == 0) || ((i % 5) == 0)) {
s = s + i;
}
}
return (s);
}
}
Now the only problem left is that I want it to be able to read ALL the numbers THEN display the sum, for now it reads one number and display the sum right after it, any ideas?