View Single Post
Old Apr 8th, 2006, 8:22 PM   #6
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Quote:
Originally Posted by andro
The Euclidean algorithm is recursive, not to mention the same method he is using
Hehe true.
By the way, that binary gcd algorithm is not as efficient as Euclidean. I implemented both algorithms and ran for N=10000:
Quote:
ali@asus:~$ time ./eucmath
1593 1669
real 12m5.579s
user 12m0.625s
sys 0m1.936s

ali@asus:~$ time ./binmath
1593 1669
real 15m47.258s
user 12m43.344s
sys 0m1.469s
#include <iostream>

struct Triple{long x,y,z;};


long gcd(long a, long b)						 //Eulidean Algorithm
{
	if (a < b)
		gcd(b,a);
	if (b == 0)
		return a;
	return gcd(b,a%b);
}

int main()
{
	long N = 10000;
	long prime_triples_count = 0, triples_count = 0, p_count = 0;
	
	Triple sols[50000];

	for (long z = 1; z <= N; z++)					 
	{		
		for (long y = 1; y < z; y++)
		{			
			for (long x = 1; x < y; x++)
		 {			 
			 if (z*z == (y*y) + (x*x))			 
			 {						 		
				    sols[triples_count].x=x; 
					sols[triples_count].y=y; 
					sols[triples_count].z=z;
				 triples_count++;													 
				 if (gcd(x,y) == 1 && gcd(x,z) == 1 && gcd(y,z) == 1)
					 prime_triples_count++;										 
				}
			}
		}
	}

	for (long p = 1; p <= N; p++)		 
	{		
		long i;
		for (i = 0; i < triples_count; i++)	
	 {				 		
			if (p == sols[i].x || p == sols[i].y || p == sols[i].z)
				break;
		}		
		if (i >= triples_count)		   
		{
			p_count++;		
		}
	}
	
	std::cout<<prime_triples_count<<&quot; &quot;<<p_count<<&quot;\n&quot;;	 
			
	return 0;
}
OpenLoop is offline   Reply With Quote