-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcd.cs
52 lines (40 loc) · 1.08 KB
/
gcd.cs
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using static System.Console;
class GCD {
static void Main(String[] vars) {
int[] numbas = Array.ConvertAll(vars, s =>int.Parse(s));
if (numbas.Length > 1) {
Array.Sort(numbas);
WriteLine("The numbers you provided were:\n" + string.Join(",", numbas));
int gcdValue = numbas[0];
foreach(int numba in numbas) {
int a,
b;
a = numba;
gcdValue = gcd(a, gcdValue);
WriteLine("For " + numba + ":");
WriteLine("1 is a prime factor 1 time!");
for (b = 2; a > 1; b++) {
if (a % b == 0) {
int x = 0;
while (a % b == 0) {
a /= b;
x++;
}
WriteLine("{0} is a prime factor {1} times!", b, x);
}
}
}
WriteLine("The greatest common divisor is " + gcdValue);
} else {
WriteLine("Not enough numbers\nUsage:\ngcd.exe [your numbers here]");
}
}
static int gcd(int a, int b) {
while (a != 0 && b != 0) {
if (a > b) a %= b;
else b %= a;
}
return a | b;
}
}