diff --git a/csharp/Euler/include/utils.cs b/csharp/Euler/include/utils.cs index 502ff5fa..b247d277 100644 --- a/csharp/Euler/include/utils.cs +++ b/csharp/Euler/include/utils.cs @@ -44,6 +44,7 @@ public static object GetAnswer(ulong n) case 64: return long.Parse(arr[3]); } + break; case "uint": switch (int.Parse(arr[2])) { @@ -56,6 +57,7 @@ public static object GetAnswer(ulong n) case 64: return ulong.Parse(arr[3]); } + break; } } throw new IOException(); diff --git a/csharp/Euler/p0004.cs b/csharp/Euler/p0004.cs index 51a7ad8a..f407535b 100644 --- a/csharp/Euler/p0004.cs +++ b/csharp/Euler/p0004.cs @@ -33,7 +33,7 @@ private static bool IsPalindrome(uint x) public object Answer() { - int answer = 0; + uint answer = 0; for (uint v = 101; v < 1000; v++) { for (uint u = 100; u < v; u++) diff --git a/csharp/Euler/p0008.cs b/csharp/Euler/p0008.cs index 5a2556c9..70dd3161 100644 --- a/csharp/Euler/p0008.cs +++ b/csharp/Euler/p0008.cs @@ -70,7 +70,7 @@ public object Answer() String slice = str.Substring(i, 13); ulong prod = 1; foreach (char c in slice) - prod *= c - '0'; + prod *= (ulong)c - '0'; if (prod > answer) answer = prod; diff --git a/csharp/Euler/p0011.cs b/csharp/Euler/p0011.cs index 65d8b809..34ad462d 100644 --- a/csharp/Euler/p0011.cs +++ b/csharp/Euler/p0011.cs @@ -31,10 +31,10 @@ public object Answer() for (byte j = 0; j < 17; j++) { // horizontal section - tmp = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3]; + tmp = (uint)grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3]; answer = Math.Max(answer, tmp); // vertical section - tmp = grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i]; + tmp = (uint)grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i]; answer = Math.Max(answer, tmp); } } @@ -43,10 +43,10 @@ public object Answer() for (byte j = 0; j < 17; j++) { // right diagonal section - tmp = grid[i][j] * grid[i + 1][j + 1] * grid[i + 2][j + 2] * grid[i + 3][j + 3]; + tmp = (uint)grid[i][j] * grid[i + 1][j + 1] * grid[i + 2][j + 2] * grid[i + 3][j + 3]; answer = Math.Max(answer, tmp); // left diagonal section - tmp = grid[i][j + 3] * grid[i + 1][j + 2] * grid[i + 2][j + 1] * grid[i + 3][j]; + tmp = (uint)grid[i][j + 3] * grid[i + 1][j + 2] * grid[i + 2][j + 1] * grid[i + 3][j]; answer = Math.Max(answer, tmp); } } diff --git a/csharp/Euler/p0014.cs b/csharp/Euler/p0014.cs index 4f5fdb87..56436e39 100644 --- a/csharp/Euler/p0014.cs +++ b/csharp/Euler/p0014.cs @@ -31,9 +31,9 @@ public object Answer() uint biggestSeen = 0; ulong biggestIdx = 0; Dictionary cache = new(); - for (long x = 1; x < 1000000; x += 1) + for (ulong x = 1; x < 1000000; x += 1) { - int result = CollatzLen(x, cache); + uint result = CollatzLen(x, cache); if (result > biggestSeen) { biggestSeen = result; @@ -43,7 +43,7 @@ public object Answer() return (uint)biggestIdx; } - static uint CollatzLen(long n, IDictionary cache) + static uint CollatzLen(ulong n, IDictionary cache) { if (n == 1) return 0; diff --git a/csharp/Euler/p0017.cs b/csharp/Euler/p0017.cs index 029910dc..4af16fa3 100644 --- a/csharp/Euler/p0017.cs +++ b/csharp/Euler/p0017.cs @@ -25,16 +25,16 @@ public class p0017 : IEuler { public object Answer() { - ushort answer = 0; - for (ushort x = 1; x < 1001; x += 1) + int answer = 0; + for (int x = 1; x < 1001; x += 1) { string str = to_string(x); answer += str.Replace(" ", "").Replace("-", "").Length; } - return answer; + return (ushort)answer; } - static String to_string(ushort n) + static String to_string(int n) { if (n >= 1000) { @@ -109,3 +109,4 @@ static String to_string(ushort n) } } } + diff --git a/csharp/Euler/p0022.cs b/csharp/Euler/p0022.cs index 8d945621..f954285e 100644 --- a/csharp/Euler/p0022.cs +++ b/csharp/Euler/p0022.cs @@ -34,7 +34,7 @@ public object Answer() uint sum = 0; foreach (char c in names[i]) { - sum += c & 0x3F; + sum += (ushort)c & 0x3Fu; } answer += sum * (i + 1); } diff --git a/csharp/Euler/p0034.cs b/csharp/Euler/p0034.cs index b5a62f0c..cbaeb357 100644 --- a/csharp/Euler/p0034.cs +++ b/csharp/Euler/p0034.cs @@ -22,7 +22,7 @@ public class p0034 : IEuler { public object Answer() { - ushort answer = 0; + uint answer = 0; for (uint x = 10; x < 100000; x += 1) { string xs = x.ToString(); @@ -36,7 +36,7 @@ public object Answer() answer += x; } } - return answer; + return (ushort)answer; } } } diff --git a/csharp/Euler/p0076.cs b/csharp/Euler/p0076.cs index 35d18d9d..7b4f2edd 100644 --- a/csharp/Euler/p0076.cs +++ b/csharp/Euler/p0076.cs @@ -26,7 +26,7 @@ public class p0076 : IEuler { public object Answer() { - ushort idx; + byte idx; uint answer = 0; byte sum = 100; byte[] counts = new byte[101]; @@ -44,11 +44,13 @@ public object Answer() idx += 1; counts[idx] += idx; sum = 0; - for (int i = idx - 1; i < 101; i += 1) + for (byte i = idx - (byte)1; i < 101; i += (byte)1) sum += counts[i]; } while (sum > 100); } - sum = Enumerable.Sum(counts); + sum = 0; + for (byte i = 0; i < 101; i += 1) + sum += counts[i]; } return answer; }