Skip to content

Commit 8adf883

Browse files
committed
Changes to the Java and Python ex. of Euclid
The implementations now support negative numbers.
1 parent 08aca49 commit 8adf883

File tree

1 file changed

+11
-0
lines changed
  • chapters/fundamental_algorithms/euclidean_algorithm

1 file changed

+11
-0
lines changed

chapters/fundamental_algorithms/euclidean_algorithm/euclidean.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ document.write(euclid_sub(128*12, 128*77) + "<br>");
203203

204204
def euclid_mod(a, b):
205205

206+
a = abs(a)
207+
b = abs(b)
206208
temp = 0
207209

208210
while b > 0:
@@ -214,6 +216,9 @@ def euclid_mod(a, b):
214216

215217
def euclid_sub(a, b):
216218

219+
a = abs(a)
220+
b = abs(b)
221+
217222
while a != b:
218223
if a > b:
219224
a = a - b
@@ -366,6 +371,9 @@ public static void main(String[] args) {
366371
}
367372

368373
public static int euclidSub(int a, int b) {
374+
a = Math.abs(a);
375+
b = Math.abs(b);
376+
369377
while (a != b) {
370378
if (a > b) {
371379
a -=b;
@@ -378,6 +386,9 @@ public static int euclidSub(int a, int b) {
378386
}
379387

380388
public static int euclidMod(int a, int b) {
389+
a = Math.abs(a);
390+
b = Math.abs(b);
391+
381392
while (b != 0){
382393
int temp = b;
383394
b = a % b;

0 commit comments

Comments
 (0)