Java source--Calculate the smallest prime number greater than a few __ source code

Source: Internet
Author: User
Tags square root

Calculates the smallest prime that is greater than one number, based on the input number.

Words are not much to say, direct paste code.

Package com.fuxuemingzhu.countprime.main;

Import Java.util.Scanner; /** * <p> * title:main * </p> * <p> * Description: Calculates a minimum prime number greater than an input of * </p> * * @author Fuxu Emingzhu * * * * * @email * * @date October 27, 2014 PM 4:01:41/public class Countprime {/** * INP

	Utnum Input integer */private static int inputnum = 0;  /** * <p> * title:main * </p> * <p> * Description: The main function of the program, from here into the * </p> * * @param
		args/public static void main (string[] args) {//guide for a guided input System.out.println ("Please enter an integer to be evaluated:");
		Gets the keyboard input getinput ();
	Output results (); /** * <p> * title:getinput * </p> * <p> * Description: Getting keyboard input Results * </p> * * PRIV
		ate static void GetInput () {////Scanner class gets keyboard operation Scanner Inputscanner = new Scanner (;
		Inputnum = Inputscanner.nextint ();
	Close Inputscanner inputscanner.close (); }/** * <p> * title:output *</p> * <p> * Description: Output operation, at the same time the input of the judgment * </p>/private static void output () {if (checkIn
		Put (Inputnum)) {System.out.println ("the smallest prime that is greater than this number is:" + countprime (inputnum)); } else {System.out.println ("input error.") You need to enter a positive integer.
	 }/** * <p> * title:checkinput * </p> * <p> * Description: Need to enter a positive integer * </p> *
		* @param num * @return input is correct/private static Boolean checkinput (int num) {if (num >= 1) {return true;

	return false; 
	 /** * <p> * title:countprime * </p> * <p> * Description: Calculate the smallest prime number greater than num * </p> * * @param num * @return The smallest prime number greater than num/private static int countprime (int num) {/////////////////is greater than the smallest prime int
		Answer = num + 1;
		The above +1 is judged from the next number of this number, and does not judge whether the number itself is a prime while (IsPrime (answer)) {answer++;

	return answer;
/** * <p> * title:isprime * </p> * <p> * Description: Determining whether a number is prime * </p>	 * * @param num * @return A number is not prime */private static Boolean isprime (int num) {Boolean check = false;
			Calculates the square root of this number, reducing the computational steps for (int i = 2; I <= math.sqrt (num); i++) {///////to determine whether the remainder of the divisible variable i is 0, zero indicates that it is divisible, not prime
			if (num% (int) i) = = 0) {return true;

	} return check;

Attach a screenshot of the operation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.