<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: GCD code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 08:03:20 GMT</pubDate>
    <description>DZone Snippets: GCD code</description>
    <item>
      <title>GCD: Divide et Impera</title>
      <link>http://snippets.dzone.com/posts/show/4583</link>
      <description>Calculates the Greatest Common Denominator (GCD) of an array of ints using a Divide at Impera approach.&lt;br /&gt;&lt;br /&gt;Time: O(lg(n))&lt;br /&gt;Memory: O(n)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;int gcd(int a, int b) {&lt;br /&gt;	int r = a % b;&lt;br /&gt;	while (r) {&lt;br /&gt;		a = b;&lt;br /&gt;		b = r;&lt;br /&gt;		r = a % b;&lt;br /&gt;	}&lt;br /&gt;	return b;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int gcdv(int *v, int p, int r) {&lt;br /&gt;	if (r == p + 1) {&lt;br /&gt;		//printf("%d,%d: %d\n", p, r, gcd(v[p], v[r]));&lt;br /&gt;		return gcd(v[p], v[r]);&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	if (r == p) {&lt;br /&gt;		//printf("%d: %d\n", r, v[r]);&lt;br /&gt;		return v[p];&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	int q = (p + r) / 2;&lt;br /&gt;	//printf("%d,%d: %d\n", p, r, gcd(gcdv(v, p, q), gcdv(v, q + 1, r)));&lt;br /&gt;	return gcd(gcdv(v, p, q), gcdv(v, q + 1, r));&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main(int argc, char *argv[]) {&lt;br /&gt;	int v[] = {45, 60, 125, 345, 65, 9875, 4555};&lt;br /&gt;	&lt;br /&gt;	printf("GCD: %d\n", gcdv(v, 0, 6));&lt;br /&gt;	&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 28 Sep 2007 06:34:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4583</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>Greatest Common Denominator function</title>
      <link>http://snippets.dzone.com/posts/show/4255</link>
      <description>This function returns the greatest common denominator (GCD) of its arguments.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;int gcd(int x, int y) {&lt;br /&gt; int a, b;&lt;br /&gt; if (x&lt;y) {a = y; b = x; }&lt;br /&gt; else if (x&gt;y) {a = x; b = y; }&lt;br /&gt; else {return x; }&lt;br /&gt; do {&lt;br /&gt;  int r = a % b;&lt;br /&gt;  a = b;&lt;br /&gt;  b = r;&lt;br /&gt; } while (b != 0);&lt;br /&gt; return a;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 03:37:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4255</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
    <item>
      <title>GCD of two numbers.</title>
      <link>http://snippets.dzone.com/posts/show/2574</link>
      <description>// finds GCD of a and b using Euclidian algorithm&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public int GCD(int a, int b)&lt;br /&gt;{&lt;br /&gt;   if (b==0) return a;&lt;br /&gt;   return GCD(b,a%b);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 10 Sep 2006 03:28:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2574</guid>
      <author>kanishkkunal (Kanishk Kunal)</author>
    </item>
    <item>
      <title>LCM function</title>
      <link>http://snippets.dzone.com/posts/show/1091</link>
      <description>&lt;code&gt;&lt;br /&gt;    ; uses GCD function&lt;br /&gt;    lcm: func [&lt;br /&gt;        {Returns the least common multiple of m and n.}&lt;br /&gt;        m [integer!]&lt;br /&gt;        n [integer!]&lt;br /&gt;    ][&lt;br /&gt;        abs either zero? n [0] [(m * n) / gcd m n]&lt;br /&gt;    ]&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Jan 2006 05:24:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1091</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>GCD function</title>
      <link>http://snippets.dzone.com/posts/show/1090</link>
      <description>&lt;code&gt;&lt;br /&gt;    gcd: func [&lt;br /&gt;        {Returns the greatest common denominator of m and n.}&lt;br /&gt;        m [integer!]&lt;br /&gt;        n [integer!]&lt;br /&gt;    ][&lt;br /&gt;        ;Euclid's algorithm&lt;br /&gt;        abs either zero? n [0] [either zero? (m // n) [n] [gcd n (m // n)]]&lt;br /&gt;    ]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Jan 2006 05:23:33 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1090</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
  </channel>
</rss>
