"함수 gcd()"의 두 판 사이의 차이

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


==같이 보기==
==같이 보기==
* [[최대공약수]]
* [[최대공약수]]

2018년 7월 14일 (토) 21:04 판

1 PHP

echo gmp_gcd(12, 21) . "\n";
# 3
var_dump( gmp_gcd(12, 21) );
# object(GMP)#1 (1) {
#   ["num"]=>
#   string(1) "3"
# }
function gcd($a, $b) { return $b ? gcd($b, $a%$b) : $a; }
var_dump( gcd(12, 21) );
var_dump( gcd(20, 8) );
# int(3)
# int(4)
function gcd($a, $b) { return ($a%$b) ? gcd($b,$a%$b) : $b; }
var_dump( gcd(12, 21) );
var_dump( gcd(20, 8) );
# int(3)
# int(4)

2 Python

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

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

3 같이 보기

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