Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grains: fix approaches full code example #2181

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions exercises/practice/grains/.approaches/bit-shifting/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

```csharp
using System;
using System.Numerics; // for use with BigInteger
using System.Numerics; // Allows using BigInteger

public static class Grains
{
Expand All @@ -27,19 +27,20 @@ Instead of using math to calculate the number of grains on a square, you can sim
To understand how this works, consider just two squares that are represented in binary bits as `00`.

You use the [left-shift operator][left-shift-operator] to set `1` at the position needed to make the correct decimal value.

- To set the one grain on Square One you shift `1` for `0` positions to the left.
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.
The result is binary `01`, which is decimal `1`.
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.
The result is binary `01`, which is decimal `1`.
- To set the two grains on Square Two you shift `1` for `1` position to the left.
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.
The result is binary `10`, which is decimal `2`.
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.
The result is binary `10`, which is decimal `2`.

| Square | Shift Left By | Binary Value | Decimal Value |
| ------- | ------------- | ------------ | ------------- |
| 1 | 0 | 0001 | 1 |
| 2 | 1 | 0010 | 2 |
| 3 | 2 | 0100 | 4 |
| 4 | 3 | 1000 | 8 |
| Square | Shift Left By | Binary Value | Decimal Value |
| ------ | ------------- | ------------ | ------------- |
| 1 | 0 | 0001 | 1 |
| 2 | 1 | 0010 | 2 |
| 3 | 2 | 0100 | 4 |
| 4 | 3 | 1000 | 8 |

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

| Square | Binary Value | Decimal Value |
| ------- | ------------ | ------------- |
| 3 | 0100 | 4 |
| Square | Binary Value | Decimal Value |
| ------ | ------------ | ------------- |
| 3 | 0100 | 4 |

| Square | Sum Binary Value | Sum Decimal Value |
| ------- | ---------------- | ----------------- |
| 1 | 0001 | 1 |
| 2 | 0011 | 3 |
| Square | Sum Binary Value | Sum Decimal Value |
| ------ | ---------------- | ----------------- |
| 1 | 0001 | 1 |
| 2 | 0011 | 3 |

## Shortening

Expand Down
19 changes: 16 additions & 3 deletions exercises/practice/grains/.approaches/max-value/content.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
# `System.UInt64.MaxValue` for Total
# `Int64.MaxValue` for Total

```csharp
public static ulong Total()
using System;

public static class Grains
{
return System.UInt64.MaxValue;
public static double Square(int i)
{
if (i is <= 0 or > 64)
throw new ArgumentOutOfRangeException(nameof(i));

return Math.Pow(2, i - 1);
}

public static double Total()
{
return UInt64.MaxValue;
}
}
```

Expand Down
1 change: 1 addition & 0 deletions exercises/practice/grains/.approaches/pow/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

```csharp
using System;
using System.Linq;

public static class Grains
{
Expand Down