java - The sum of all the multiples of 3 or 5 below N. Project Euler -


so i'm doing project euler challenge , i'm stuck @ first one, i'm using java pl. example if have list natural numbers below 10 multiples of 3 or 5, 3, 5, 6 , 9. sum of these multiples 23. have find sum of multiples of 3 or 5 below n.

my code works on eclipse "nice try, did not pass test case." stdout : no response , when submit code wrong answer on test cases, here's code:

public class solution {     public static void main(string[] args) {         (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;         (int = 0; < nn; i++) {             if (((i % 3) == 0) || ((i % 5) == 0)                 && !(((i % 3) == 0) && ((i % 5) == 0))) {                 s = s + i;             }         }         return (s);     } } 

update : so, looked more , turns out how it's supposed 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;     (int = 0; < nn; i++) {         if (((i % 3) == 0) || ((i % 5) == 0)) {             s = s + i;         }     }     return (s); } 

}

now problem left want able read numbers display sum, reads 1 number , display sum right after it, ideas?

you skipping numbers should not skipped.

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

this statement says: i must divisible 3 or 5 , must not divisible 3 , 5. rephrased: i must divisible 3 or 5, not both of them. delete second line , should work.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -