Files
github/nod.sh
2026-02-28 11:08:19 +03:00

35 lines
682 B
Bash

#!/bin/bash
gcd() {
local a=$1
local b=$2
if [ $a -eq 0 ]; then
echo $b
return 0
fi
if [ $b -eq 0 ]; then
echo $a
return 0
fi
while [ $b -ne 0 ]; do
local temp=$b
b=$((a % b))
a=$temp
done
echo $a
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
if [ $# -ne 2 ]; then
exit 1
fi
if ! [[ "$1" =~ ^[0-9]+$ ]] || ! [[ "$2" =~ ^[0-9]+$ ]]; then
echo "Ошибка: Аргументы должны быть целыми положительными числами"
exit 1
fi
result=$(gcd $1 $2)
echo "НОД($1, $2) = $result"
fi