Greatest common divisor/fi: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Greatest common divisor/fi
| Old | New | Line | |
|---|---|---|---|
| 1 | + | # Suurin yhteinen tekijä | |
| 2 | + | ||
| 3 | + | Suurin yhteinen tekijä on suurin kokonaisluku, jolla voidaan jakaa annetut kaksi kokonaislukua. Jos annetut kokonaisluvut ovat 121 ja 143 niin niiden suurin yhteinen tekijä on 11. | |
| 4 | + | ||
| 5 | + | On olemassa monia menetelmiä laskea tämä. Esimerkiksi jakoon perustuva Eukleideen algoritmia käyttävä versio voidaan ohjelmoida näin: | |
| 6 | + | ||
| 7 | + | ## Funktio GreatestCommonDivisor | |
| 8 | + | ||
| 9 | + | ``` pascal | |
| 10 | + | ||
| 11 | + | function GreatestCommonDivisor(a, b: Int64): Int64; | |
| 12 | + | var | |
| 13 | + | temp: Int64; | |
| 14 | + | begin | |
| 15 | + | while b <> 0 do | |
| 16 | + | begin | |
| 17 | + | temp := b; | |
| 18 | + | b := a mod b; | |
| 19 | + | a := temp | |
| 20 | + | end; | |
| 21 | + | result := a | |
| 22 | + | end; | |
| 23 | + | ``` | |
| 24 | + | ||
| 25 | + | Tai toinen vaihtoehtoinen tapa | |
| 26 | + | ||
| 27 | + | ``` pascal | |
| 28 | + | function GreatestCommonDivisor_EuclidsSubtractionMethod(a, b: Int64): Int64; | |
| 29 | + | //only works with positive integers | |
| 30 | + | begin | |
| 31 | + | if (a < 0) then a := -a; | |
| 32 | + | if (b < 0) then b := -b; | |
| 33 | + | if (a = 0) then Exit(b); | |
| 34 | + | if (b = 0) then Exit(a); | |
| 35 | + | while not (a = b) do | |
| 36 | + | begin | |
| 37 | + | if (a > b) then | |
| 38 | + | a := a - b | |
| 39 | + | else | |
| 40 | + | b := b - a; | |
| 41 | + | end; | |
| 42 | + | Result := a; | |
| 43 | + | end; | |
| 44 | + | ``` | |
| 45 | + | ||
| 46 | + | ## Katso myös | |
| 47 | + | ||
| 48 | + | - [Recursive example](http://rosettacode.org/wiki/Greatest_common_divisor#Pascal_.2F_Delphi_.2F_Free_Pascal) | |
| 49 | + | - [[Least common multiple/fi|Pienin yhteinen jaettava]] (Least common multiple) | |
| 50 | + | - [[Mod]] | |
| 51 | + | ||
| 52 | + | --- | |
| 53 | + | *From the Free Pascal wiki, page "Greatest common divisor/fi", as it stood on 2020-02-16 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
