From 40ecdfbad0755e44995b6248f91c5aa35614595d Mon Sep 17 00:00:00 2001 From: Mukundan Senthil Date: Thu, 14 Nov 2019 20:31:59 +0530 Subject: [PATCH 1/2] Add viml to book.json --- book.json | 4 +++ .../code/viml/euclidean.vim | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 contents/euclidean_algorithm/code/viml/euclidean.vim diff --git a/book.json b/book.json index 929901230..1a2c85379 100644 --- a/book.json +++ b/book.json @@ -204,6 +204,10 @@ { "lang": "kotlin", "name": "Kotlin" + }, + { + "lang": "vim", + "name": "VimL" } ] } diff --git a/contents/euclidean_algorithm/code/viml/euclidean.vim b/contents/euclidean_algorithm/code/viml/euclidean.vim new file mode 100644 index 000000000..c7b939f20 --- /dev/null +++ b/contents/euclidean_algorithm/code/viml/euclidean.vim @@ -0,0 +1,33 @@ +function s:euclid_mod(a, b) + let l:a = abs(a:a) + let l:b = abs(a:b) + + while l:b != 0 + let l:c = l:b + let l:b = l:a % l:b + let l:a = l:c + endwhile + + return l:a +endfunction + +function s:euclid_sub(a, b) + let l:a = abs(a:a) + let l:b = abs(a:b) + + while l:a != l:b + if l:a > l:b + let l:a -= l:b + else + let l:b -= l:a + endif + endwhile + + return l:a +endfunction + +let s:check_1 = s:euclid_mod(64 * 67, 64 * 71) +let s:check_2 = s:euclid_sub(128 * 12, 128 * 77) + +echo 'Modulus-based euclidean algorithm result:' s:check_1 +echo 'subtraction-based euclidean algorithm result:' s:check_2 From 0cccabab41ae77df856bd0fa1ea4b5ed0522e048 Mon Sep 17 00:00:00 2001 From: Mukundan Senthil Date: Thu, 14 Nov 2019 20:32:27 +0530 Subject: [PATCH 2/2] Add euclidean algorithim in viml --- contents/euclidean_algorithm/euclidean_algorithm.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index d1ac7e2de..e751e19a9 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -5,6 +5,8 @@ Computer science is (almost by definition) a science about computers -- a device The algorithm is a simple way to find the *greatest common divisor* (GCD) of two numbers, which is useful for a number of different applications (like reducing fractions). The first method (envisioned by Euclid) uses simple subtraction: {% method %} +{% sample lang="vim" %} +[import:14-27, lang="vim"](code/viml/euclidean.vim) {% sample lang="c" %} [import:17-30, lang="c_cpp"](code/c/euclidean_example.c) {% sample lang="cs" %} @@ -87,6 +89,8 @@ Here, we simply line the two numbers up every step and subtract the lower value Modern implementations, though, often use the modulus operator (%) like so {% method %} +{% sample lang="vim" %} +[import:1-12, lang="vim"](code/viml/euclidean.vim) {% sample lang="c" %} [import:4-16, lang="c_cpp"](code/c/euclidean_example.c) {% sample lang="cs" %} @@ -179,6 +183,8 @@ Here's a video on the Euclidean algorithm: ## Example Code {% method %} +{% sample lang="vim" %} +[import, lang="vim"](code/viml/euclidean.vim) {% sample lang="c" %} [import, lang="c_cpp"](code/c/euclidean_example.c) {% sample lang="cs" %}