Tilted Forum Project - TFP - Sexuality, Philosophy and Political Discussion

Go Back   Tilted Forum Project - TFP - Sexuality, Philosophy and Political Discussion > Interests > Tilted Technology

Reply
 
LinkBack Thread Tools
Old 04-26-2004, 01:31 PM   #1 (permalink)
Rookie
 
Join Date: May 2003
[c++] Euclidean Algorithm

Hello!
Does anyone have any C++ source code that uses a simple euclidean algorithm to calculate the GCD of two integers? I tried searching but couldn't turn up what I was looking for...

Thanks,
Tom
TSerra is offline   Reply With Quote
Old 04-26-2004, 01:39 PM   #2 (permalink)
 
KnifeMissile's Avatar
 
Join Date: May 2003
Location: Waterloo, Ontario
Do you just not know the various algorithms for finding the greatest common divisor or can't you just implement them, yourself? They're pretty simple and shouldn't take you long to do...
KnifeMissile is offline   Reply With Quote
Old 04-26-2004, 01:48 PM   #3 (permalink)
Rookie
 
Join Date: May 2003
I have the technical aspects of the Euclidean, but I'm not sure how to translate the definition from a math textbook into c++ code.

I just thought there might be a pre-written c++ function out there that I could stick into a program and take advantage of.

Thanks,
Tom
TSerra is offline   Reply With Quote
Old 04-26-2004, 02:15 PM   #4 (permalink)
Rookie
 
Join Date: May 2003
You're right, once I took the time to sit down and think about it, I came up with this, which seems to work alright.

Thanks,
Tom

Code:
int gcd(int a, int b) {
int remainder;

while (remainder!=0) {
remainder = a % b;
a = b;
b = remainder;
}

return a;
}
TSerra is offline   Reply With Quote
Old 04-26-2004, 02:26 PM   #5 (permalink)
 
KnifeMissile's Avatar
 
Join Date: May 2003
Location: Waterloo, Ontario
Good for you! There's a small bug in your code (remainder is uninitialized) but, otherwise, it should work... You can either initialize your variables, change your while() loop into a do{}while() loop, or both...

I was about to suggest this:
Code:
unsigned int GCD( unsigned int a, unsigned int b) {&#10	return b == 0 ? a : GCD( b, a % b);&#10}
It's needlessly recursive, so I would say that your implementation is better (and that you should stick with it!) but mine looks more fun...
KnifeMissile is offline   Reply With Quote
Old 04-26-2004, 02:48 PM   #6 (permalink)
 
KnifeMissile's Avatar
 
Join Date: May 2003
Location: Waterloo, Ontario
Actually, now that I look at it, both our implementations assume that b > a, which is lame. You shouldn't have to care which order you pass your integers to a GCD function but I'm sure you can fix that, while you're there...
KnifeMissile is offline   Reply With Quote
Old 04-26-2004, 03:02 PM   #7 (permalink)
Wehret Den Anfängen!
 
Join Date: Nov 2003
Location: Ontario, Canada
Knife, actually they don't.
If b>a, his GCD code (and I assume yours) swaps a and b in the first iteration, and goes on from there.
__________________
Last edited by JHVH : 10-29-4004 BC at 09:00 PM. Reason: Time for a rest.
Yakk is offline   Reply With Quote
Old 04-26-2004, 03:09 PM   #8 (permalink)
 
KnifeMissile's Avatar
 
Join Date: May 2003
Location: Waterloo, Ontario
I think you're right. For some reason, I thought that the modulo operator might do something weird if modulus was greater than the numerator but, in retrospect, it should all be fine...
KnifeMissile is offline   Reply With Quote
Reply

Bookmarks

Tags
algorithm, euclidean

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -7. The time now is 06:35 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
All text (c) 2002-2008 Tilted Forum Project
"Insignia" vBulletin 3.5 - b6gm6n - x7x7x7.com