함수 gcd()

Jmnote (토론 | 기여)님의 2018년 7월 14일 (토) 21:01 판 (→‎PHP)

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(20,8) )
# 4
print( gcd(12,21) )
# 3

3 같이 보기

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