anyone know an algorithm to find the lcm of two numbers?
int lcm(int m, int n) { // TODO }
find the gcd(a,b) using the division alg
lcm(a,b)=a*b/gcd(a,b)
yeah: int gcd(int a, int b) { while (a % b != 0) { temp = a; a = b; b = a % temp; } return b; }
thanks
int lcm(int m, int n) { while cont { i = i + 1 if m*i == n*i break
int lcm(int a, int b) { return (a * b)/gcd(a, b); }
man am i ignorant. what language is that?
C
ah ok. in my next life...
in python it's even better: def gcd(a,b): while a%b: a,b = b, a%b return b lcm = lambda x,y: x*y/gcd(x,y)
I dislike the non-structured nature of Python. Fortran is where it is at, especially for us engineers.
Why do engineers like FORTRAN so much? I happened to see an open Chem. E. textbook and it's full of FORTRAN code :-D
function lcm(a, b) return a / gcd(a, b) * b function gcd(a, b) if (b != 0) return gcd(b, a % b) else return a
Cause it was designed by us for us. It's original purpose was a high-level language for the purpose of numerical and scientific calculations. I mainly do all my computational work in MATLAB as it has great functions and objects already built in. It's also great at solving differential equations.
MATLAB is great for numerical computing :-D
so no one uses FORTRAN? all engineers use MATLAB or Octave
Nuclear Engineers take a FORTRAN class at Texas A&M. All other disciplines take a MATLAB class. I think MATLAB is displacing the use of FORTRAN. One of my older profs uses FORTRAN for everything, but as a student, we all use MATLAB.
Join our real-time social learning platform and learn together with your friends!