Created
May 11, 2016 19:02
-
-
Save SonoSooS/16d84a9e2ce60bca675926177b9453ae to your computer and use it in GitHub Desktop.
Very plain LargestCommonMultiple calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
typedef unsigned long long u64; | |
u64 lcm(u64 f, u64 s) | |
{ | |
u64 n1, n2; | |
if(f > s) { n1 = f; n2 = s; } | |
else { n1 = s; n2 = f; } | |
u64 i; | |
for(i = 1; i <= n2; i++) if(!((n1 * i) % n2)) return n1 * i; | |
return n2; | |
} | |
u64 parseU(char* wat) | |
{ | |
u64 n = 0; | |
u64 m = 1; | |
char* s = wat; | |
while(*wat) wat++; | |
while(wat != s) { n += (*(--wat) - '0') * m; m *= 10; } | |
return n; | |
} | |
int main(int argc, char** argv) | |
{ | |
if(argc < 3) | |
{ | |
puts("Error: at least 2 args are required!"); | |
return 1; | |
} | |
u64 n = parseU(argv[1]); | |
int i = 2; | |
for(; i != argc; i++) n = lcm(n, parseU(argv[i])); | |
printf("Result: %llu\n", n); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment