1: public class Math2 {
2: public static int square( int n ) {
3: return n*n;
4: }
5:
6: public static int cube( int n ) {
7: return n*n*n;
8: }
9:
10: public static int toThePower( int n, int power ) {
11: int result=n;
12: for( int i=0; i<power; i++ ) {
13: result *= n;
14: }
15: return result;
16: }
17:
18: public static int factorial( int n ) {
19: int result = n;
20: for( int i=n-1; i>0; i- ) {
21: result *= i;
22: }
23: return result;
24: }
25:
26: public static boolean isPrime( int n ) {
27: // 这里怎么编写???
28: return false;
29: }
30:
31: public static void showNumber( String operation, int n ) {
32: System.out.println( "The result of the " + operation + " is " + n );
33: }
34:
35: public static void main( String[] args ) {
36: int a = 5;
37: showNumber( "Square", square( a ) );
38: showNumber( "Cube", cube( a ) );
39: showNumber( "To The Power", toThePower( a, 4 ) );
40: showNumber( "Factorial", factorial( a ) );
41: }
42: }
2: public static int square( int n ) {
3: return n*n;
4: }
5:
6: public static int cube( int n ) {
7: return n*n*n;
8: }
9:
10: public static int toThePower( int n, int power ) {
11: int result=n;
12: for( int i=0; i<power; i++ ) {
13: result *= n;
14: }
15: return result;
16: }
17:
18: public static int factorial( int n ) {
19: int result = n;
20: for( int i=n-1; i>0; i- ) {
21: result *= i;
22: }
23: return result;
24: }
25:
26: public static boolean isPrime( int n ) {
27: // 这里怎么编写???
28: return false;
29: }
30:
31: public static void showNumber( String operation, int n ) {
32: System.out.println( "The result of the " + operation + " is " + n );
33: }
34:
35: public static void main( String[] args ) {
36: int a = 5;
37: showNumber( "Square", square( a ) );
38: showNumber( "Cube", cube( a ) );
39: showNumber( "To The Power", toThePower( a, 4 ) );
40: showNumber( "Factorial", factorial( a ) );
41: }
42: }
原来就是判断一个数是否为素数..
你单拿出来问不就完了么..里面这么写
public static boolean isPrime(int n){
for(i=2;i<n;i++)
if(n%i==0)return false;
return true;
}
首先判断该值是否是2,如果不是,再判断,就是1楼的算法,改进下:public static boolean isPrime(int n){
for(i=2;i <n/2;i++)
if(n%i==0)
return false;
return true;
}
for(int i = 2 ; i * i <= n ; i ++ ){
if( n % i == 0){
return false;
}
}
return true;
}
public static boolean isPrime(int n) {
int primes55[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,
73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,
181,191,193,197,199,211,223,227,229,233,239,241,251,257};
for(int i=0;i<55;i++) {
if (n%primes55[i] == 0) {
if (n == primes55[i]) {
return true;
}
else {
return false;
}
}
}
int maxtest = n/4;
for(int i=259; i<maxtest; i+=2)
if (n%i == 0)
return false;
return true;
}
public static boolean isPrime(int n) {
if(n < 2)
return false; int primes55[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,
73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,
181,191,193,197,199,211,223,227,229,233,239,241,251,257};
for(int i=0;i<55;i++) {
if (n%primes55[i] == 0) {
if (n == primes55[i]) {
return true;
}
else {
return false;
}
}
}
int maxtest = (int)Math.sqrt(n);
for(int i=259; i<maxtest; i+=2)
if (n%i == 0)
return false;
return true;
}