Concept:
Greatest common divisor
Headline
The greatest common divisor of two integers
Illustration
We face an algorithmic problem because the greatest common divisor can be computed by a (simple) algorithm.
- We assume (for simplicity) two positive operands x and y.
- Perform the following steps to return the greatest common divisor of x and y.
- Repeat the following step until x equals y:
- If x > y
- then subtract y from x and
- else subtract x from y.
- If x > y
- Return x.
- Repeat the following step until x equals y:
// Compute greatest common divisor
public static int gcd(int x, int y) {
// This version requires positive integers.
assert x > 0 && y > 0;
while (x != y) {
if (x > y)
x = x - y;
else
y = y - x;
}
return x;
}
Here is also an implementation in Language:Haskell:
-- Operands are supposed to be positive integers.
gcd :: Int -> Int -> Int
gcd x y | x > y = gcd (x-y) y
| x < y = gcd x (y-x)
| otherwise = x
Citation
(http://en.wikipedia.org/wiki/Greatest_common_divisor, 15 April 2013)
In mathematics, the greatest common divisor (gcd), also known as the greatest common factor (gcf), or highest common factor (hcf), of two or more integers (at least one of which is not zero), is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4.
There are no revisions for this page.
User contributions
User edits
Syntax for editing wiki
For you are available next options:will make text bold.
will make text italic.
will make text underlined.
will make text striked.
will allow you to paste code headline into the page.
will allow you to link into the page.
will allow you to paste code with syntax highlight into the page. You will need to define used programming language.
will allow you to paste image into the page.
is list with bullets.
is list with numbers.
will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.
will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.
will allow your to insert code snippets from @worker.
Syntax for editing wiki
For you are available next options:will make text bold.
will make text italic.
will make text underlined.
will make text striked.
will allow you to paste code headline into the page.
will allow you to link into the page.
will allow you to paste code with syntax highlight into the page. You will need to define used programming language.
will allow you to paste image into the page.
is list with bullets.
is list with numbers.
will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.
will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.
will allow your to insert code snippets from @worker.