-
-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from robkeim/grains
Add Grains solution
- Loading branch information
Showing
3 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
public class Grains | ||
{ | ||
public static ulong Square(int n) | ||
{ | ||
return n == 1 | ||
? 1 | ||
: 2 * Square(n - 1); | ||
} | ||
|
||
public static ulong Total() | ||
{ | ||
ulong total = 0; | ||
|
||
for (int i = 1; i <= 64; i++) | ||
{ | ||
total += Square(i); | ||
} | ||
|
||
return total; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using NUnit.Framework; | ||
|
||
[TestFixture] | ||
public class GrainsTest | ||
{ | ||
[Test] | ||
public void Test_square_1() | ||
{ | ||
Assert.That(Grains.Square(1), Is.EqualTo(1)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Test_square_2() | ||
{ | ||
Assert.That(Grains.Square(2), Is.EqualTo(2)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Test_square_3() | ||
{ | ||
Assert.That(Grains.Square(3), Is.EqualTo(4)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Test_square_4() | ||
{ | ||
Assert.That(Grains.Square(4), Is.EqualTo(8)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Test_square_16() | ||
{ | ||
Assert.That(Grains.Square(16), Is.EqualTo(32768)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Test_square_32() | ||
{ | ||
Assert.That(Grains.Square(32), Is.EqualTo(2147483648)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Test_square_64() | ||
{ | ||
Assert.That(Grains.Square(64), Is.EqualTo(9223372036854775808)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Test_total_grains() | ||
{ | ||
Assert.That(Grains.Total(), Is.EqualTo(18446744073709551615)); | ||
} | ||
} |