はじめに
この実験では、C プログラミングを使用して、2 つの整数の最小公倍数 (LCM) を求める方法を学びます。この実験では、ユーザー入力から 2 つの整数を取得することから始まり、LCM = (a*b)/GCD(a,b) の公式を使用して LCM を計算し、最後に結果を出力するまでの手順を段階的に説明します。この実験の終わりまでに、ユークリッドの互除法などの数論的概念を適用して、C での実用的な問題を解決する方法をしっかりと理解しているはずです。
2 つの整数の入力
このステップでは、C プログラミングでユーザー入力から 2 つの整数をどのように読み込むか、最小公倍数 (LCM) の計算における最初のステップを学びます。
まず、LCM プログラム用の新しい C ファイルを作成しましょう。
cd ~/project
nano lcm.c
次に、2 つの整数を取得するコードを追加します。
#include <stdio.h>
int main() {
int a, b;
printf("Enter two positive integers:\n");
printf("First number: ");
scanf("%d", &a);
printf("Second number: ");
scanf("%d", &b);
printf("You entered: %d and %d\n", a, b);
return 0;
}
プログラムをコンパイルして実行します。
gcc lcm.c -o lcm
./lcm
出力例:
Enter two positive integers:
First number: 12
Second number: 18
You entered: 12 and 18
コードを詳しく見てみましょう。
scanf()関数は、ユーザーからの整数入力を取得するために使用されます。%dフォーマット指定子は、整数入力に使用されます。&aと&bは、入力値が格納されるメモリのアドレスを渡します。
LCM = (a*b)/GCD(a,b) を使用
このステップでは、最小公倍数 (LCM) を計算するために、LCM(a,b) = (a*b)/GCD(a,b) の公式を実装します。最初に、ユークリッドの互除法を使用して最大公約数 (GCD) を計算する関数を作成します。
lcm.c ファイルを以下のコードで更新します。
#include <stdio.h>
// ユークリッドの互除法を用いた GCD 計算関数
int calculateGCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// LCM 計算関数
int calculateLCM(int a, int b) {
return (a * b) / calculateGCD(a, b);
}
int main() {
int a, b, lcm;
printf("Enter two positive integers:\n");
printf("First number: ");
scanf("%d", &a);
printf("Second number: ");
scanf("%d", &b);
lcm = calculateLCM(a, b);
printf("The Least Common Multiple of %d and %d is: %d\n", a, b, lcm);
return 0;
}
プログラムをコンパイルして実行します。
gcc lcm.c -o lcm
./lcm
出力例:
Enter two positive integers:
First number: 12
Second number: 18
The Least Common Multiple of 12 and 18 is: 36
主な構成要素を詳しく見てみましょう。
calculateGCD()は、ユークリッドの互除法を実装して最大公約数 (GCD) を求めます。calculateLCM()は、LCM(a,b) = (a*b)/GCD(a,b) の公式を使用します。- ユークリッドの互除法は、繰り返し剰余を求めることで効率的に GCD を計算します。
Print the LCM
In this final step, you will run the LCM program and verify its output for different input combinations. We'll test the program with various integer pairs to demonstrate the LCM calculation.
Compile the program (if not already compiled):
cd ~/project
gcc lcm.c -o lcm
Run the program with different input combinations:
./lcm << EOF
12
18
EOF
Example output for 12 and 18:
Enter two positive integers:
First number: 12
Second number: 18
The Least Common Multiple of 12 and 18 is: 36
Let's try another example:
./lcm << EOF
15
25
EOF
Example output for 15 and 25:
Enter two positive integers:
First number: 15
Second number: 25
The Least Common Multiple of 15 and 25 is: 75
Key points to understand:
- The LCM is the smallest positive integer that is divisible by both input numbers
- For 12 and 18, the LCM is 36
- For 15 and 25, the LCM is 75
- The program uses the formula LCM(a,b) = (a*b)/GCD(a,b)
To make the program more robust, you can add input validation:
nano lcm.c
Update the main() function to include input validation:
int main() {
int a, b, lcm;
printf("Enter two positive integers:\n");
printf("First number: ");
scanf("%d", &a);
printf("Second number: ");
scanf("%d", &b);
// Input validation
if (a <= 0 || b <= 0) {
printf("Error: Please enter positive integers only.\n");
return 1;
}
lcm = calculateLCM(a, b);
printf("The Least Common Multiple of %d and %d is: %d\n", a, b, lcm);
return 0;
}
Recompile and test the updated program:
gcc lcm.c -o lcm
./lcm
最小公倍数 (LCM) の計算
この実験では、C プログラミングで 2 つの整数の最小公倍数 (LCM) を求める方法を学びます。最初に、ユーザーから 2 つの整数を入力を受け取ります。次に、ユークリッドの互除法を使用して最大公約数 (GCD) を計算する関数を作成し、公式 LCM(a,b) = (a*b)/GCD(a,b) を実装します。最後に、計算された LCM を出力します。
重要な学習点は、scanf() を使用して整数を入力すること、ユークリッドの互除法を使用して GCD を計算すること、そして公式を使用して LCM を計算することです。この実験の終わりまでに、C で 2 つの数の LCM を求める方法をしっかりと理解しているはずです。



