package test; public class test { void printPrimes(int n){ int curPrime; int numPrimes; boolean isPrime; int [] primes = new int [43]; primes[0]=2; numPrimes = 1; curPrime = 2; while ( numPrimes < n) { curPrime ++ ; isPrime = true; for ( int i = 0 ; i <= numPrimes-1 ;i++){ if (isDivisible(primes[i],curPrime)) { isPrime = false; break ; } } if (isPrime){ primes[numPrimes] = curPrime ; numPrimes ++ ; } } for (int i = 0 ; i <= numPrimes -1 ; i++ ) { System.out.println("Prime: " + primes[i]); } } boolean isDivisible(int i, int curPrime) { if ( curPrime % i == 0 ) return true; return false; } }
package test; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; public class testTest { private test t= null; @Before public void setUp() throws Exception { t = new test (); } @Test public void test() { t.printPrimes(0); t.printPrimes(3); t.printPrimes(5); } }
时间: 2024-10-07 01:42:17