-
-
Notifications
You must be signed in to change notification settings - Fork 359
Euclid algorithm in bash #497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
9dcaf93
c5cb003
a23dd47
91b3cfe
36c8333
1ea7343
ae6a1c0
6435b7f
85dd93a
c198611
02f514a
f2bdb54
f3be8ed
0508ea2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,6 +163,10 @@ | |
{ | ||
"lang": "lolcode", | ||
"name": "LOLCODE" | ||
}, | ||
{ | ||
"lang": "bash", | ||
"name": "Bash" | ||
} | ||
] | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
abs() { | ||
local ret=$1 | ||
if [ $ret -lt 0 ]; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Though, you can do even better considering the |
||
let "ret = $ret * -1" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While we are working with integers, do general arithmetic instead, using more modern Bash:
|
||
fi | ||
echo $ret | ||
} | ||
|
||
euclid_mod() { | ||
local a=$(abs $1) | ||
local b=$(abs $2) | ||
|
||
while [ $b -ne 0 ]; do | ||
let "tmp = $b" | ||
let "b = $a % $b" | ||
let "a = tmp" | ||
done | ||
echo $a | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer |
||
} | ||
|
||
euclid_sub() { | ||
local a=$(abs $1) | ||
local b=$(abs $2) | ||
|
||
while [ $a -ne $b ]; do | ||
if [ $a -gt $b ]; then | ||
let "a -= $b" | ||
else | ||
let "b -= $a" | ||
fi | ||
done | ||
echo $a | ||
} | ||
|
||
result=$(euclid_mod 143 693) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the other examples for consistency: result=$(euclid_mod $((64 * 67)) $((64 * 81))) etc. |
||
echo $result | ||
result=$(euclid_sub 150 400) | ||
echo $result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually make
lang
the file extension sosh
in this caseThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah ok