Skip to content

Commit

Permalink
Stylistic changes + new math tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Jul 31, 2024
1 parent 16352da commit 971158a
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 52 deletions.
53 changes: 52 additions & 1 deletion csharp/Euler.Test/test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,62 @@ public async Task EulerTest_Problem(Type problem, bool isSlow, object expected)
if (prob is null)
throw new Exception("Unable to construct test object");
Stopwatch sw = Stopwatch.StartNew();
object result = await prob.Answer();
object result = await Task.Run(() => prob.Answer());
sw.Stop();
Assert.Equal(expected, result);
if (!isSlow)
Assert.True(sw.Elapsed <= oneMinute);
}

[Fact]
public void EulerTest_Mathematics_Factorial()
{
List<ulong> results = new() {
1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600,6227020800, 87178291200,
1307674368000, 20922789888000, 355687428096000, 6402373705728000, 121645100408832000,
2432902008176640000
};
for (byte i = 0; i < results.Count; i += 1)
{
ulong expected = results[i];
Assert.Equal(expected, Mathematics.Factorial(i));
}
}

[Fact]
public async Task EulerTest_Mathematics_NChooseR()
{
List<List<ulong>> results = new() {
new() { 1, 1 },
new() { 1, 2, 1 },
new() { 1, 3, 3, 1 },
new() { 1, 4, 6, 4, 1 },
new() { 1, 5, 10, 10, 5, 1 },
new() { 1, 6, 15, 20, 15, 6, 1 },
new() { 1, 7, 21, 35, 35, 21, 7, 1 },
new() { 1, 8, 28, 56, 70, 56, 28, 8, 1 },
new() { 1, 9, 36, 84, 126, 126, 84, 36, 9, 1 },
new() { 1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1 },
new() { 1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1 },
new() { 1, 12, 66, 220, 495, 792, 924, 792, 495, 220, 66, 12, 1 },
new() { 1, 13, 78, 286, 715, 1287, 1716, 1716, 1287, 715, 286, 78, 13, 1 },
new() { 1, 14, 91, 364, 1001, 2002, 3003, 3432, 3003, 2002, 1001, 364, 91, 14, 1 },
new() { 1, 15, 105, 455, 1365, 3003, 5005, 6435, 6435, 5005, 3003, 1365, 455, 105, 15, 1 },
new() { 1, 16, 120, 560, 1820, 4368, 8008, 11440, 12870, 11440, 8008, 4368, 1820, 560, 120, 16, 1 },
new() { 1, 17, 136, 680, 2380, 6188, 12376, 19448, 24310, 24310, 19448, 12376, 6188, 2380, 680, 136, 17, 1 },
new() { 1, 18, 153, 816, 3060, 8568, 18564, 21824, 43758, 48620, 43758, 21824, 18564, 8568, 3060, 816, 153, 18, 1 },
};
List<Task> tasks = new();
for (byte i = 0; i < results.Count; i += 1)
{
for (byte j = 0; j < results[i].Count; j += 1)
{
ulong expected = results[i][j];
tasks.Append(Task.Run(() => Assert.Equal(expected, Mathematics.NChooseR(i, j))));
}
}
foreach (Task task in tasks)
await task;
}
}
}
15 changes: 7 additions & 8 deletions csharp/Euler/include/math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,28 @@ public static class Mathematics
public static ulong Factorial(ulong n)
{
ulong answer = 1;
for (ulong x = 2; x < n; x += 1)
for (ulong x = 2; x <= n; x += 1)
answer *= x;
return answer;
}

public static ulong NChooseR(ulong n, ulong r)
{
if (n < 20)
if (n <= 20)
return Factorial(n) / Factorial(r) / Factorial(n - r);
ulong answer, tmp;
uint i, j;
sbyte[] factors = new sbyte[n + 1];
// collect factors of final number
for (i = 2; i <= n; i += 1)
factors[i] = 1;

// negative factor values indicate need to divide
for (i = 2; i <= r; i += 1)
{
factors[i] -= 1;
}
for (i = 2; i <= n - r; i += 1)
{
factors[i] -= 1;
}

// this loop reduces to prime factors only
for (i = (uint)n; i > 1; i -= 1)
{
Expand All @@ -45,6 +43,7 @@ public static ulong NChooseR(ulong n, ulong r)
}
}
}

i = j = 2;
answer = 1;
while (i <= n)
Expand All @@ -64,13 +63,12 @@ public static ulong NChooseR(ulong n, ulong r)
answer = tmp * i;
}
if (answer < tmp)
{
return ulong.MaxValue; // this indicates an overflow
}
factors[i] -= 1;
}
i += 1;
}

while (j <= n)
{
while (factors[j] < 0)
Expand All @@ -80,6 +78,7 @@ public static ulong NChooseR(ulong n, ulong r)
}
j += 1;
}

return answer;
}
}
Expand Down
6 changes: 3 additions & 3 deletions csharp/Euler/p0000_template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ namespace Euler
{
public interface IEuler
{
public Task<object> Answer();
public object Answer();
}
public class p0000 : IEuler
{
public Task<object> Answer()
public object Answer()
{
return Task.FromResult<object>(0);
return 0;
}
}
}
4 changes: 2 additions & 2 deletions csharp/Euler/p0001.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Euler
{
public class p0001 : IEuler
{
public Task<object> Answer()
public object Answer()
{
int answer = 0;
for (int i = 0; i < 1000; i += 3)
Expand All @@ -30,7 +30,7 @@ public Task<object> Answer()
for (int i = 0; i < 1000; i += 15)
answer -= i;

return Task.FromResult<object>(answer);
return answer;
}
}
}
4 changes: 2 additions & 2 deletions csharp/Euler/p0002.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Euler
{
public class p0002 : IEuler
{
public Task<object> Answer()
public object Answer()
{
int answer = 0,
i = 2,
Expand All @@ -35,7 +35,7 @@ public Task<object> Answer()
j = tmp;
}

return Task.FromResult<object>(answer);
return answer;
}
}
}
6 changes: 2 additions & 4 deletions csharp/Euler/p0004.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private static bool IsPalindrome(int x)
return rep == Reverse(rep);
}

