27 【程序 27 求素数】
题目:求 100 之内的素数
package cskaoyan; public class cskaoyan27 { @org.junit.Test public void prime() { int mix = 1; int max = 100; for (int i = mix; i <= max; i++) { if (isPrime(i)) { System.out.println(i); } } } private boolean isPrime(int number) { boolean flag = true; if (number < 2) { flag = false; } else if (number == 2) { flag = true; } else { for (int i = 2; i <= Math.sqrt(number); i++) { if (number % i == 0) { flag = false; break; } } } return flag; } }
原文地址:https://www.cnblogs.com/denggelin/p/11403380.html
时间: 2024-10-03 11:44:29