diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2022-11-07 18:00:47 +0200 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2022-11-07 18:00:47 +0200 |
commit | f595946c3f7ebdb7d0cac5f96150c9a06edeb949 (patch) | |
tree | 2dc3cedfa733421059c2ac3db74c873a80f79bc4 /gcd.cs | |
parent | 106d7ffe8dd8e9164dde1878885d19aedb3b0d9a (diff) | |
download | gcd-f595946c3f7ebdb7d0cac5f96150c9a06edeb949.tar.gz |
Update C#
Diffstat (limited to 'gcd.cs')
-rw-r--r-- | gcd.cs | 47 |
1 files changed, 21 insertions, 26 deletions
@@ -1,32 +1,27 @@ using System; +using System.Linq; -namespace GCD { - class Program { - static uint gcd2(uint a, uint b) { - uint c; - while (b != 0) { - c = b; - b = a % b; - a = c; - } - return a; - } +class Program { + static ulong gcd2(ulong a, ulong b) { + ulong c; + while (b != 0) { + c = b; + b = a % b; + a = c; + } + return a; + } - static uint gcdn(uint [] n) { - uint r = n[0]; - for (int i = 1; i < n.Length; i++) - r = gcd2(r, n[i]); - return r; - } + static ulong gcdn(ulong[] nums) { + return nums.Aggregate(0UL, (gcd, n) => gcd2(gcd, n)); + } - static void Main(string [] argv) { - uint [] a = Array.ConvertAll<string, uint>(argv, - new Converter<string, uint> - (delegate(string s) {return uint.Parse(s);}) - ); + static void Main(string[] argv) { + ulong[] nums = Array.ConvertAll<string, ulong>( + argv, new Converter<string, ulong>( + delegate(string s) { return ulong.Parse(s); })); - Console.WriteLine("{0}", gcdn(a)); - } - } + if (nums.Length > 0) + Console.WriteLine("{0}", gcdn(nums)); + } } - |