2 Commits

Author SHA1 Message Date
0cd515b428 Обновить script_LCM.sh 2026-04-20 10:22:26 +03:00
Максим Тер-Томасов
bfd0168b25 Add LCM script (Task 2) 2026-04-19 14:34:14 +03:00
2 changed files with 17 additions and 11 deletions

17
script_LCM.sh Executable file
View File

@@ -0,0 +1,17 @@
#!/bin/bash
gcd() {
local a=$1 b=$2
while [ $b -ne 0 ]; do
local temp=$b; b=$((a % b)); a=$temp
done
echo $a
}
lcm() {
local a=$1 b=$2
local g=$(gcd $a $b)
echo $(( (a * b) / g ))
}
if [ $# -ne 2 ]; then
echo "Usage: $0 <num1> <num2>"; exit 1
fi
echo "НОК($1, $2) = $(lcm $1 $2)"

View File

@@ -1,11 +0,0 @@
#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo "=== Калькулятор НОД и НОК ==="
read -p "Введите первое число: " n1
read -p "Введите второе число: " n2
if ! [[ "$n1" =~ ^[0-9]+$ ]] || ! [[ "$n2" =~ ^[0-9]+$ ]]; then
echo "Ошибка: введите целые числа!"; exit 1
fi
echo "Результаты:"
echo -n "НОД: "; bash "$SCRIPT_DIR/script_GCD.sh" "$n1" "$n2"
echo -n "НОК: "; bash "$SCRIPT_DIR/script_LCM.sh" "$n1" "$n2"