Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8039607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:37:35+00:00 2026-06-05T03:37:35+00:00

I am trying to run this code to print the sum of all the

  • 0

I am trying to run this code to print the sum of all the prime numbers less than 2 million. This loop is never ending. Can anyone tell me what is wrong with the code? It seems to work with smaller numbers though.

public static void main(String[] args) {

        long result = 1;

        for(int i=0; i<2000000; i++) {
            if(isPrime(i)) {
                result+= i;
            }
        }
        System.out.println(result);

    }
private static boolean isPrime(long n) {
    boolean result = false;

    for(long i=2; i<(long)Math.sqrt(n); i++) {
        if(n%i == 0) {
            result = false;
            break;
        }
        else result = true;
    }
    return result;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:37:37+00:00Added an answer on June 5, 2026 at 3:37 am

    In isPrime you are only testing division by 2:

    private static boolean isPrime(long n) {
        boolean result = false;
    
        for(long i=1; i<n/2; i++) {
            if(n%2 == 0) {
                result = false;
                break;
            }
            else result = true;
        }
        return result;
    
    }
    

    it should be division by every i and starting from 2:

    for(long i=2; i<n/2; i++) {
        if(n%i == 0) {
          ...
    

    Practically in your current version an odd number n will keep dividing by 2 up to n/2 instead of stopping much sooner. Consider n = 21. You are dividing by 2 from 1 to 10, instead of dividing by 3 at the 3rd step and exiting.

    It not only gives incorrect results, but also takes much longer than needed to reach a return statement.

    Edit: For faster results check out this sieve of Erathostenes method:

    public static long sumOfPrimes(int n) {
    
        long sum = 0;
    
        boolean[] sieve = new boolean[n];
        for(int i = 2; i < Math.sqrt(n); i++) {
            if(!sieve[i]) {
                for(int j = i * i; j < n; j += i) {
                    sieve[j] = true;
                }
            }
        }
    
        for(int i = 2; i < n; i++) {
            if(!sieve[i]) {             
                sum += i;
            }
        }
    
        return sum;
    }
    

    Edit #2: Found some bugs with your new version. Here’s the corrected one:

    private static boolean isPrime(long n) {
        boolean result = false;
    
        if(n == 2 || n == 3) return true;
    
        for (long i = 2; i <= (long) Math.sqrt(n); i++) {
            if (n % i == 0) {
                result = false;
                break;
            } else
                result = true;
        }
    
        System.out.println(n + " " + result);
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run this code so that it runs a function for all
I'm trying to run this code but this error appear: Uncaught TypeError: string is
I'm trying to run this code: let coins = [50, 25, 10, 5, 2,1]
I am trying to run this code: ItemTaxonomy iTaxonomy = from itemTaxonomy in connection.ItemTaxonomy
I am trying to run this code : exec myStoredProcedure Cast('c5b48202-36af-4597-9780-5366d4188f55' AS uniqueidentifier), 744,
I am trying to run this seemingly simple piece of code which is repeated
Trying to convert this c code into MIPS and run it in SPIM. int
Trying to run this piece of code: void print_matrix(matrix* arg) { int i, j;
I am trying to run this code where I have a list of lists.
this piece of code is driving me crazy. I'm trying to print a help

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.