From 82a0a158eb97490bbd19a86c86495feb9161891a Mon Sep 17 00:00:00 2001 From: lilrax Date: Sat, 14 Mar 2026 22:58:32 +0300 Subject: [PATCH] Add LCM script (Task 2) - final --- script_LCM.sh | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 script_LCM.sh diff --git a/script_LCM.sh b/script_LCM.sh new file mode 100644 index 0000000..5bbd33f --- /dev/null +++ b/script_LCM.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +# Function to find GCD (Greatest Common Divisor) +gcd() { + local a=$1 + local b=$2 + + while [ $b -ne 0 ]; do + local temp=$b + b=$((a % b)) + a=$temp + done + + echo $a +} + +# Function to find LCM (Least Common Multiple) +lcm() { + local a=$1 + local b=$2 + + # LCM = (a * b) / GCD(a, b) + local gcd_value=$(gcd $a $b) + echo $(( (a * b) / gcd_value )) +} + +# Check arguments +if [ $# -ne 2 ]; then + echo "Usage: $0 " + exit 1 +fi + +# Call function and print result +result=$(lcm $1 $2) +echo "НОК($1, $2) = $result"