I want to find it out for the number

2345678901233.. but it is giving the error ..is there any method to find out the primes beyond the range of integer..

import java.util.*;

    

public class inc {
   int flag=0;
   int h=2;

    public void checkprime(int k)
            {

        while(h<k){

        if(k%h==0){

           flag=1;

        }

       h++;

    }
        if(flag==1 ||k==1 )
        System.out.println("The number is not prime");
        else
                    System.out.println("The number is prime");


    }







 public static void main(String[] args) {

 inc a=new inc();

 Scanner i=new Scanner(System.in);
 int j=i.nextInt();

 a.checkprime(j);

 }

 }

Datatype "int" in Java uses 4 bytes, range -2,147,483,648 to 2,147,483,647,
Datatype "long" uses 8, range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

(source)

This may work..

import java.util.*;



public class inc {
   int flag=0;
   long h=2;

    public void checkprime(long k)
            {

        while(h<(k/2)){
        if(k%h==0){
           flag=1;
        }
       h++;
    }
        if(flag==1 ||k==1 )
            System.out.println("The number is not prime");
        else
            System.out.println("The number is prime");
    }
     public static void main(String[] args) {

     inc a=new inc();

     Scanner i=new Scanner(System.in);
     long j=i.nextLong();
     a.checkprime(j);
     }
 }
commented: Do not post code solutions +0

Well a few things on your program.

You probably want it to exit the loop when it determines it is not-prime so you get the result faster. Really large number can take awhile to find if they are prime so you could be talking about several seconds,tens of seconds, or minutes of getting a result quicker.

Secondly you use h < k/2 because 2 is the minimum number you are going to divide the prime by but fact is after you divide by 3 if h > k/3 it also can't be prime so to expedite that I'd reccomend something like:

class Prime
{
public boolean isPrime(long number)
{
public boolean isPrime = false;
public long counter = 2;
while(counter < number/counter)
{
if(number%counter == 0)
{
isPrime = true;
break;
}
return isPrime;
}
}

public static void main(string[] argz)
{
Prime a = new Prime();
long b = 2345678901233;
if(a.isPrime(b))
System.out.println("Number is Prime");
else
System.out.println("Number is not Prime");
}

All of these are brute force methodologies and very slow in comparison to other algorithms but if you got a cup of copy and a couple dozen minutes feel free : )

/**
 * @(#)FindingPrimeNumbers.java
 *
 *
 * @author Ramanpreet Singh
 * @version 1.00 2010/11/10
 */
import java.util.Scanner;

public class FindingPrimeNumbers 
{
    public static void main(String[] args) 
    {
        int num;	//number entered by the user
        Scanner scan = new Scanner(System.in);
        
        //Ask for output
        System.out.println("Enter the number that you want to check whether it is a prime number or not: ");
        num = scan.nextInt();
        
        boolean notPrime = true;
        int i, j; 	//used for loops
        for(i = 1 ; i <= num/2; i++)
        {
        	for(j = 1; j <= num/2; j++)
        	{
        		if(notPrime)
        		{
        			if(num == (i*j))
        				notPrime = false;
        		}
        	}
        }
        
        if(notPrime)
        	System.out.println("The number you entered is a prime number.");
        else
        	System.out.println("The number you entered is not a prime number.");
    }
}

Here you this is what I just created after reading this post, I think this would find a prime number for you

ChaseRLewis - you are on the right track, and you can tight the bound even more by summing all the integers up to sqrt(n) instead of up to k/3, as can be seen here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.