Skip to content

Instantly share code, notes, and snippets.

@mmstick
Last active August 29, 2015 14:12
Show Gist options
  • Save mmstick/079994ff16e3f2b85007 to your computer and use it in GitHub Desktop.
Save mmstick/079994ff16e3f2b85007 to your computer and use it in GitHub Desktop.
// C Implementation
#include <stdio.h>
// A recursive function that returns the GCD of two numbers.
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
int lcm(int x, int y)
{
return x * y / gcd(x, y);
}
int main()
{
printf("Enter two positive numbers to find the Lowest Common Multiple: ");
int a, b;
scanf("%d %d", &a, &b);
printf("The LCM of %d and %d is %d.\n", a, b, lcm(a, b));
}
// D implementation
import std.stdio;
// A recursive function that returns the GCD of two numbers.
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y); // Ternary Operator
}
void main()
{
auto lcm = (int x, int y) => (x * y) / gcd(x, y); // Lambda expression
write("Enter two positive numbers to find the Lowest Common Multiple: ");
int a, b;
readf("%d %d", &a, &b);
writef("The LCM of %d and %d is %d.\n", a, b, lcm(a, b));
}
// Go implementation
package main
import "fmt"
// gcd returns the GCD of two input numbers.
func gcd(x, y int) int {
for temp := 0; x != 0; y = temp {
temp = x
x = y % x
}
return y
}
func main() {
lcm := func(x, y int) int {
return x * y / gcd(x, y)
}
fmt.Print("Enter two positive numbers to find the Lowest Common Multiple: ")
var a, b int
fmt.Scanf("%d %d", &a, &b)
fmt.Printf("The LCM of %d and %d is %d.\n", a, b, lcm(a, b))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment