Files
laboratory_work1-DS-/lcm.sh
2026-02-27 19:56:02 +00:00

37 lines
673 B
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
if [ $# -ne 2 ]; then
echo "Использование: $0 <число1> <число2>"
exit 1
fi
if ! [[ $1 =~ ^[0-9]+$ ]] || ! [[ $2 =~ ^[0-9]+$ ]]; then
echo "Ошибка: аргументы должны быть неотрицательными целыми числами"
exit 1
fi
a=$1
b=$2
if [ $a -eq 0 ] || [ $b -eq 0 ]; then
echo "НОК($1, $2) = 0"
exit 0
fi
gcd() {
local x=$1
local y=$2
while [ $y -ne 0 ]; do
local remainder=$((x % y))
x=$y
y=$remainder
done
echo $x
}
gcd_result=$(gcd $a $b)
lcm_result=$(( (a * b) / gcd_result ))
echo "НОК($1, $2) = $lcm_result"