Project euler exercise 10 on Java. isPrime method doesn't work in the loop

galomoribundo

So, I'm beggining to learn programming and trying project euler with java. The problem 10 seems pretty straight forward and I thought I could use a method I used to get primes before in another problem. The thing is, the method works except when I put it in the for loop and I can't manage to see the diference between this and the other.

So this is my code

package euler10;
public class Primesum {
    public static void main(String[] args) {
        int suma=0;

        for (int i=0; i<2000000; i=i+2){

            if (isPrime(i) == true){
                System.out.println(i);
                suma=suma+i;
            }

        }
        System.out.println(suma);
    }

    public static boolean isPrime(int num) {
         boolean prime = false;
         long i;
        for (i=2; i < Math.sqrt(num) ; i++){
            long n = num%i;
            if (n == 0){
                prime = false;
            } else {
                prime = true;
            }
        }
        return prime;
    }
}

The isPrime method works fine out of the loop but in it it's always true. Even with even numbers it will return true, and I think those aren't very primey :)

ppeterka

I don't really think that there is anything to do with the loop...

However, there is a logic flaw in the code...

public static boolean isPrime(int num) {
    long i;
    for (i=2; i <= Math.sqrt(num) ; i++){
        long n = num%i;
        if (n == 0){
            return false;//found a divisor : not prime
        } 
    }
    //went through all the way to sqrt(num), and found no divisor: prime!
    return true;
}

We can stop whenever the first divisor is found, there is no need to find all of them -- that is another excercise...

Also, logically, if one wanted to use the boolean variable this way, it would have been initialised with true, and put to false, and kept at that, when a divisor is found...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Euler's method in MATLAB: code doesn't work

From Dev

simple isPrime function from a tutorial doesn't work

From Dev

the code for project euler #5 doesn't works

From Dev

Ruby: #8 on Project Euler, solution doesn't seem to work, but I feel so close

From Dev

Trying to complete Project Euler #5, passes all syntax checks but doesn't work

From Dev

Polymorphism doesn't work in method arguments in Java

From Dev

Java generic method doesn't work as expected

From Dev

listFiles() method in Java doesn't work in .jar

From Dev

Java generic method doesn't work as expected

From Dev

Project Euler #10

From Dev

Zed Shaw exercise 20 doesn't work

From Dev

Method type-parameterized doesn't work in for loop

From Dev

recursive method to display a string backwards but while loop doesn't work

From Dev

Project euler 12 python code doesn't run, is it slow or what?

From Java

Project Euler problem 10, wrong answer but why (Java)

From Dev

Why doesn't JUnit test work in Play 2.3.1/Java project?

From Dev

Project Euler #23 in Java

From Dev

Project Euler 2 in Java

From Dev

Project Euler #8 in java

From Dev

Project Euler 14 Java

From Dev

Project Euler #23 in Java

From Dev

Project Euler #22 Java

From Dev

Example project doesn't work

From Dev

Java's String concatenation doesn't work inside 'for' loop

From Dev

do-while loop doesn't seem to work (Java)

From Dev

return statement in a Java for loop doesn't exit method

From Dev

Return off a static, recursive Java method doesn't work

From Dev

ImageJ (plugin Java): auto threshold method doesn't work

From Dev

Spring security with Java Config doesn´t work eraseCredentials method

Related Related

HotTag

Archive