473,806 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Highest Common Factor


Anyone want to give me a hand to write efficient algorithms for Highest
Common Factor and Lowest Common Multiple? This is what I have so far...

Caveat: I haven't gone over the following code in detail, so there might be
a subtle bug lurking somewhere.

unsigned HCF(unsigned const a, unsigned const b)
{
unsigned bigger,smaller;

register unsigned hcf;

if (a>b) bigger=a,smalle r=b;
else bigger=b,smalle r=a;

if ( !(bigger%smalle r) ) return smaller;

hcf = smaller-1;

while (a%hcf || b%hcf) --hcf;

return hcf;
}

Any ideas to make it more efficient?

Here's what I've got for Lowest Common Multiple:

unsigned LCM(unsigned const a,unsigned const b)
{
unsigned bigger,smaller, max;

register unsigned lcm;

if (a>b) bigger=a,smalle r=b;
else bigger=b,smalle r=a;

if ( !(bigger%smalle r) ) return bigger;

max = (unsigned)-1 - bigger;

if (bigger max) return 0;

lcm = bigger<<1;

while(lcm%a || lcm%b)
{
if (lcm max) return 0;
lcm += bigger;
}

return lcm;
}

Any ideas to make it more efficient?

--

Frederick Gotham
Oct 23 '06 #1
10 3148
On Oct 23, 11:01 pm, Frederick Gotham <fgotha...@SPAM .comwrote:
Anyone want to give me a hand to write efficient algorithms for Highest
Common Factor and Lowest Common Multiple? This is what I have so far...

Caveat: I haven't gone over the following code in detail, so there might be
a subtle bug lurking somewhere.

unsigned HCF(unsigned const a, unsigned const b)
{
unsigned bigger,smaller;

register unsigned hcf;

if (a>b) bigger=a,smalle r=b;
else bigger=b,smalle r=a;

if ( !(bigger%smalle r) ) return smaller;

hcf = smaller-1;

while (a%hcf || b%hcf) --hcf;

return hcf;

}Any ideas to make it more efficient?

Here's what I've got for Lowest Common Multiple:

