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

(새 문서: 분류: 수학 ==PHP== {{참고|PHP gcd()}} <source lang='php'> $gcd = gmp_gcd("12", "21"); echo $gcd . "\n"; # 3 var_dump($gcd); # object(GMP)#1 (1) { # ["num"]=> # string(1)...)
 
태그: 수동 되돌리기
 
(사용자 2명의 중간 판 27개는 보이지 않습니다)
1번째 줄: 1번째 줄:
[[분류: 수학]]
==개요==
[[분류: 최대공약수]]
;함수 gcd()
 
==같이 보기==
* [[최대공약수]]
* [[함수 lcm()]]
* [[유클리드 호제법]]
 
==C++==
{{참고|C++ gcd()}}
<syntaxhighlight lang='cpp' run>
#include <iostream>
#include <numeric>
using namespace std;
 
int main() {
    cout << gcd(12, 21) << endl; // 3
    cout << lcm(20, 8) << endl; // 40
}
</syntaxhighlight>
 
==Java==
[[분류: Java]]
{{참고|자바 gcd()}}
<syntaxhighlight lang='java' run>
import java.math.BigInteger;
public class MyClass {
    private static int gcd(int a, int b) {
        return BigInteger.valueOf(a).gcd(BigInteger .valueOf(b)).intValue();
    }
    public static void main(String args[]) {
        System.out.println( gcd(12,21) ); // 3
        System.out.println( gcd(20,8) ); // 40
    }
}
</syntaxhighlight>
 
==PHP==
==PHP==
[[분류: PHP]]
{{참고|PHP gcd()}}
{{참고|PHP gcd()}}
<source lang='php'>
 
$gcd = gmp_gcd("12", "21");
{{소스헤더|gmp_gcd() 사용}}
echo $gcd . "\n";
<syntaxhighlight lang='php'>
function gcd($a,$b) { return gmp_intval(gmp_gcd($a,$b)); }
var_dump( gcd(12,21) );
# int(3)
</syntaxhighlight>
<syntaxhighlight lang='php'>
var_dump( gmp_intval(gmp_gcd(12, 21)) );
# int(3)
</syntaxhighlight>
<syntaxhighlight lang='php'>
echo gmp_gcd(12, 21) . "\n";
# 3
# 3
var_dump($gcd);
</syntaxhighlight>
<syntaxhighlight lang='php'>
var_dump( gmp_gcd(12, 21) );
# object(GMP)#1 (1) {
# object(GMP)#1 (1) {
#  ["num"]=>
#  ["num"]=>
#  string(1) "3"
#  string(1) "3"
# }
# }
</source>
</syntaxhighlight>
{{소스헤더|구현}}
<syntaxhighlight lang='php' run>
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)
</syntaxhighlight>
<syntaxhighlight lang='php' run>
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)
</syntaxhighlight>


==같이 보기==
==Python==
* [[최대공약수]]
[[분류: Python]]
{{참고|Python gcd()}}
<syntaxhighlight lang='Python' run>
from math import gcd
print( gcd(12,21) ) # 3
print( gcd(20,8) )  # 4
</syntaxhighlight>
<syntaxhighlight lang='Python' run>
def gcd(a,b):
    while b > 0:
        a, b = b, a % b
    return a
 
print( gcd(12,21) ) # 3
print( gcd(20,8) )  # 4
</syntaxhighlight>

2023년 10월 19일 (목) 16:51 기준 최신판

1 개요[ | ]

함수 gcd()

2 같이 보기[ | ]

3 C++[ | ]

#include <iostream>
#include <numeric>
using namespace std;

int main() {
    cout << gcd(12, 21) << endl; // 3
    cout << lcm(20, 8) << endl; // 40
}

4 Java[ | ]

import java.math.BigInteger;
public class MyClass {
    private static int gcd(int a, int b) {
        return BigInteger.valueOf(a).gcd(BigInteger .valueOf(b)).intValue();
    }
    public static void main(String args[]) {
        System.out.println( gcd(12,21) ); // 3
        System.out.println( gcd(20,8) ); // 40
    }
}

5 PHP[ | ]

gmp_gcd() 사용
function gcd($a,$b) { return gmp_intval(gmp_gcd($a,$b)); }
var_dump( gcd(12,21) );
# int(3)
var_dump( gmp_intval(gmp_gcd(12, 21)) );
# int(3)
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)

6 Python[ | ]

from math 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
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}