如何求小于该数的质数
从系统中读取一个文件 文件中有数字和字符,他们是用逗号分开的,把读取到的东西进行判断,如果是数字计算出小于该数的质数并把它输出,如果不是数字就输出它不是数字。求各位帮忙,给个代码!万分感激了
[解决办法]
public class Primes{ public static void main(String args[]) { int maxValue = 50; // The maximum value to be checked. // Check all values from 2 to maxValue: OuterLoop: for (int i = 2; i <= maxValue; i++) { // Try dividing by all integers from 2 to square root of i: for (int j = 2; j <= Math.sqrt(i); j++) { if (i % j == 0) // This is true if j divides exactly continue OuterLoop; // so exit the loop. } // We only get here if we have a prime: System.out.println(i); // so output the value. } }}
[解决办法]