Greatest common divisor: Difference between revisions
Content added Content deleted
m (→{{header|J}}) |
(→{{header|D}}: remove silly solution) |
||
Line 437: | Line 437: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>bool isEven(int number) |
|||
{ |
|||
return number%2 == 0; |
|||
} |
|||
int gcd(int a, int b) |
|||
{ |
|||
if(a == 0) |
|||
return b; |
|||
else if(b == 0) |
|||
return a; |
|||
else if(isEven(a) && isEven(b)) |
|||
return gcd(a>>1, b>>1) << 1; |
|||
else if(isEven(a) && !isEven(b)) |
|||
return gcd(a>>1, b); |
|||
else if(!isEven(a) && isEven(b)) |
|||
return gcd(a, b>>1); |
|||
else if(a >= b) |
|||
return gcd(a-b, b); |
|||
return gcd(a, b-a); |
|||
}</lang> |
|||
===Recursive=== |
|||
<lang d>long gcd(long x, long y) { |
<lang d>long gcd(long x, long y) { |
||
if (y == 0) |
if (y == 0) |
||
Line 467: | Line 442: | ||
return gcd(y, x%y); |
return gcd(y, x%y); |
||
}</lang> |
}</lang> |
||
=={{header|Dc}}== |
=={{header|Dc}}== |