襄阳网站seo,微信小程序做一个多少钱,网页设计学徒培训招生,wordpress手机版登录【概述】 素数#xff1a;只能被1和自己整除。 素数的判断方法#xff1a; 我们把非素数都写成 ab 的形式#xff0c;如#xff1a; 16 116 16 28 24 124 24 212 24 38 24 46 同样#xff0c;我们发现#xff0c;a 和 b 其中一定会有一个数字 根号n#xff0…【概述】 素数只能被1和自己整除。 素数的判断方法 我们把非素数都写成 a×b 的形式如 16 1×16 16 2×8 24 1×24 24 2×12 24 3×8 24 4×6 同样我们发现a 和 b 其中一定会有一个数字 根号n比方法2的范围又缩小了。 在代码中用 Math.sqrt() 表示根号 ———————————————— 判断一个数是否为素数在我之前写的那篇有3种方法的详解在此附上原文连接 原文链接https://blog.csdn.net/T2283380276/article/details/134723172 【代码】
package practice;import java.util.Scanner;/*** Author : tipper* Description :求1-100中所有的素数*/
public class P9 {public static void main(String[] args) {Scanner scanner new Scanner(System.in);int n scanner.nextInt(); //输出 1-n内所有素数for (int i 1; i n; i) {int j 2;for (; j Math.sqrt(i); j) {if (i % j 0) {break;}}if (j Math.sqrt(i)) {System.out.println(i 是素数);}}}
}
【输出结果】