Skip to content

Commit 96a691a

Browse files
author
Mukundan Senthil
authored
Implementation of Euclidean Algorithim in VimL (#654)
* Add viml to book.json * Add euclidean algorithim in viml
1 parent 4790839 commit 96a691a

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

book.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@
208208
{
209209
"lang": "kotlin",
210210
"name": "Kotlin"
211+
},
212+
{
213+
"lang": "vim",
214+
"name": "VimL"
211215
}
212216
]
213217
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function s:euclid_mod(a, b)
2+
let l:a = abs(a:a)
3+
let l:b = abs(a:b)
4+
5+
while l:b != 0
6+
let l:c = l:b
7+
let l:b = l:a % l:b
8+
let l:a = l:c
9+
endwhile
10+
11+
return l:a
12+
endfunction
13+
14+
function s:euclid_sub(a, b)
15+
let l:a = abs(a:a)
16+
let l:b = abs(a:b)
17+
18+
while l:a != l:b
19+
if l:a > l:b
20+
let l:a -= l:b
21+
else
22+
let l:b -= l:a
23+
endif
24+
endwhile
25+
26+
return l:a
27+
endfunction
28+
29+
let s:check_1 = s:euclid_mod(64 * 67, 64 * 71)
30+
let s:check_2 = s:euclid_sub(128 * 12, 128 * 77)
31+
32+
echo 'Modulus-based euclidean algorithm result:' s:check_1
33+
echo 'subtraction-based euclidean algorithm result:' s:check_2

contents/euclidean_algorithm/euclidean_algorithm.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Computer science is (almost by definition) a science about computers -- a device
55
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:
66

77
{% method %}
8+
{% sample lang="vim" %}
9+
[import:14-27, lang="vim"](code/viml/euclidean.vim)
810
{% sample lang="c" %}
911
[import:17-30, lang="c_cpp"](code/c/euclidean_example.c)
1012
{% sample lang="cs" %}
@@ -91,6 +93,8 @@ Here, we simply line the two numbers up every step and subtract the lower value
9193
Modern implementations, though, often use the modulus operator (%) like so
9294

9395
{% method %}
96+
{% sample lang="vim" %}
97+
[import:1-12, lang="vim"](code/viml/euclidean.vim)
9498
{% sample lang="c" %}
9599
[import:4-16, lang="c_cpp"](code/c/euclidean_example.c)
96100
{% sample lang="cs" %}
@@ -187,6 +191,8 @@ Here's a video on the Euclidean algorithm:
187191
## Example Code
188192

189193
{% method %}
194+
{% sample lang="vim" %}
195+
[import, lang="vim"](code/viml/euclidean.vim)
190196
{% sample lang="c" %}
191197
[import, lang="c_cpp"](code/c/euclidean_example.c)
192198
{% sample lang="cs" %}

0 commit comments

Comments
 (0)