File tree Expand file tree Collapse file tree 4 files changed +98
-0
lines changed Expand file tree Collapse file tree 4 files changed +98
-0
lines changed Original file line number Diff line number Diff line change 1+ public class LcmGcd {
2+
3+ // recursive function to return gcd of a and b
4+ private static int gcd (int a , int b ) {
5+ /*please see: https://en.wikipedia.org/wiki/Euclidean_algorithm*/
6+ if (b == 0 ) return a ;
7+ return gcd (b , a % b );
8+ }
9+
10+ // function to return LCM of two numbers
11+ private static int lcm (int a , int b ) {
12+ /*
13+ a * b = lcm(a, b) * gcd (a, b)
14+ lcm(a, b) = (a * b) / gcd(a, b)
15+ */
16+ return (a / gcd (a , b )) * b ;
17+ }
18+
19+ public static void main (String [] args ) {
20+ System .out .println ("lcm of 10, 35 is: " + lcm (10 , 35 ));
21+ System .out .println ("gcd of 10, 35 is: " + gcd (10 , 35 ));
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+
3+ // recursive function to return gcd of a and b
4+ int gcd (int a , int b ) {
5+ /*please see: https://en.wikipedia.org/wiki/Euclidean_algorithm*/
6+ if (b == 0 ) return a ;
7+ return gcd (b , a % b );
8+ }
9+
10+ // function to return LCM of two numbers
11+ int lcm (int a , int b ) {
12+ /*
13+ a * b = lcm(a, b) * gcd (a, b)
14+ lcm(a, b) = (a * b) / gcd(a, b)
15+ */
16+ return (a / gcd (a , b )) * b ;
17+ }
18+
19+ int main () {
20+ printf ("lcm of 10, 35 is: %d\n" , lcm (10 , 35 ));
21+ printf ("lcm of 10, 35 is: %d\n" , gcd (10 , 35 ));
22+ return 0 ;
23+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+
3+ using namespace std ;
4+
5+ // recursive function to return gcd of a and b
6+ int gcd (int a, int b) {
7+ /* please see: https://en.wikipedia.org/wiki/Euclidean_algorithm*/
8+ if (b == 0 ) return a;
9+ return gcd (b, a % b);
10+ }
11+
12+ // function to return LCM of two numbers
13+ int lcm (int a, int b) {
14+ /*
15+ a * b = lcm(a, b) * gcd (a, b)
16+ lcm(a, b) = (a * b) / gcd(a, b)
17+ */
18+ return (a / gcd (a, b)) * b;
19+ }
20+
21+ int main () {
22+ cout << " lcm of 10, 35 is: " << lcm (10 , 35 ) << " \n " ;
23+ cout << " gcd of 10, 35 is: " << gcd (10 , 35 ) << " \n " ;
24+ return 0 ;
25+ }
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import "fmt"
4+
5+ // recursive function to return gcd of a and b
6+ func gcd (a int , b int ) int {
7+ /*please see: https://en.wikipedia.org/wiki/Euclidean_algorithm*/
8+ if b == 0 {
9+ return a
10+ }
11+ return gcd (b , a % b )
12+ }
13+
14+ // function to return LCM of two numbers
15+ func lcm (a int , b int ) int {
16+ /*
17+ a * b = lcm(a, b) * gcd (a, b)
18+ lcm(a, b) = (a * b) / gcd(a, b)
19+ */
20+ return (a / gcd (a , b )) * b
21+ }
22+
23+ func main () {
24+ fmt .Println ("lcm of 10, 35 is: " , lcm (10 , 35 ))
25+ fmt .Println ("lcm of 10, 35 is: " , gcd (10 , 35 ))
26+ }
27+
You can’t perform that action at this time.
0 commit comments