"파이썬 gcd()"의 두 판 사이의 차이

3번째 줄: 3번째 줄:
;파이썬 gcd()
;파이썬 gcd()


<source lang='Python'>
<source lang='Python' run>
from fractions import gcd
from math import gcd
print( gcd(20,8) )
print( gcd(20,8) ) # 4
# 4
print( gcd(12,21) ) # 3
print( gcd(12,21) )
# 3
</source>
</source>
<source lang='Python'>
<source lang='Python' run>
def gcd(a,b):
def gcd(a,b):
     while b > 0:
     while b > 0:
16번째 줄: 14번째 줄:
     return a
     return a


print( gcd(12,21) )
print( gcd(12,21) ) # 3
# 3
print( gcd(20,8) ) # 4
print( gcd(20,8) )
# 4
</source>
</source>



2020년 7월 12일 (일) 01:39 판

1 개요

Python gcd()
파이썬 gcd()
from math import gcd
print( gcd(20,8) ) # 4
print( gcd(12,21) ) # 3
def gcd(a,b):
    while b > 0:
        a, b = b, a % b
    return a

print( gcd(12,21) ) # 3
print( gcd(20,8) ) # 4

2 같이 보기

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