From 1f9f75f959a3576a469723e7159523368691580b Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Wed, 27 Sep 2023 17:50:26 +0200 Subject: [PATCH] grains: fix approaches full code example --- .../.approaches/bit-shifting/content.md | 37 ++++++++++--------- .../grains/.approaches/max-value/content.md | 19 ++++++++-- .../grains/.approaches/pow/content.md | 1 + 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/exercises/practice/grains/.approaches/bit-shifting/content.md b/exercises/practice/grains/.approaches/bit-shifting/content.md index 6d6da172fa..892d8a48c7 100644 --- a/exercises/practice/grains/.approaches/bit-shifting/content.md +++ b/exercises/practice/grains/.approaches/bit-shifting/content.md @@ -2,7 +2,7 @@ ```csharp using System; -using System.Numerics; // for use with BigInteger +using System.Numerics; // Allows using BigInteger public static class Grains { @@ -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`. @@ -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 diff --git a/exercises/practice/grains/.approaches/max-value/content.md b/exercises/practice/grains/.approaches/max-value/content.md index 8e101ce928..f6334aa52b 100644 --- a/exercises/practice/grains/.approaches/max-value/content.md +++ b/exercises/practice/grains/.approaches/max-value/content.md @@ -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; + } } ``` diff --git a/exercises/practice/grains/.approaches/pow/content.md b/exercises/practice/grains/.approaches/pow/content.md index 2e3baa06dc..2418713306 100644 --- a/exercises/practice/grains/.approaches/pow/content.md +++ b/exercises/practice/grains/.approaches/pow/content.md @@ -2,6 +2,7 @@ ```csharp using System; +using System.Linq; public static class Grains {