As of May 4 2007 the scripts will autodetect your timezone settings. Nothing here has to be changed, but there are a few things

Please follow this blog

Search this blog

Tuesday, March 25, 2008

An alternative GCD algorithm

The greatest common divisor ( GCD ) of two integers a and b is usually calculated with the ( well-known ) Euclidean Algorithm. There is however an alternative algorithm which is based on an entirely different idea. Let's illustrate this idea with an example. Let a, b be integers, a >:b and (a, b) = GCD(a,b). Then the following rules can be applied recursively until a=b=GCD(a,b):
If ( a=even AND b=even) then GCD(a,b)=2*GCD(a/2,b/2)
If ( a=odd AND b=even) then GCD(a,b)=GCD(a,b/2)
If ( a=even AND b=odd) then GCD(a,b)=GCD(a/2,b)
If ( a=odd AND b=odd) then GCD(a,b)=GCD(a-b,b)

Example
(36, 27) = (27, 36/2)
(27, 18) = (27, 18/2)
(27, 9) = (27-9, 9)
(18, 9) = (18/2, 9)
(9, 9) Halt.
GCD(36,27)=9.

Compare using the Euclidean Algorithm
36 = 1 * 27 + 9
27 = 3 * 9 + 0 Halt.
GCD(36,27)=9.

However, this doesn't mean that the Euclidean Algorithm is always faster.

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Popular Posts

Welcome to The Bridge

Mathematics: is it the fabric of MEST?
This is my voyage
My continuous mission
To uncover hidden structures
To create new theorems and proofs
To boldly go where no man has gone before




(Raumpatrouille – Die phantastischen Abenteuer des Raumschiffes Orion, colloquially aka Raumpatrouille Orion was the first German science fiction television series. Its seven episodes were broadcast by ARD beginning September 17, 1966. The series has since acquired cult status in Germany. Broadcast six years before Star Trek first aired in West Germany (in 1972), it became a huge success.)