Home » Tip Bank » Java » Java APIs
  • Email Article
  • Comment on this Article
  • Share Article
    • Digg
    • del.icio.us
    • Newvine
    • furl
    • StumbleUpon
    • BlinkList
    • Newsvine
    • Magnolia
    • Facebook
    • Tailrank
    • Slashdot
    • Technorati
    • Google Bookmarks
    • Yahoo Favorites
    • Windows Live
    • Ask
Language: Java
Expertise: Intermediate
Apr 17, 2018

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Watch

How to Check Whether a Number Is Prime

Java has an API called BigInteger and it is easily able to identify if the number under consideration is a prime number or not.

Code snippet:

import java.math.BigInteger;

public class ProbablePrime
{
    public static void main(String[] args) 
   {
      ProbablePrime probablePrime = new ProbablePrime();
      int numberArg = Integer.parseInt(args[0]);
      probablePrime.proceed(numberArg);
   }
   
   private void proceed(int numberArg)
   {
      //numberArg is the number under consideration. The argument 1 for isProbablePrime method is the certainty
      System.out.println(BigInteger.valueOf(numberArg).isProbablePrime(1));
      
   }
}

/*

Expected output:

[root@mypc]# java ProbablePrime 79
true

[root@mypc]# java ProbablePrime 80
false

*/
M S Sridhar
 
Submit a Tip
Browse "Java" Tips
Browse All Tips
googlecode.com
javascriptkit.com