Update PrimeNumbers.java
This commit is contained in:
1 parent
7275bdb0e8
commit
d42322b89f
1 file changed
+19
-22
@@ -1,36 +1,33 @@
|
|||||||
package finalexam.StandardIO;
|
package finalexam.StandardIO;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class PrimeNumbers {
|
public class PrimeNumbers {
|
||||||
private static boolean isPrime(int n) {
|
|
||||||
if (n <= 1)
|
|
||||||
return false;
|
|
||||||
for (int i = 2; i <= Math.sqrt(n); i++)
|
|
||||||
if (n % i == 0)
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Scanner sc = new Scanner(System.in);
|
Scanner sc = new Scanner(System.in);
|
||||||
int i = sc.nextInt();
|
int n = sc.nextInt();
|
||||||
int ans = 0;
|
ArrayList<Integer> primes = new ArrayList<>();
|
||||||
List<Integer> li = new ArrayList<Integer>();
|
for (int i = 2; i < n; i++) {
|
||||||
for (int j = 2; j < i; j++) {
|
if (isPrime(i))
|
||||||
if (isPrime(j)) {
|
primes.add(i);
|
||||||
ans++;
|
|
||||||
li.add(j);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
System.out.print(ans + "(分别是");
|
System.out.print(primes.size() + "(分别是");
|
||||||
for (int j = 0; j < li.size(); j++) {
|
for (int i = 0; i < primes.size(); i++) {
|
||||||
System.out.print(li.get(j));
|
System.out.print(primes.get(i));
|
||||||
if (j != li.size() - 1)
|
if (i != primes.size() - 1)
|
||||||
System.out.print(",");
|
System.out.print(",");
|
||||||
}
|
}
|
||||||
System.out.println(")");
|
System.out.println(")");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static boolean isPrime(int n) {
|
||||||
|
if (n <= 1)
|
||||||
|
return false;
|
||||||
|
for (int i = 2; i <= Math.sqrt(n); i++) {
|
||||||
|
if (n % i == 0)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user