Skip to content

Commit b6c05c0

Browse files
Merge pull request #89 from robkeim/grains
Add Grains solution
2 parents ba352ae + 54cc69e commit b6c05c0

3 files changed

Lines changed: 90 additions & 1 deletion

File tree

config.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"rna-transcription",
1313
"sum-of-multiples",
1414
"space-age",
15+
"grains",
1516
"hamming",
1617
"raindrops",
1718
"nucleotide-count",
@@ -131,6 +132,13 @@
131132
"Filtering"
132133
]
133134
},
135+
{
136+
"slug": "grains",
137+
"difficulty": 2,
138+
"topics": [
139+
"Integers"
140+
]
141+
},
134142
{
135143
"slug": "raindrops",
136144
"difficulty": 2,
@@ -307,7 +315,7 @@
307315
{
308316
"slug": "allergies",
309317
"difficulty": 4,
310-
"topics": [
318+
"topics": [
311319
"Bitwise operations",
312320
"Filtering"
313321
]

exercises/grains/Example.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Grains
2+
{
3+
public static ulong Square(int n)
4+
{
5+
return n == 1
6+
? 1
7+
: 2 * Square(n - 1);
8+
}
9+
10+
public static ulong Total()
11+
{
12+
ulong total = 0;
13+
14+
for (int i = 1; i <= 64; i++)
15+
{
16+
total += Square(i);
17+
}
18+
19+
return total;
20+
}
21+
}

exercises/grains/GrainsTest.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using NUnit.Framework;
2+
3+
[TestFixture]
4+
public class GrainsTest
5+
{
6+
[Test]
7+
public void Test_square_1()
8+
{
9+
Assert.That(Grains.Square(1), Is.EqualTo(1));
10+
}
11+
12+
[Ignore("Remove to run test")]
13+
[Test]
14+
public void Test_square_2()
15+
{
16+
Assert.That(Grains.Square(2), Is.EqualTo(2));
17+
}
18+
19+
[Ignore("Remove to run test")]
20+
[Test]
21+
public void Test_square_3()
22+
{
23+
Assert.That(Grains.Square(3), Is.EqualTo(4));
24+
}
25+
26+
[Ignore("Remove to run test")]
27+
[Test]
28+
public void Test_square_4()
29+
{
30+
Assert.That(Grains.Square(4), Is.EqualTo(8));
31+
}
32+
33+
[Ignore("Remove to run test")]
34+
[Test]
35+
public void Test_square_16()
36+
{
37+
Assert.That(Grains.Square(16), Is.EqualTo(32768));
38+
}
39+
40+
[Ignore("Remove to run test")]
41+
[Test]
42+
public void Test_square_32()
43+
{
44+
Assert.That(Grains.Square(32), Is.EqualTo(2147483648));
45+
}
46+
47+
[Ignore("Remove to run test")]
48+
[Test]
49+
public void Test_square_64()
50+
{
51+
Assert.That(Grains.Square(64), Is.EqualTo(9223372036854775808));
52+
}
53+
54+
[Ignore("Remove to run test")]
55+
[Test]
56+
public void Test_total_grains()
57+
{
58+
Assert.That(Grains.Total(), Is.EqualTo(18446744073709551615));
59+
}
60+
}

0 commit comments

Comments
 (0)