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 5953113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:46:42+00:00 2026-05-22T17:46:42+00:00

I made a program which can generate primes. I want the program to write

  • 0

I made a program which can generate primes. I want the program to write to file. When I generate primes up to Integer.Max_Value it doesn’t write all of the primes for some reason and it just stops. Here’s my code:

import java.io.*;

public class Primes {


public static void main(String[] args) throws IOException {

    generate(Integer.MAX_VALUE);

}

// generate primes.
public static void generate(int limit) throws IOException {
     BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\Primes.txt"));
     writer.write(2);
     for (int i = 3; i <= limit; i += 2) {
        if (isPrime(i)) {
            writer.write(Integer.toString(i));
            writer.newLine();
        }
    }
    writer.close();
}

// checking for primes
public static boolean isPrime(int n) {
    for (int i = 2; i <= (Math.sqrt(n)); i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}
}
  • 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-05-22T17:46:42+00:00Added an answer on May 22, 2026 at 5:46 pm

    I think your program isn’t hanging at all, it’s just taking a long time to do its work.

    As the numbers get larger, the amount of time taken to check them increases in proportion to their square root. Additionally, the distances between prime numbers is greater, the larger the numbers. Putting these together, the speed at which primes are found will tail off quite quickly.

    Have you attached a debugger to your program, and paused execution to inspect the local variables and see where it’s got to? Have you even put in System.out.println("About to calculate for " + i);? How do your know your program “just stops”?

    The first step should be some investigation into what your program’s actually doing. If you output timestamps with the println messages, you can also get an idea of how the speed of checking each individual number decays, and thus when you spot a pause you can have a rough estimate of how long the current check should take.


    Besides, your program will never terminate, since you have an infinite outer loop. Your for loop’s terminating condition is i <= Integer.MAX_VALUE, which means it will run until it finds an i such that i > Integer.MAX_VALUE. Can you name a value for i where that will hold true?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a program. I also made my own file type, which the program
I've made a program which contains pictures that can be dragged to move on
i have made a program in which i make a file in the data/data/mypackagename/
I have created a concurrent, recursive directory traversal and file processing program, which sometimes
I'm building a tiny webapplication in which our customers can update some details about
i have some project, which can be running on databases with different names. I
I am working on a program in which you can register complaints. There are
I made several changes to sharepoint 'init.js' which located in folder: C:\Program Files\Common Files\Microsoft
Hey, i've a simple question. Task: Program a backup system which can be used
I am making a project for college and have made a program which creates

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.