<p>Does anyone know a mechanism to calculate at compile-time the LCM (Least Common Multiple) and/or GCD (Greatest Common Denominator) of at least two number in <strong>C</strong> (<strong>not C++</strong>, I know that template magic is available there)?</p>

<p>I generally use <strong>GCC</strong> and recall that it can calculate certain values at compile-time when all inputs are known (ex: sin, cos, etc...).</p>

<p>I'm looking for how to do this in <strong>GCC</strong> (preferably in a manner that other compilers could handle) and hope the same mechanism would work in Visual Studio.</p>

[{'answer_id': 76746, 'author': 'Kevin', 'author_id': 6386, 'author_profile': 'https://Stackoverflow.com/users/6386', 'pm_score': 3, 'selected': False, 'text': '<p>I figured it out afterall...</p>\n\n<pre><code>#define GCD(a,b) ((a&gt;=b)*GCD_1(a,b)+(a&lt;b)*GCD_1(b,a))\n#define GCD_1(a,b) ((((!(b)))*(a)) + (!!(b))*GCD_2((b), (a)%((b)+!(b))))\n#define GCD_2(a,b) ((((!(b)))*(a)) + (!!(b))*GCD_3((b), (a)%((b)+!(b))))\n#define GCD_3(a,b) ((((!(b)))*(a)) + (!!(b))*GCD_4((b), (a)%((b)+!(b))))\n#define GCD_4(a,b) ((((!(b)))*(a)) + (!!(b))*GCD_5((b), (a)%((b)+!(b))))\n#define GCD_5(a,b) ((((!(b)))*(a)) + (!!(b))*GCD_6((b), (a)%((b)+!(b))))\n#define GCD_6(a,b) ((((!(b)))*(a)) + (!!(b))*GCD_7((b), (a)%((b)+!(b))))\n#define GCD_7(a,b) ((((!(b)))*(a)) + (!!(b))*GCD_8((b), (a)%((b)+!(b))))\n#define GCD_8(a,b) ((((!(b)))*(a)) + (!!(b))*GCD_last((b), (a)%((b)+!(b))))\n#define GCD_last(a,b) (a)\n\n#define LCM(a,b) (((a)*(b))/GCD(a,b))\n\n\nint main()\n{\n    printf("%d, %d\\n", GCD(21,6), LCM(21,6));\n    return 0;\n}\n</code></pre>\n\n<p>Note, depending on how large your integers go, you may need to include more intermediate steps (i.e. GCD_9, GCD_10, etc...).</p>\n\n<p>I hope this helps!</p>\n'}, {'answer_id': 77708, 'author': 'Kevin Loney', 'author_id': 13834, 'author_profile': 'https://Stackoverflow.com/users/13834', 'pm_score': 1, 'selected': False, 'text': '<p>I realize your only interested in a C implementation but I thought I\'d comment on C++ and template metaprogramming anyway. I\'m not completely convinced that it is possible in C++ as you need well defined initial conditions in order to terminate the recursive expansion.</p>\n\n<pre><code>template&lt;int A, int B&gt;\nstruct GCD {\n    enum { value = GCD&lt;B, A % B&gt;::value };\n};\n\n/*\nBecause GCD terminates when only one of the values is zero it is impossible to define a base condition to satisfy all GCD&lt;N, 0&gt;::value conditions\n*/\ntemplate&lt;&gt;\nstruct GCD&lt;A, 0&gt; { // This is obviously not legal\n    enum { value = A };\n};\n\nint main(void)\n{\n    ::printf("gcd(%d, %d) = %d", 7, 35, GCD&lt;7, 35&gt;::value);\n}\n</code></pre>\n\n<p>This may be possible with C++0x however not %100 certain though.</p>\n'}, {'answer_id': 78794, 'author': 'harningt', 'author_id': 12713, 'author_profile': 'https://Stackoverflow.com/users/12713', 'pm_score': 2, 'selected': False, 'text': "<p>Partly based on Kevin's answer, here's a macro-sequence that has compile-time failure for constant-values and run-time errors otherwise.</p>\n\n<p>It could also be configured to pull in a non-compile time function if failure is not an option.</p>\n\n<pre><code>#define GCD(a,b) ( ((a) &gt; (b)) ? ( GCD_1((a), (b)) ) : ( GCD_1((b), (a)) ) )\n\n#define GCD_1(a,b) ( ((b) == 0) ? (a) : GCD_2((b), (a) % (b) ) )\n#define GCD_2(a,b) ( ((b) == 0) ? (a) : GCD_3((b), (a) % (b) ) )\n#define GCD_3(a,b) ( ((b) == 0) ? (a) : GCD_4((b), (a) % (b) ) )\n#define GCD_4(a,b) ( ((b) == 0) ? (a) : GCD_5((b), (a) % (b) ) )\n#define GCD_5(a,b) ( ((b) == 0) ? (a) : GCD_6((b), (a) % (b) ) )\n#define GCD_6(a,b) ( ((b) == 0) ? (a) : GCD_7((b), (a) % (b) ) )\n#define GCD_7(a,b) ( ((b) == 0) ? (a) : GCD_8((b), (a) % (b) ) )\n#define GCD_8(a,b) ( ((b) == 0) ? (a) : GCD_9((b), (a) % (b) ) )\n#define GCD_9(a,b) (assert(0),-1)\n</code></pre>\n\n<p>Beware expanding this too large, even if it would terminate early, since the compiler has to fully plug in everything before even evaluating.</p>\n"}, {'answer_id': 70047263, 'author': 'thisismyhomeworkaccount', 'author_id': 17464920, 'author_profile': 'https://Stackoverflow.com/users/17464920', 'pm_score': -1, 'selected': False, 'text': '<pre><code> int gcd(int n1,int n2){\n       while(n1!=n2){\n        if(n1 &gt; n2) n1 -= n2;\n        else n2 -= n1;\n    }\n    return n1;\n}\nint lcm(int n1, int n2){\n    int total =n1*n2;\n    return total/gcd(n1,n2);\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/76334', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12713/']