codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
/* find what is the smallest number that is evenly divisible by all of the numbers from 1 to 20?. using LCM formula refer to http://en.wikipedia.org/wiki/Least_common_multiple */ #include <iostream> using namespace std; int lcm(int a,int b,int c = 0) { if (a%b == 0) { return a; } c = (c == 0) ? a+a : c+a; return (c%b == 0)? c : lcm(a,b,c); } int main() { int i,x=2,total; for (i = 3;i<=20;i++) { cout << "lcm(" << x << ","<<i<<") = " ; x = lcm(x,i); cout << x << endl; } }
Private
[
?
]
Run code