Skip to content

Commit f9dd4c0

Browse files
grains: fix approaches full code example (#2181)
1 parent f710b31 commit f9dd4c0

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

exercises/practice/grains/.approaches/bit-shifting/content.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
```csharp
44
using System;
5-
using System.Numerics; // for use with BigInteger
5+
using System.Numerics; // Allows using BigInteger
66
77
public static class Grains
88
{
@@ -27,19 +27,20 @@ Instead of using math to calculate the number of grains on a square, you can sim
2727
To understand how this works, consider just two squares that are represented in binary bits as `00`.
2828

2929
You use the [left-shift operator][left-shift-operator] to set `1` at the position needed to make the correct decimal value.
30+
3031
- To set the one grain on Square One you shift `1` for `0` positions to the left.
31-
So, if `n` is `1` for square One, you subtract `n` by `1` to get `0`, which will not move it any positions to the left.
32-
The result is binary `01`, which is decimal `1`.
32+
So, if `n` is `1` for square One, you subtract `n` by `1` to get `0`, which will not move it any positions to the left.
33+
The result is binary `01`, which is decimal `1`.
3334
- To set the two grains on Square Two you shift `1` for `1` position to the left.
34-
So, if `n` is `2` for square Two, you subtract `n` by `1` to get `1`, which will move it `1` position to the left.
35-
The result is binary `10`, which is decimal `2`.
35+
So, if `n` is `2` for square Two, you subtract `n` by `1` to get `1`, which will move it `1` position to the left.
36+
The result is binary `10`, which is decimal `2`.
3637

37-
| Square | Shift Left By | Binary Value | Decimal Value |
38-
| ------- | ------------- | ------------ | ------------- |
39-
| 1 | 0 | 0001 | 1 |
40-
| 2 | 1 | 0010 | 2 |
41-
| 3 | 2 | 0100 | 4 |
42-
| 4 | 3 | 1000 | 8 |
38+
| Square | Shift Left By | Binary Value | Decimal Value |
39+
| ------ | ------------- | ------------ | ------------- |
40+
| 1 | 0 | 0001 | 1 |
41+
| 2 | 1 | 0010 | 2 |
42+
| 3 | 2 | 0100 | 4 |
43+
| 4 | 3 | 1000 | 8 |
4344

4445
For `Total` we want all of the 64 bits set to `1` to get the sum of grains on all sixty-four squares.
4546
The easy way to do this is to set the 65th bit to `1` and then subtract `1`.
@@ -48,14 +49,14 @@ To go back to our two-square example, if we can grow to three squares, then we c
4849
which is decimal `4`.
4950
By subtracting `1` we get `3`, which is the total amount of grains on the two squares.
5051

51-
| Square | Binary Value | Decimal Value |
52-
| ------- | ------------ | ------------- |
53-
| 3 | 0100 | 4 |
52+
| Square | Binary Value | Decimal Value |
53+
| ------ | ------------ | ------------- |
54+
| 3 | 0100 | 4 |
5455

55-
| Square | Sum Binary Value | Sum Decimal Value |
56-
| ------- | ---------------- | ----------------- |
57-
| 1 | 0001 | 1 |
58-
| 2 | 0011 | 3 |
56+
| Square | Sum Binary Value | Sum Decimal Value |
57+
| ------ | ---------------- | ----------------- |
58+
| 1 | 0001 | 1 |
59+
| 2 | 0011 | 3 |
5960

6061
## Shortening
6162

exercises/practice/grains/.approaches/max-value/content.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1-
# `System.UInt64.MaxValue` for Total
1+
# `Int64.MaxValue` for Total
22

33
```csharp
4-
public static ulong Total()
4+
using System;
5+
6+
public static class Grains
57
{
6-
return System.UInt64.MaxValue;
8+
public static double Square(int i)
9+
{
10+
if (i is <= 0 or > 64)
11+
throw new ArgumentOutOfRangeException(nameof(i));
12+
13+
return Math.Pow(2, i - 1);
14+
}
15+
16+
public static double Total()
17+
{
18+
return UInt64.MaxValue;
19+
}
720
}
821
```
922

exercises/practice/grains/.approaches/pow/content.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
```csharp
44
using System;
5+
using System.Linq;
56

67
public static class Grains
78
{

0 commit comments

Comments
 (0)