-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcm.cs
More file actions
37 lines (27 loc) · 857 Bytes
/
Copy pathlcm.cs
File metadata and controls
37 lines (27 loc) · 857 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using static System.Console;
class LCM {
static void Main(String[] vars) {
int[] numbas = Array.ConvertAll(vars, s =>int.Parse(s));
int population = numbas.Length;
if (population > 1) {
int lcm = leastCommonMultiple(numbas, population);
Write("------\nThe least common multiple is: " + lcm);
} else {
WriteLine("Not enough numbers\nUsage:\nlcm.exe [your numbers here]");
}
}
static int leastCommonMultiple(int[] arr, int population) {
for (int i = 1; i < population; i++)
arr[i] = pairMultiple(arr[i - 1], arr[i]);
return arr[population - 1];
}
static int pairMultiple(int x, int y) {
int count = 1;
while (count <= Int32.MaxValue) {
if (count % x == 0 && count % y == 0) break;
count++;
}
return count;
}
}