GCD / LCM Polyglots!
C / C++, 79 78 73 bytes
Thanks to @ETHproductions for saving a byte!
int f(int a,int b){int c,d=a*b;for(;a;b=c)c=a,a=b%a;auto e=.5;c=e?d/b:b;}
C calculates the GCD: Try it online!
C++ calculates the LCM: Try it online!
In C, auto e=.5
declares an integer variable with the auto storage class (which is the default), which is then initialized to 0, whereas in C++11 it declares a double, which is initialized to 0.5. So the variable's value will be truthy in C++ and falsy in C.
The function calculates the GCD with Euclid's algorithm, and the LCM by dividing the product of a and b by the GCD.
Omitting the return statement works at least on GCC. The 78 byte solution below should work with any compiler:
int f(int a,int b){int c,d=a*b;for(;a;b=c)c=a,a=b%a;auto e=.5;return e?d/b:b;}
Jelly / Actually, 2 bytes
00000000: 1e 67 .g
This is a hexdump (xxd) of the submitted program. It cannot be tested online because TIO doesn't support the CP437 encoding. @Mego was kind enough to verify that this works on Cygwin, which implements CP437 as intended for Actually.
Jelly: GCD
Jelly uses the Jelly code page, so it sees the following characters.
œg
Try it online!
How it works
œ
is an incomplete token and thus ignored. g
is the GCD built-in.
Actually: LCM
Actually uses CP 437, so it sees the following characters.
▲g
Try it online!
How it works
▲
is the LCM input. Since g
(GCD) requires two integer inputs, it isn't executed.
Actually / Jelly, 3 bytes
00000000: 11 1c 67 ..g
This is a hexdump (xxd) of the submitted program.
Try it online!1
Actually: GCD
Actually uses CP 437, so it sees the following characters.
◄∟g
Try it online!
How it works
(implicit) Read a and b from STDIN and push them on the stack.
◄ Unassigned. Does nothing.
∟ Unassigned. Does nothing.
g Pop a and b and push gcd(a,b).
(implicit) Write the result to STDOUT.
Jelly: LCM
Jelly uses the Jelly code page, so it sees the following characters.
×÷g
Try it online!
How it works
×÷g Main link. Left argument: a. Right argument: b
× Multiply; yield ab.
g GCD; yield gcd(a,b).
÷ Division; yield ab/gcd(a,b) = lcm(a,b).
Note: The formula gcd(a,b)lcm(a,b) = ab holds because a and b are positive.
1 TIO actually uses UTF-8 for Actually. Since both the ASCII characters and the CP437 characters 0x11 and 0x1c are unassigned, the program works nevertheless.