diff --git a/book.json b/book.json index 718584ff8..287f680c2 100644 --- a/book.json +++ b/book.json @@ -212,6 +212,10 @@ { "lang": "vim", "name": "VimL" + }, + { + "lang": "coco", + "name": "Coconut" } ] } diff --git a/contents/euclidean_algorithm/code/coconut/euclidean.coco b/contents/euclidean_algorithm/code/coconut/euclidean.coco new file mode 100644 index 000000000..8f5b5c9d7 --- /dev/null +++ b/contents/euclidean_algorithm/code/coconut/euclidean.coco @@ -0,0 +1,19 @@ +def euclid_sub(a is int, 0) = a +addpattern def euclid_sub(0, b is int) = b + +addpattern def euclid_sub(a is int, b is int): + if a < b: + return euclid_sub(a, b - a) + elif b < a: + return euclid_sub(a - b, b) + return a + + +def euclid_mod(a is int, 0) = a +addpattern def euclid_mod(0, b is int) = b + +addpattern def euclid_mod(a is int, b is int) = euclid_mod(b, a % b) + +if __name__ == '__main__': + print('Euclidean mod:', euclid_mod(64 * 67, 64 * 81)) + print('Euclidean sub:', euclid_sub(128 * 12, 128 * 77)) diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index bb9702c0d..0de143dac 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -81,6 +81,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two {% sample lang="ps1" %} [import:1-14, lang="powershell"](code/powershell/euclidean_algorithm.ps1) +{% sample lang="coco" %} +[import:1-9, lang="coconut"](code/coconut/euclidean.coco) {% endmethod %} @@ -169,6 +171,8 @@ Modern implementations, though, often use the modulus operator (%) like so {% sample lang="ps1" %} [import:16-27, lang="powershell"](code/powershell/euclidean_algorithm.ps1) +{% sample lang="coco" %} +[import:12-15, lang="coconut"](code/coconut/euclidean.coco) {% endmethod %} @@ -282,6 +286,8 @@ The code snippets were taken from this [Scratch project](https://scratch.mit.edu {% sample lang="ps1" %} [import, lang="powershell"](code/powershell/euclidean_algorithm.ps1) +{% sample lang="coco" %} +[import, lang="coconut"](code/coconut/euclidean.coco) {% endmethod %}