public Task<object> Answer()
public object Answer()
{
int answer = 0;
for (int v = 101; v < 1000; v++)
Expand All @@ -40,13 +40,11 @@ public Task<object> Answer()
{
int p = u * v;
if (IsPalindrome(p) && p > answer)
{
answer = p;
}
}
}

return Task.FromResult<object>(answer);
return answer;
}
}
}
5 changes: 3 additions & 2 deletions csharp/Euler/p0006.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Euler
{
public class p0006 : IEuler
{
public Task<object> Answer()
public object Answer()
{
int sum_of_squares = 0,
sum = 0;
Expand All @@ -32,8 +32,9 @@ public Task<object> Answer()
sum += i;
sum_of_squares += i * i;
}

int square_of_sum = sum * sum;
return Task.FromResult<object>(square_of_sum - sum_of_squares);
return square_of_sum - sum_of_squares;
}
}
}
9 changes: 3 additions & 6 deletions csharp/Euler/p0008.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace Euler
public class p0008 : IEuler
{
private const int MAX = 500;
public Task<object> Answer()
public object Answer()
{
String str = String.Concat(
"73167176531330624919225119674426574742355349194934",
Expand Down Expand Up @@ -70,15 +70,12 @@ public Task<object> Answer()
String slice = str.Substring(i, 13);
Int64 prod = 1;
foreach (char c in slice)
{
prod *= c - '0';
}

if (prod > answer)
{
answer = prod;
}
}
return Task.FromResult<object>(answer);
return answer;
}
}
}
6 changes: 2 additions & 4 deletions csharp/Euler/p0009.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Euler
{
public class p0009 : IEuler
{
public Task<object> Answer()
public object Answer()
{
for (int c = 3; ; c++)
{
Expand All @@ -31,9 +31,7 @@ public Task<object> Answer()
{
int a_square = a * a;
if (a_square + b_square == c_square && a + b + c == 1000)
{
return Task.FromResult<object>(a * b * c);
}
return a * b * c;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions csharp/Euler/p0011.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Euler
{
public class p0011 : IEuler
{
public Task<object> Answer()
public object Answer()
{
int answer = 0, tmp;
for (byte i = 0; i < 20; i++)
Expand All @@ -50,7 +50,7 @@ public Task<object> Answer()
answer = Math.Max(answer, tmp);
}
}
return Task.FromResult<object>(answer);
return answer;
}

byte[][] grid = new byte[20][]{
Expand Down
13 changes: 3 additions & 10 deletions csharp/Euler/p0014.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Euler
{
public class p0014 : IEuler
{
public Task<object> Answer()
public object Answer()
{
int biggestSeen = 0;
long biggestIdx = 0;
Expand All @@ -40,28 +40,21 @@ public Task<object> Answer()
biggestIdx = x;
}
}
return Task.FromResult<object>((int)biggestIdx);
return (int)biggestIdx;
}

static int CollatzLen(long n, IDictionary<long, int> cache)
{
if (n == 1)
{
return 0;
}
else if (cache.ContainsKey(n))
{
return cache[n];
}

int result;
if (n % 2 == 0)
{
result = 1 + CollatzLen(n / 2, cache);
}
else
{
result = 2 + CollatzLen((3 * n + 1) / 2, cache);
}
cache.Add(n, result);
return result;
}
Expand Down
4 changes: 2 additions & 2 deletions csharp/Euler/p0015.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ namespace Euler
{
public class p0015 : IEuler
{
public Task<object> Answer()
public object Answer()
{
return Task.FromResult<object>((long)Mathematics.NChooseR(40, 20));
return (long)Mathematics.NChooseR(40, 20);
}
}
}
4 changes: 2 additions & 2 deletions csharp/Euler/p0017.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ namespace Euler
{
public class p0017 : IEuler
{
public Task<object> Answer()
public object Answer()
{
int answer = 0;
for (int x = 1; x < 1001; x += 1)
{
string str = to_string(x);
answer += str.Replace(" ", "").Replace("-", "").Length;
}
return Task.FromResult<object>(answer);
return answer;
}

static String to_string(int n)
Expand Down
4 changes: 2 additions & 2 deletions csharp/Euler/p0076.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Euler
{
public class p0076 : IEuler
{
public Task<object> Answer()
public object Answer()
{
ushort idx;
int answer = 0;
Expand Down Expand Up @@ -54,7 +54,7 @@ public Task<object> Answer()
}
sum = Enumerable.Sum(counts);
}
return Task.FromResult<object>(answer);
return answer;
}
}
}
4 changes: 2 additions & 2 deletions csharp/Euler/p0836.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ namespace Euler
{
public class p0836 : IEuler
{
public Task<object> Answer()
public object Answer()
{
return Task.FromResult<object>("aprilfoolsjoke");
return "aprilfoolsjoke";
}
}
}

0 comments on commit 971158a

Please sign in to comment.