Skip to content

Instantly share code, notes, and snippets.

@AndrewAllison
Created October 9, 2019 07:57
Show Gist options
  • Save AndrewAllison/1c10258fdf7a220dc962fe8f7ccc6185 to your computer and use it in GitHub Desktop.
Save AndrewAllison/1c10258fdf7a220dc962fe8f7ccc6185 to your computer and use it in GitHub Desktop.
Common C# test type thangs
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