unsigned LCM(unsigned const a,unsigned const b)
{
unsigned bigger,smaller, max;

register unsigned lcm;

if (a>b) bigger=a,smalle r=b;
else bigger=b,smalle r=a;

if ( !(bigger%smalle r) ) return bigger;

max = (unsigned)-1 - bigger;

if (bigger max) return 0;

lcm = bigger<<1;

while(lcm%a || lcm%b)
{
if (lcm max) return 0;
lcm += bigger;
}

return lcm;

}Any ideas to make it more efficient?
For hcf (AKA gcd, greatest common divisor), read about Euclid's
algorithm (http://en.wikipedia.org/wiki/Euclidean_algorithm).
--
Nachch

Oct 23 '06 #2
Frederick Gotham wrote:
Anyone want to give me a hand to write efficient algorithms for Highest
Common Factor and Lowest Common Multiple? This is what I have so far...

Caveat: I haven't gone over the following code in detail, so there might be
a subtle bug lurking somewhere.
I haven't got access to a C compiler anymore, but I used
Knuth's binary algorithm when I needed this function.

unsigned HCF (unsigned a, unsigned b)
{
int za, zb, zr;

if (a == 0 || b == 0)
return a + b;

za = zb = 0;
while ((a & 1) == 0)
{
a >>= 1; ++za;
}
while ((b & 1) == 0)
{
b >>= 1; ++zb;
}
zr = za zb ? zb : za;

/* odd HCF of two odd numbers (Knuth) */
while (a != b)
{
if (a b)
{
a -= b;
do
a >>= 1;
while ((a & 1) == 0);
}
else
{
b -= a;
do
b >>= 1;
while ((b & 1) == 0);
}
} /* end Knuth */

while (zr)
{
a <<= 1; --zr;
}

return a;
}
--

Oct 23 '06 #3
na****@gmail.co m wrote:
On Oct 23, 11:01 pm, Frederick Gotham <fgotha...@SPAM .comwrote:
Anyone want to give me a hand to write efficient algorithms for Highest
Common Factor and Lowest Common Multiple?
For hcf (AKA gcd, greatest common divisor), read about Euclid's
algorithm (http://en.wikipedia.org/wiki/Euclidean_algorithm).
Indeed, Euclid's algorithm seems the best way to go, unless you want to
try factorising the numbers. Note that you can modify the algorithm
slightly whereby instead of calculating a%b (which will be in the range
0 to b-1 inclusive) you allow negative numbers to give a result in the
range -b/2 to b/2. This guarentees that b is halving in magnitude each
time so places a log bound on the running time.

For lcm, note that hcf * lcm = product of the two numbers, so you can
re-use the hcf routine.

Hope this helps.
Paul.

Oct 23 '06 #4
Frederick Gotham wrote:
Anyone want to give me a hand to write efficient algorithms for Highest
Common Factor and Lowest Common Multiple? This is what I have so far...

Caveat: I haven't gone over the following code in detail, so there might be
a subtle bug lurking somewhere.

unsigned HCF(unsigned const a, unsigned const b)
{
unsigned bigger,smaller;

register unsigned hcf;

if (a>b) bigger=a,smalle r=b;
else bigger=b,smalle r=a;

if ( !(bigger%smalle r) ) return smaller;

hcf = smaller-1;

while (a%hcf || b%hcf) --hcf;

return hcf;
}

Any ideas to make it more efficient?

Here's what I've got for Lowest Common Multiple:

unsigned LCM(unsigned const a,unsigned const b)
{
unsigned bigger,smaller, max;

register unsigned lcm;

if (a>b) bigger=a,smalle r=b;
else bigger=b,smalle r=a;

if ( !(bigger%smalle r) ) return bigger;

max = (unsigned)-1 - bigger;

if (bigger max) return 0;

lcm = bigger<<1;

while(lcm%a || lcm%b)
{
if (lcm max) return 0;
lcm += bigger;
}

return lcm;
}

Any ideas to make it more efficient?
Several gcd programs in C can be found at

http://galileo.phys.virginia.edu/cla...l01/Cprogs.htm

Look at item #4.

--
Julian V. Noble
Professor Emeritus of Physics
University of Virginia
Oct 23 '06 #5
Frederick Gotham wrote:
Anyone want to give me a hand to write efficient algorithms for Highest
Common Factor and Lowest Common Multiple? This is what I have so far...

Caveat: I haven't gone over the following code in detail, so there might be
a subtle bug lurking somewhere.

unsigned HCF(unsigned const a, unsigned const b)
{
unsigned bigger,smaller;

register unsigned hcf;

if (a>b) bigger=a,smalle r=b;
else bigger=b,smalle r=a;

if ( !(bigger%smalle r) ) return smaller;

hcf = smaller-1;

while (a%hcf || b%hcf) --hcf;

return hcf;
}

Any ideas to make it more efficient?
You may find this one useful:

long gcd(long a, long b)
{
long c;

if (b==0)
return a; /*so that gcd(5,0) should work*/

/*****Euclidean algorithm*****/
while ((c=a%b))
{
a = b;
b = c;
}

return b;
}

Oct 24 '06 #6

"Frederick Gotham" <fg*******@SPAM .comwrote in message
news:j4******** ***********@new s.indigo.ie...
>
Anyone want to give me a hand to write efficient algorithms for Highest
Common Factor and Lowest Common Multiple? This is what I have so far...

Caveat: I haven't gone over the following code in detail, so there might
be
a subtle bug lurking somewhere.

unsigned HCF(unsigned const a, unsigned const b)
{
unsigned bigger,smaller;

register unsigned hcf;

if (a>b) bigger=a,smalle r=b;
else bigger=b,smalle r=a;

if ( !(bigger%smalle r) ) return smaller;

hcf = smaller-1;

while (a%hcf || b%hcf) --hcf;

return hcf;
}

Any ideas to make it more efficient?

Here's what I've got for Lowest Common Multiple:

unsigned LCM(unsigned const a,unsigned const b)
{
unsigned bigger,smaller, max;

register unsigned lcm;

if (a>b) bigger=a,smalle r=b;
else bigger=b,smalle r=a;

if ( !(bigger%smalle r) ) return bigger;

max = (unsigned)-1 - bigger;

if (bigger max) return 0;

lcm = bigger<<1;

while(lcm%a || lcm%b)
{
if (lcm max) return 0;
lcm += bigger;
}

return lcm;
}

Any ideas to make it more efficient?
I doubt it. OTOH, if Dr. Knuth looked at this, he wouldn't get it. On the
first hand, this source executes so lightning fast that to argue about its
speed would be to argue about the comparative speed of light in various
media.

My question is, using an appropriate caller, how large a number can this
work for? EC
Oct 24 '06 #7

Elijah Cardon wrote:
"Frederick Gotham" <fg*******@SPAM .comwrote in message
news:j4******** ***********@new s.indigo.ie...

Anyone want to give me a hand to write efficient algorithms for Highest
Common Factor and Lowest Common Multiple? This is what I have so far...

Caveat: I haven't gone over the following code in detail, so there might
be
a subtle bug lurking somewhere.

unsigned HCF(unsigned const a, unsigned const b)
{
unsigned bigger,smaller;

register unsigned hcf;

if (a>b) bigger=a,smalle r=b;
else bigger=b,smalle r=a;

if ( !(bigger%smalle r) ) return smaller;

hcf = smaller-1;

while (a%hcf || b%hcf) --hcf;

return hcf;
}

Any ideas to make it more efficient?

Here's what I've got for Lowest Common Multiple:

unsigned LCM(unsigned const a,unsigned const b)
{
unsigned bigger,smaller, max;

register unsigned lcm;

if (a>b) bigger=a,smalle r=b;
else bigger=b,smalle r=a;

if ( !(bigger%smalle r) ) return bigger;

max = (unsigned)-1 - bigger;

if (bigger max) return 0;

lcm = bigger<<1;

while(lcm%a || lcm%b)
{
if (lcm max) return 0;
lcm += bigger;
}

return lcm;
}

Any ideas to make it more efficient?
I doubt it. OTOH, if Dr. Knuth looked at this, he wouldn't get it. On the
first hand, this source executes so lightning fast that to argue about its
speed would be to argue about the comparative speed of light in various
media.
Really?

Doesn't the execution time of

while (a%hcf || b%hcf) --hcf;

double every time the smaller operand increases by 1 bit?
>
My question is, using an appropriate caller, how large a number can this
work for? EC
Can it do (assuming appropriate data types)

a: 2**177149-1 b: 2**19685-1 ?

Oct 24 '06 #8
Frederick Gotham wrote:
Anyone want to give me a hand to write efficient algorithms for Highest
Common Factor and Lowest Common Multiple? This is what I have so far...

Caveat: I haven't gone over the following code in detail, so there might be
a subtle bug lurking somewhere.

unsigned HCF(unsigned const a, unsigned const b)
<SNIP>

Any ideas to make it more efficient?

Here's what I've got for Lowest Common Multiple:

unsigned LCM(unsigned const a,unsigned const b)
<SNIP>

Any ideas to make it more efficient?
If you want to calculate both HCF (usually called
greatest common divisor) and LCM for the same
pair of numbers a,b then you can use the formula
LCM(a,b) = (a*b)/HCF(a,b) This should be faster than
calling both of your functions.

Oct 24 '06 #9
Spiros Bousbouras wrote:
If you want to calculate both HCF (usually called
greatest common divisor) and LCM for the same
pair of numbers a,b then you can use the formula
LCM(a,b) = (a*b)/HCF(a,b) This should be faster than
calling both of your functions.
It's better to use LCM(a,b) = a*(b/HCF(a,b)). This avoids one potential
for overflow, and there can't be a rounding error since the result of
HCF() by definition is a factor of b.

Oct 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1820
by: Michel | last post by:
Hi there, I need to make a poisson distribution function that uses: double Math.Exp(double d) The d argument is a negative number in my case. When d becomes bigger and bigger, the result loses precision and is finally 0 (when d reaches 650) because it is too small to fit in a double. In my case, d can become -10000, so I need to have a greater precision number and Exp function.
21
13491
by: Jaspreet | last post by:
I was working on some database application and had this small task of getting the second highes marks in a class. I was able to do that using subqueries. Just thinking what is a good way of getting second highest value in an integer array. One method I know of is to make the 1st pass through the array and find the highest number. In the second pass we can find the highest number which is less than the number we obtained in the 1st pass.
3
1640
by: Frank Esser | last post by:
Hi ! I have several buttons and a common click event for all of them. How can I get the reference to the clicked control in a switch? This does not work: private void Button_Click(object sender, System.EventArgs e) { switch ((Button)sender)
7
3360
by: Jan | last post by:
Hi there, Is there a fast way to get the highest value from an array? I've got the array strStorage(intCounter) I tried something but it all and's to nothing If someone good helpme, TIA
4
4899
by: johnk | last post by:
I have a table of items, with revision numbers. I need to extract the items with highest revision number. The items may be listed several times and I don't know what the highest revision number for each item is. How do I do this?
10
4380
by: strife | last post by:
Hi, This is a homework question. I will try to keep it minimal so not to have anyone do it for me. I am really just stuck on one small spot. I have to figure out the highest number from a users input. I give them a menu to choose from, and I am stuck on only one of the choices. When this choice is picked I must allow the user to enter how many numbers he/she wants to enter; then I must figure out which number is the highest. I have...
0
1689
by: Carroll | last post by:
I would like to be able to select the maximum (highest ) value for a field, the 2nd highest, 3rd highest, etc. Are there any other functions that I might consider using with SQL? MAX will only pick up the highest and MIN the lowest value, but is there a way to pick up middle values, i.e., the 2nd, 3rd, etc, so I can list them in one row, along with a common key? Thanks, Carroll Rinehart
2
3417
by: Richy | last post by:
I'm getting the message: common language runtime detected an invalid program all too frequently in VB 2008 Express. I use edit-and-continue a lot and it usually happens after making a minor change, even if I undo the change. It didn't used to happen, it is getting more and more frequent and it is really frustrating now. Does anybody know how to stop this from happening?
6
11501
Markus
by: Markus | last post by:
Things to discuss: Headers What are they? What does PHP have to do with headers? Why can they only be sent before any output? Common causes
0
10623
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10373
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5683
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3852
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.