"함수 is prime()"의 두 판 사이의 차이

5번째 줄: 5번째 줄:


==Python==
==Python==
{{참고|파이썬 is_prime()}}
[[category: Python]]
[[category: Python]]
<source lang='Python'>
<source lang='Python'>

2018년 7월 18일 (수) 19:54 판


함수 is_prime()

1 Python

def is_prime(x):
	if x < 2:
		return False
	for n in range(2, x-1):
		if x % n == 0:
			return False
	return True

for i in range(1,30):
	if is_prime(i):
		print( i, end=' ' )
# 2 3 5 7 11 13 17 19 23 29
def is_prime(n):
	if n < 2:
		return False
	if n < 4:
		return True
	if n%2==0 or n%3==0:
		return False
	i = 5
	while i**2 <= n:
		if n%i==0 or n%(i+2)==0:
			return False
		i = i + 6
	return True

for i in range(1,30):
	if is_prime(i):
		print( i, end=' ' )
# 2 3 5 7 11 13 17 19 23 29

2 같이 보기

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