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

잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
2번째 줄: 2번째 줄:
;Java isPrime()
;Java isPrime()
;자바 isPrime()
;자바 isPrime()
<source lang='java'>
<syntaxhighlight lang='java'>
public class Main {
public class Main {
static boolean isPrime(int n) {
static boolean isPrime(int n) {
12번째 줄: 12번째 줄:
}
}
}
}
</source>
</syntaxhighlight>
<source lang='java'>
<syntaxhighlight lang='java'>
public class MyClass {
public class MyClass {
     static boolean isPrime(int n) {
     static boolean isPrime(int n) {
26번째 줄: 26번째 줄:
     }
     }
}
}
</source>
</syntaxhighlight>
<source lang='java'>
<syntaxhighlight lang='java'>
public class MyClass {
public class MyClass {
     static boolean isPrime(int n) {
     static boolean isPrime(int n) {
41번째 줄: 41번째 줄:
     }
     }
}
}
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2020년 11월 2일 (월) 02:49 판

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 }}