Prime number Concept
Prime number, also known as Prime, refers to a number of natural numbers greater than 1, except for 1 and the integer itself, which cannot be divisible by other natural numbers (which can also be defined as numbers of only 1 and two factors per se).
The smallest prime number is 2, which is also the only even number in the prime, and the other primes are odd. There are infinitely many primes, so there is no maximum prime number.
One: According to the definition to solve:
and the stupidest way, the less efficient:
Package test.ms;
public class Findprime {
//Find the prime between 1 to 1000;
public static void Main (string[] args) {
printprime (1000);
}
public static void printprime (int n) {for
(int i = 2; i < n; i++) {
int count = 0;
for (int j = 2; j<=i; j + +) {
if (i%j==0) {
count++;
}
if (j==i & count = = 1) {
System.out.print (i+ "");
}
if (Count > 1) {break
;
}
}
}}
2: Square Root:
Package test.ms;
public class Prime {public
static void Main (string[] args) {for
(int j = 2; j<1000; j + +) {
if (m (j)) {
Sy Stem.out.print (j+ "");
}} public static Boolean m (int num) {for
(int j = 2; j<=math.sqrt (num); j + +) {
if (num%j = = 0) {return
false;
}
}
return true;
}
3: Find the Law (excerpt from a discussion forum)
The smallest prime number is 2, which is also the only even number in the prime, and the other primes are odd. There are infinitely many primes, so there is no maximum prime number.
Package test.ms;
Import java.util.ArrayList;
Import java.util.List; public class Primes {public static void main (string[] args) {//prime list<integer> Primes = g
Etprimes (1000);
Output for (int i = 0; i < primes.size (); i++) {Integer prime = Primes.get (i);
System.out.printf ("%8d", Prime);
if (i% = = 9) {System.out.println (); }}/** * Find all primes within n * * @param n Range * * * @return N all primes/private static L
ist<integer> getprimes (int n) {list<integer> result = new arraylist<integer> ();
Result.add (2);
for (int i = 3; I <= n; i + 2) {if (!divisible (i, result)) {result.add (i);
} return result; /** * Determine whether n can be divisible * @param n the number to be judged * @param primes a list of prime numbers * * @return if n can be primes
Returns true if any one of the integers is divisible. */private static Boolean divisible (int n, List<integer> primes) {for (Integer prime:primes) {if (n% prime = = 0) {return true;
return false;
}
}
The first and second are simple methods:
The third method illustrates the characteristic of a prime number: only 2 of all prime numbers are even.
If a number can be divisible by its previous prime number, then it is not prime.