From f595946c3f7ebdb7d0cac5f96150c9a06edeb949 Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Mon, 7 Nov 2022 18:00:47 +0200 Subject: Update C# --- gcd.cs | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/gcd.cs b/gcd.cs index 0825cea..325e27a 100644 --- a/gcd.cs +++ b/gcd.cs @@ -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(argv, - new Converter - (delegate(string s) {return uint.Parse(s);}) - ); + static void Main(string[] argv) { + ulong[] nums = Array.ConvertAll( + argv, new Converter( + delegate(string s) { return ulong.Parse(s); })); - Console.WriteLine("{0}", gcdn(a)); - } - } + if (nums.Length > 0) + Console.WriteLine("{0}", gcdn(nums)); + } } - -- cgit v1.2.3