• Every Single Prime Number Between One and a Billion

    There are over fifty-million prime numbers between one and a billion.

    The fall semester has begun at my college and I wanted to start practicing my programming skills, lest they start to atrophy. We haven’t gotten any major projects yet, so I just started working on my own in my spare time.

    I started writing a simple Boolean method that would check if an inputted number was prime or not (shown in the image above). Then, I thought it would be fun if the program recorded and printed out the largest prime number below any of the input numbers that weren’t prime. Then, I added a public integer so that the program could also record the amount of prime numbers leading up to the user input. At that point I realized that with the simple addition of a print method, I could combine the two integers and allow the program to list every single prime number leading up to the user’s input.

    I soon found myself having a lot of fun seeing the amount of prime numbers below bigger and bigger input values: starting with 100; then 1,000; then 10,000; and soon after 1,000,000 and 10,000,000. However, I soon noticed that the large numbers were outputting thousands and tens of thousands of numbers, erasing older inputs from the terminal.

    So naturally, my next step was to use FileWriter and PrintWriter to allow the function to write its output in a .txt file in addition to the terminal. This allowed me to obtain a whole list of all the prime numbers leading up to the user input.

    So, of course, I decided to find every single prime number between one and a billion.

    It took approximately half an hour of runtime, but I emerged victorious in the end with a cumulative list of every single in the form of a .txt file one gigabyte in size.

    There are 50,847,534 prime numbers under 1,000,000,000, with the largest being 999,999,937.

    Here is a link to download the file if anyone is interested. It’s about 970 megabyes.

    Below is the code I used in my program:

    import java.io.IOError;
    import java.io.IOException;
    import java.util.Scanner;
    
    
    public class Main
    {
        public static void main(String[] args) throws IOException {
            Scanner scan = new Scanner(System.in);
            System.out.println("Please input number:\n");
            int input = scan.nextInt();
            countPrimes(input);
    
        }
    
        public static boolean isPrime(int num)
            {
            if(num == 2 )
            {
                return true;
            }
            else if (num==1||num==0)
            {
                return false;
            }
            else for (int i = 2; i < Math.sqrt(num) + 1; i++)
            {
                if(num%i == 0)
                {
                    return false;
                }
            }
            return true;
        }
        public static int count = 0;
    
        public static void countPrimes(int num) throws IOException {
            WriteFile writeFile = new WriteFile("primeNumbers.txt");
            writeFile.openFile();
            for(int i = 2; i < num; i ++)
            {
                if(isPrime(i))
                {
                    count++;
                    String primeNumber = count + ". " + i;
                    System.out.println(primeNumber);
                    writeFile.writeToFile(primeNumber);
                }
            }
            String countMessage = "There are " + count + " prime numbers under " + num + ".";
            System.out.println(countMessage);
            writeFile.writeToFile(countMessage);
            writeFile.closeFile();
        }
    
    }
    
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.io.IOException;
    
    public class WriteFile
    {
        private String path;
        private boolean appendToFile = false;
        private PrintWriter printLine;
    
        public WriteFile(String filePath)
        {
            path = filePath;
    
        }
    
        public WriteFile(String filePath, boolean appendValue)
        {
            path = filePath;
            appendToFile = appendValue;
        }
        public void openFile() throws IOException
        {
            FileWriter write = new FileWriter(path, appendToFile);
            printLine = new PrintWriter(write);
        }
        public void closeFile()
        {
            if(printLine != null)
            {
                printLine.close();
            }
        }
        public void writeToFile(String textLine)
        {
            printLine.printf("%s%n", textLine);
        }
    
    
    
    
    
    }