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

태그: 수동 되돌리기
 
(사용자 2명의 중간 판 19개는 보이지 않습니다)
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]]
{{참고|PHP gcd()}}
{{참고|PHP gcd()}}
<source lang='php'>
 
{{소스헤더|gmp_gcd() 사용}}
<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";
echo gmp_gcd(12, 21) . "\n";
# 3
# 3
</syntaxhighlight>
<syntaxhighlight lang='php'>
var_dump( gmp_gcd(12, 21) );
var_dump( gmp_gcd(12, 21) );
# object(GMP)#1 (1) {
# object(GMP)#1 (1) {
11번째 줄: 61번째 줄:
#  string(1) "3"
#  string(1) "3"
# }
# }
</source>
</syntaxhighlight>
<source lang='php'>
{{소스헤더|구현}}
<syntaxhighlight lang='php' run>
function gcd($a, $b) { return $b ? gcd($b, $a%$b) : $a; }
function gcd($a, $b) { return $b ? gcd($b, $a%$b) : $a; }
var_dump( gcd(12, 21) );
var_dump( gcd(12, 21) );
18번째 줄: 69번째 줄:
# int(3)
# int(3)
# int(4)
# int(4)
</source>
</syntaxhighlight>
<source lang='php'>
<syntaxhighlight lang='php' run>
function gcd($a, $b) { return ($a%$b) ? gcd($b,$a%$b) : $b; }
function gcd($a, $b) { return ($a%$b) ? gcd($b,$a%$b) : $b; }
var_dump( gcd(12, 21) );
var_dump( gcd(12, 21) );
25번째 줄: 76번째 줄:
# int(3)
# int(3)
# int(4)
# int(4)
</source>
</syntaxhighlight>


==Python==
==Python==
[[분류: Python]]
[[분류: Python]]
{{참고|Python gcd()}}
{{참고|Python gcd()}}
<source lang='Python'>
<syntaxhighlight lang='Python' run>
from fractions import gcd
from math import gcd
print( gcd(12,21) )
print( gcd(12,21) ) # 3
# 3
print( gcd(20,8) ) # 4
print( gcd(20,8) )
</syntaxhighlight>
# 4
<syntaxhighlight lang='Python' run>
</source>
<source lang='Python'>
def gcd(a,b):
def gcd(a,b):
     while b > 0:
     while b > 0:
43번째 줄: 92번째 줄:
     return a
     return a


print( gcd(12,21) )
print( gcd(12,21) ) # 3
# 3
print( gcd(20,8) ) # 4
print( gcd(20,8) )
</syntaxhighlight>
# 4
</source>
 
==같이 보기==
* [[최대공약수]]
* [[함수 lcm()]]

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 }}