"자바 isPrime()"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
 
2번째 줄: 2번째 줄:
;Java isPrime()
;Java isPrime()
;자바 isPrime()
;자바 isPrime()
<syntaxhighlight lang='java'>
<syntaxhighlight lang='java' run>
public class Main {
public class Main {
static boolean isPrime(int n) {
static boolean isPrime(int n) {
8번째 줄: 8번째 줄:
}
}
public static void main(String args[]) {
public static void main(String args[]) {
for(int i=0; i<30; i++) if(isPrime(i)) System.out.printf("%d ",i);
for(int i=0; i<30; i++) if(isPrime(i)) System.out.printf("%d ",i); // 2 3 5 7 11 13 17 19 23 29
// 2 3 5 7 11 13 17 19 23 29
}
}
}
}
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang='java'>
<syntaxhighlight lang='java' run>
public class MyClass {
public class MyClass {
     static boolean isPrime(int n) {
     static boolean isPrime(int n) {
22번째 줄: 21번째 줄:
     }
     }
     public static void main(String args[]) {
     public static void main(String args[]) {
         for(int i=0; i<30; i++) if(isPrime(i)) System.out.printf("%d ",i);
         for(int i=0; i<30; i++) if(isPrime(i)) System.out.printf("%d ",i); // 2 3 5 7 11 13 17 19 23 29  
        // 2 3 5 7 11 13 17 19 23 29  
     }
     }
}
}
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang='java'>
<syntaxhighlight lang='java' run>
public class MyClass {
public class MyClass {
     static boolean isPrime(int n) {
     static boolean isPrime(int n) {
37번째 줄: 35번째 줄:
     }
     }
     public static void main(String args[]) {
     public static void main(String args[]) {
         for(int i=0; i<30; i++) if(isPrime(i)) System.out.printf("%d ",i);
         for(int i=0; i<30; i++) if(isPrime(i)) System.out.printf("%d ",i); // 2 3 5 7 11 13 17 19 23 29  
        // 2 3 5 7 11 13 17 19 23 29  
     }
     }
}
}

2021년 4월 20일 (화) 21:46 기준 최신판

1 개요[ | ]

Java isPrime()
자바 isPrime()
public class Main {
	static boolean isPrime(int n) {
		return (new java.math.BigInteger(String.valueOf(n))).isProbablePrime(100);
	}
	public static void main(String args[]) {
		for(int i=0; i<30; i++) if(isPrime(i)) System.out.printf("%d ",i); // 2 3 5 7 11 13 17 19 23 29
	}
}
public class MyClass {
    static boolean isPrime(int n) {
        if( n<2 ) return false;
        for( int i=2; i<=(int)Math.sqrt(n); i++)
            if( n%i == 0) return false;
        return true;
    }
    public static void main(String args[]) {
        for(int i=0; i<30; i++) if(isPrime(i)) System.out.printf("%d ",i); // 2 3 5 7 11 13 17 19 23 29 
    }
}
public class MyClass {
    static boolean isPrime(int n) {
        if( n < 2 ) return false;
        if( n < 4 ) return true;
        if( n%2==0 || n%3==0 ) return false;
        for(int i=5; i*i<=n; i+=6 ) if(n%i==0 || n%(i+2)==0) return false;
        return true;
    }
    public static void main(String args[]) {
        for(int i=0; i<30; i++) if(isPrime(i)) System.out.printf("%d ",i); // 2 3 5 7 11 13 17 19 23 29 
    }
}

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}