0

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?

Hadh
  • 317
  • 1
  • 7
  • 22
  • Check for Corner cases! If the input is more than size of int?? – ganeshvjy Jul 01 '15 at 22:55
  • I'm not sure I understood you, should I control user input? – Hadh Jul 01 '15 at 22:56
  • Shouldn't you use standard input? Not as argument? – aleksandar Jul 01 '15 at 22:57
  • Why are you excluding multiplies of 15? – user3707125 Jul 01 '15 at 22:59
  • There is no input actually. You should find the sum of all the multiples of 3 or 5 below 1000 not N. – aleksandar Jul 01 '15 at 23:00
  • I agree with @Zubergu. Your code didnt handle certain corner cases. Think about number 15. Your loop simply skips this number, instead of adding it to the sum once! – ganeshvjy Jul 01 '15 at 23:01
  • @zubergu I'm not learning through websites and I did run my code on Eclipse as I mentionned and it works fine and I know what I'm doing and what I did is take an argument or more at once and calculate the sum of multiples of 3 and 5, @ user3707125 because you'll be calculating multiple of 3 and 5 twice since 15 is in common I have the same results as the website what are the other corner cases? – Hadh Jul 01 '15 at 23:07
  • And how do I use standard input instead of arguments? – Hadh Jul 01 '15 at 23:08
  • 1
    @Hadh Sorry, didn't read the question close enough. hackerrank provides input from System.in. and is very specific on how to do it (read their manual), there are also possible problems with timeout as you calculate sum in every case twice. Another problem coming to mind is integer overflow, check problem specification for possible input size. – zubergu Jul 01 '15 at 23:14
  • Can you please provide me with the link to their manual or on how to use the system.in because I can't seem to find it. Thank you. – Hadh Jul 02 '15 at 00:16
  • @Hadh please remember to select an answer if your question was answered. – Turing85 Jul 20 '15 at 12:29

3 Answers3

2

You are skipping some numbers that should not be skipped.

if (((i % 3) == 0) || ((i % 5) == 0)
    && !(((i % 3) == 0) && ((i % 5) == 0)))

This statement says: i must be divisible by 3 or 5 AND is must not be divisible by 3 and 5. Rephrased: i must be divisible by 3 or 5, but not both of them. Just delete the second line and it should work.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Turing85
  • 18,217
  • 7
  • 33
  • 58
  • Yes it's correct, the line I did wasn't doing anything, and I still have the same problem, I think it's what @zubergu said, something with the System.in – Hadh Jul 02 '15 at 00:26
0

I believe it is a combination of what Turing85 said and wazaaaap. The examples for Project Euler all show it does not take different inputs. You just have to produce the correct output. So replace Integer.parseInt(args[j]); with Integer.parseInt(1000); To add to what Turing said, the solution should follow the following psuedocode:

target=999
sum=0
for i=1 to target do
if (i mod 3=0) or (i mod 5)=0 then sum:=sum+i
output sum
  • 1
    `Integer.parseInt(1000);`? How about just `1000`? – Ted Hopp Jul 01 '15 at 23:36
  • How OP starts the program is irrelevant. If he/she wishes to input the parameter as command line argument, this is totally fine. The result will not be affected (unless the command line argument will exceed `Integer.MAX_VALUE`, but is not the goal of the exercise). – Turing85 Jul 01 '15 at 23:38
  • No, you see in the first line you insert how many integers you have for example if I have 10 and 100 and I want to know the sum of multiples of 3 and 5 below 10 and the sum that's below 100, the input should be : **2** **10** **100** the output : **23** **2318** – Hadh Jul 02 '15 at 00:22
0

Using a for loop, you can get all the numbers from 0 to 1000, and by using an if condition, you get the required numbers i.e., multiples of 3, 5, adding them gives the final output, like the following:

public class SUM_3_5{

    public static void main(String []args) {
        int sum=0;
        int i;

        for(i=0;i<1000;i++)
        {
             if(i%3==0||i%5==0)
                 sum=sum+i;
        }

        System.out.println(sum);
    }
}
Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44
  • the question is to add all the multiples of 3 or 5. using the FOR loop we can get all the numbers from 0 to 1000, and by using the IF condition we get the required numbers i.e., multiples of 3, 5, adding them gives the final output. – Akshay Raghavendra Dec 03 '18 at 16:21