Read about java program to find prime number, The latest news, videos, and discussion topics about java program to find prime number from alibabacloud.com
I used C ++ I wrote a code to determine whether a prime number is a prime number, but today I use
JAVA
it takes a lot of time. I have to admit myself
JAVA too much water, you need to study hard in
1 ImportJava.util.Scanner;2 3 /**4 * @authorSchrödinger's Cat5 * Java Determines if a number is prime6 * 7 * Primes, also known as prime numbers, refers to the number of natural numbers greater than 1 that cannot be divisible by other natural numbers except 1 and itself .*/8 Public classPrimenumber {9 Public St
The program prompts the user to enter a number, and then checks whether the number entered is prime.ImportJava.util.Scanner;classprimecheck{ Public Static voidMain (String args[]) {inttemp; BooleanIsprime=true; Scanner Scan=NewScanner (system.in); System.out.println ("Enter a number for check:"); //capture the input in
primes (prime number)Also known as Prime number, refers to the number of natural numbers greater than 1 , except 1 and the number itself, can not be divisible by other natural numbers (can also be defined as only 1 and the
Import java.util.*;public static void Main (string[] args) {Scanner sc=new Scanner (system.in); System.out.println ("Please enter an integer greater than 0 M");//prime number is the remainder is 1 and its own positive integer int m=sc.nextint (); if (m==2| | M==1) {System.out.println ("you entered a prime number");} Ex
Title: Determine the number of primes between 101-200 and the output of all primes.Idea: The method of judging primes: to remove 2 to sqrt (this number) with a number, if divisible, indicates that the number is not a prime, and vice versa is a
1 packagetest;2 3 Importjava.util.Scanner;4 5 //determine if the number of inputs is prime6 public classTest18 {7 public Static voidmain (string[] Args) {8Scanner s =NewScanner (system.in);9System.out.println ("number of inputs judged");Ten intA =S.nextint (); one for(inti=2; I//the smallest prime is 2. a if(a% i = = 0 i! = A)
It's nothing in itself, a bunch of codeIt's a tribute to the next.Had just learned Java, write a prime number within the output 100For me this has the code to run the performance neat person (but itself does not understand the algorithm), cannot tolerateSo I read some information.Reference:http://blog.csdn.net/program_think/article/details/7032600Own Cottage A(No
Prime: A natural number that is greater than 1 and that can only be divisible by 1 and itself is called Prime.
We think that the computer determines whether a number is a prime process: for example 11
i=2
2*3 2*3 2*5
I=3
3*2 3*3
I=4
4*2
I=5
5*2
I=6 (over 11 in size
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; /** *
Attach a screenshot of the operation.
Package com.day3.one;public class PrimeNumber1 {/*** @param args* Print a prime number between 101-200 and count, and one line per 5 output*/public static void Main (string[] args) {int count=0;for (int m=101;m{Boolean a=true;for (int i=2;i{if (m%i==0){A=false;Break}}if (a==true){System.out.print (m+ "");count++;}if (count%5==0)System.out.println ();}System.out.println (' \ n ' + ' total ' +count+ '
Title: Determine the number of primes between 101-200 and the output of all primes.Program Analysis: The method of judging primes: to remove 2 to sqrt (this number) with a number, if divisible, indicates that the number is not a prime, and vice versa is a prime.public class
(1) Time spent without optimization:public class Primenumber {public static void main (string[] args) { long start = System.currenttimemi Llis (); Boolean flag = False; for (int i = 2, I ) {for (int j = 2; J ) {if (i% j = 0) {flag = True;} } if (flag = = False) System.out.print (i+ ""); Flag = False;} long end = System.currenttimemillis (); System.out.println ("\ n" + (end- start));}} The time spent is: 27038ms(2) Optimize one: Inner loop judgment, w
Write a program to find the number of K in the array and the position of the output number. For example, in {2, 4, 3, 7}, the first number is 7 and the position is 4. The numbers of the second and third largest are 4, which can be output at any position of 1 and 3. Function
There is a 3*4 matrix that requires the program to find the value of the largest element in it, along with its line number and column number. (Daleitai algorithm)Solution: Program:#include int main (){int I, j, row = 0, Colum = 0, Max;int A[3][4] = {{1,2,3,4},{4,5,6,7},{-1,3
"154-find Minimum in rotated Sorted Array II (find the smallest number in the rotated Array II)""leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"Original QuestionFollow to "Find Minimum in rotated Sorted Array":What if duplicates is allo
Say nothing, directly on the code.The first is the node class, we all know/** * Binary tree node class * * @author heyufan * * @param And then the implementation of the two-fork search tree. /** * */package datastructure.tree.binarytree;import java.util.linkedlist;import java.util.Queue;/** * binary sorting tree * * @author Heyufan * */public class binarytree Test:/** * */package datastructure.test;import datastructure.tree.binarytree.binarytree;/** * @author Administrator * */public cl
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:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.