Weird situation in prime number checking code

lylec

When I was solving a problem for Project Euler, it asked me to sum up all the primes below 2 million. Here is my code:

#include<stdio.h>
#include<math.h>
int isPrime(int);
int main() {
    long long int sum = 0;
    int i; // index
    for(i = 2 ; i < 2000000 ; i++) {
        if(isPrime(i)) {
            sum += i;
        }
    }
    printf("%lli\n", sum);
}

int isPrime(int num) {
    int i; // index
    int sq = sqrt(num);
    for(i = 2 ; i <= sq ; i++) {
        if(num % i == 0) {
            return 0;
        }
    }
    return 1;
}

This code leads to the correct answer, 142913828922. But when I change the for loop in isPrime() to:

for(i = 2; i <= sq+1; i++)   // or even sq+2, sq+3, etc.

It leads to incorrect results such as 142913828920 and 142913828917, etc.

Why does it go wrong? Theoretically, it doesn't change the number isPrime() sends to main(), does it?

Patrick Roberts

Considering you changed the sum from 142913828922 to 142913828920, then the difference is 2, which means you're interpreting 2 as not prime. Changing sq to sq+1 should achieve that difference. Changing it to sq+2 would end up making 3 not prime.

((int)sqrt(2))+1 == 2
((int)sqrt(3))+2 == 3

and so on.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Algorithm for checking if number is factorable into a set of prime numbers

From Dev

Unable to find the mistake in prime number code in python

From Dev

Unary operators "++" and "--" weird situation

From Dev

Checking for a Prime Number with JavaScript

From Dev

Checking for prime number in boolean expression without defining a function

From Dev

prime number generation code in python

From Dev

Finding the 10001st Prime Number - Code not returning correct number

From Dev

Python Checking if prime number

From Dev

What is the difference between these two methods for checking if a number is a prime?

From Dev

Do not understand code to test if a number is prime or not

From Dev

Checking whether a number is prime in Scala

From Dev

Whats the difference between those two prime number checking algorithms

From Dev

Checking for prime number - C# logic

From Dev

Unable to find the mistake in prime number code in python

From Dev

Prime Number code

From Dev

Unary operators "++" and "--" weird situation

From Dev

generating random numbers and checking for number of prime numbers

From Dev

code that check if number is prime number

From Dev

Do not understand code to test if a number is prime or not

From Dev

Python - Why doesn't this prime number checking algorithm work?

From Dev

Python- checking a number to see if it's a prime number by using divisors

From Dev

PLSQL - How does this Prime number code work?

From Dev

A loop for checking prime number

From Dev

Code to print Nth prime number

From Dev

Asking user for input - prime number code

From Dev

Checking if a number is a prime number from a list, in Python

From Dev

Pari GP - Checking if user keyed in a prime number

From Dev

Confused about prime number checking function

From Dev

Getting weird output while finding prime number in java

Related Related

  1. 1

    Algorithm for checking if number is factorable into a set of prime numbers

  2. 2

    Unable to find the mistake in prime number code in python

  3. 3

    Unary operators "++" and "--" weird situation

  4. 4

    Checking for a Prime Number with JavaScript

  5. 5

    Checking for prime number in boolean expression without defining a function

  6. 6

    prime number generation code in python

  7. 7

    Finding the 10001st Prime Number - Code not returning correct number

  8. 8

    Python Checking if prime number

  9. 9

    What is the difference between these two methods for checking if a number is a prime?

  10. 10

    Do not understand code to test if a number is prime or not

  11. 11

    Checking whether a number is prime in Scala

  12. 12

    Whats the difference between those two prime number checking algorithms

  13. 13

    Checking for prime number - C# logic

  14. 14

    Unable to find the mistake in prime number code in python

  15. 15

    Prime Number code

  16. 16

    Unary operators "++" and "--" weird situation

  17. 17

    generating random numbers and checking for number of prime numbers

  18. 18

    code that check if number is prime number

  19. 19

    Do not understand code to test if a number is prime or not

  20. 20

    Python - Why doesn't this prime number checking algorithm work?

  21. 21

    Python- checking a number to see if it's a prime number by using divisors

  22. 22

    PLSQL - How does this Prime number code work?

  23. 23

    A loop for checking prime number

  24. 24

    Code to print Nth prime number

  25. 25

    Asking user for input - prime number code

  26. 26

    Checking if a number is a prime number from a list, in Python

  27. 27

    Pari GP - Checking if user keyed in a prime number

  28. 28

    Confused about prime number checking function

  29. 29

    Getting weird output while finding prime number in java

HotTag

Archive