0

How can i use the brute force (naive algorithm) to check if a 16 bit long integer is prime or not and print all the prime numbers before it. number example: 1254786951475276. This is my code:

   import java.io.*;

public class PrimeNumbers {

    public static boolean checkPrime(long n)
    {
        if (n % 2 == 0)
            return false;

        for (int i = 3; i <= Math.sqrt(n); i += 2) 
        {
            if (n % i == 0)
                return false;
        }

        return true;
    }

    public static void generatePrimeNumbers(long n) {

        for (int i = 2; i<= n-1 ; i++) {

            if (checkPrime(i)) {
                System.out.println(i);
            }
        }
    }

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

        BufferedReader br = new BufferedReader (new InputStreamReader(System.in));

        long n = 0;
        n = Integer.parseInt(br.readLine());

        generatePrimeNumbers(n);
    }
    }
Learner
  • 41
  • 3
  • I guess you mean 16 digit.... – Afshin Mar 05 '19 at 16:08
  • Can you describe what you are actually trying to do? What you've written is a bit of an XY problem, meaning you had one problem and you're asking about another problem which you thought was related but wasn't. I can tell you that there are 279,238,341,033,925 primes with 16 digits. If you printed a million every second, it'd still take you nearly 9 years to print the results from this program. And I can guarantee you such a brute force algorithm won't get remotely close to that speed. – Cort Ammon Mar 05 '19 at 16:12
  • So you should probably explain what you were trying to do which led you to believing you needed to use this algorithm. Then it may be easier to suggest better algorithms. – Cort Ammon Mar 05 '19 at 16:12
  • first i want the user to enter a number upto 16 digits, and that number will be checked if it is prime or not. now the problem with this program which i have written is that it is not accepting 16 digit numbers. How can i perform primality check on a 16 digit number using this program? – Learner Mar 05 '19 at 16:26

1 Answers1

0

I guess you mean 16 digit, not 16 bit. 16 bit is really easy and you can just check it by a simple loop and division. Your checkPrime() can be optimized like this:

public static boolean checkPrime(long n)
{
    if (n == 2)
        return true;

    if (n % 2 == 0)
        return false;

    for (long i = 3; i <= Math.sqrt(n); i += 2) 
    {
        if (n % i == 0)
            return false;
    }

    return true;
}

But if number gets too big(I think even for 16 digit, it will not be necessary), you can use Miller-robin primality test. This test can be used to check if a VERY BIG number is prime or not.

If you need to generate all prime numbers till a specific number, you can use Sieve of Eratosthenes.

Just remember that int range is -2,147,483,648 to 2,147,483,647, so it cannot keep 16 digit number. You can use long because its range is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, so it can hold 16 digit number.

Afshin
  • 8,839
  • 1
  • 18
  • 53
  • Even after optimization my program is not accepting any number more than 10 digits. I want the program to be able to work with 16 digits maximum. (0-16 digits). – Learner Mar 05 '19 at 16:28
  • i tried implementing miller robin primality test, but the program was too complex for me to implement since i am a new programmer. i could not get miller robin test working, So i thought i would just stick to this program and try to get it work. – Learner Mar 05 '19 at 16:29
  • @Learner because you are using `int` as input parameter. change your data type to `long`. – Afshin Mar 05 '19 at 16:31
  • i tried that as well. I still get an error. Did u try running the code after changing the int to long? did it work for u? – Learner Mar 05 '19 at 16:36
  • @Learner It should work if you change everything to `long`. – Afshin Mar 05 '19 at 16:46