检测是否为n的因子 Exercise07_06

 1 /**
 2  * @author 冰樱梦
 3  * 时间:2018年下半年
 4  * 题目:检测是否为n的因子
 5  *
 6  */
 7 public class Exercise07_06 {
 8     public static void main(String[] args){
 9         final int NUMBER_OF_PRIMES=50;
10         final int NUMBER_OF_PRIMES_PER_LINE=10;
11         int count=0;
12         int number=2;
13         System.out.println("The first 50 prime numbers are 
");
14         
15         while(count<NUMBER_OF_PRIMES){
16             boolean isPrime=true;
17             
18             for(int divisor=2;divisor<=Math.pow(number, 0.5);divisor++){
19                 if(number % divisor==0){
20                     isPrime=false;
21                     break;
22                 }
23             }
24             if(isPrime){
25                 count++;
26                 
27                 if(count%NUMBER_OF_PRIMES_PER_LINE==0){
28                     System.out.println(number);
29                 }
30                 else
31                     System.out.print(number+" ");
32             }
33             number++;
34         }
35         
36     }
37 }
原文地址:https://www.cnblogs.com/cherrydream/p/10174050.html