Skip to content

Commit e1c1d7c

Browse files
PaddyKeleios
authored andcommitted
Add Euclidean Algorithm in PowerShell (algorithm-archivists#626)
* added PowerShell language * added Euclidean Algorithm in PowerShell * Corrected indentation in book.json * Added PowerShell to .editorconfig * added more meaningful outputs
1 parent 4c73370 commit e1c1d7c

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

Diff for: .editorconfig

+6
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,13 @@ trim_trailing_whitespace = false
162162
insert_final_newline = false
163163
end_of_line = lf
164164

165+
# PowerShell
166+
[*.ps1]
167+
indent_style = space
168+
indent_size = 4
169+
165170
# CoffeeScript
166171
[*.coffee]
167172
indent_style = space
168173
indent_size = 2
174+

Diff for: book.json

+4
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@
194194
"name": "Scheme"
195195
},
196196
{
197+
"lang": "ps1",
198+
"name": "PowerShell"
199+
},
200+
{
197201
"lang": "v",
198202
"name": "Vlang"
199203
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function Sub-Euclid($a, $b) {
2+
$a = [Math]::Abs($a)
3+
$b = [Math]::Abs($b)
4+
5+
while ($a -ne $b) {
6+
if ($a -gt $b) {
7+
$a = $a - $b
8+
} else {
9+
$b = $b - $a
10+
}
11+
}
12+
13+
return $a
14+
}
15+
16+
function Mod-Euclid($a, $b) {
17+
$a = [Math]::Abs($a)
18+
$b = [Math]::Abs($b)
19+
20+
while ($b -ne 0) {
21+
$tmp = $b
22+
$b = $a % $b
23+
$a = $tmp
24+
}
25+
26+
return $a
27+
}
28+
29+
Write-Host "Subtraction-based euclidean algorithm result: $(Mod-Euclid $(64 * 67) $(64 * 81))"
30+
Write-Host "Modulus-based euclidean algorithm result: $(Sub-Euclid $(128 * 12) $(128 * 77))"

Diff for: contents/euclidean_algorithm/euclidean_algorithm.md

+12
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
7575
<p>
7676
<img class="center" src="code/scratch/euclid_sub.svg" width="200" />
7777
</p>
78+
# leave one line empty:
79+
80+
{% sample lang="ps1" %}
81+
[import:1-14, lang="powershell"](code/powershell/euclidean_algorithm.ps1)
7882

7983
{% endmethod %}
8084

@@ -157,6 +161,10 @@ Modern implementations, though, often use the modulus operator (%) like so
157161
<p>
158162
<img class="center" src="code/scratch/euclid_mod.svg" width="200" />
159163
</p>
164+
# leave one line empty:
165+
166+
{% sample lang="ps1" %}
167+
[import:16-27, lang="powershell"](code/powershell/euclidean_algorithm.ps1)
160168

161169
{% endmethod %}
162170

@@ -264,6 +272,10 @@ The code snippets were taken from this [Scratch project](https://scratch.mit.edu
264272
<p>
265273
<img class="center" src="code/scratch/main.svg" width="200" />
266274
</p>
275+
# leave one line empty:
276+
277+
{% sample lang="ps1" %}
278+
[import, lang="powershell"](code/powershell/euclidean_algorithm.ps1)
267279

268280
{% endmethod %}
269281

0 commit comments

Comments
 (0)