-
-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy patheuclid.st
More file actions
29 lines (27 loc) · 645 Bytes
/
euclid.st
File metadata and controls
29 lines (27 loc) · 645 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Integer>>euclidSub: secondNumber
"Euclidean algorithm with subtraction"
| a b |
a := self abs.
b := secondNumber abs.
[ a == b ] whileFalse: [
a > b ifTrue: [
a := a - b.
] ifFalse: [
b := b - a.
].
].
^a.
Integer>>euclidMod: secondNumber
"Euclidean algorithm with modulus."
| a b oldB |
a := self abs.
b := secondNumber abs.
[ b == 0 ] whileFalse: [
oldB := b.
b := a % b.
a := oldB.
].
^a.
Transcript show: ((64 * 67) euclidSub: (64 * 81)).
Transcript cr.
Transcript show: ((128 * 12) euclidMod: (128 * 77)).