Created
October 9, 2019 07:57
-
-
Save AndrewAllison/1c10258fdf7a220dc962fe8f7ccc6185 to your computer and use it in GitHub Desktop.
Common C# test type thangs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
/// <summary> | |
/// Calculate the number of grains of wheat on a chessboard given that the number on each square doubles. | |
/// </summary> | |
public static class Grains | |
{ | |
public static ulong Square(int n) | |
{ | |
if (n <= 0 || n > 64) | |
throw new ArgumentOutOfRangeException(); | |
return (ulong)Math.Pow(2, n - 1); | |
} | |
public static ulong Total() | |
{ | |
ulong total = 0; | |
for (var i = 1; i < 65; i++) | |
total += Square(i); | |
return total; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment