2
2
3
3
``` csharp
4
4
using System ;
5
- using System .Numerics ; // for use with BigInteger
5
+ using System .Numerics ; // Allows using BigInteger
6
6
7
7
public static class Grains
8
8
{
@@ -27,19 +27,20 @@ Instead of using math to calculate the number of grains on a square, you can sim
27
27
To understand how this works, consider just two squares that are represented in binary bits as ` 00 ` .
28
28
29
29
You use the [ left-shift operator] [ left-shift-operator ] to set ` 1 ` at the position needed to make the correct decimal value.
30
+
30
31
- 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 ` .
33
34
- 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 ` .
36
37
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 |
43
44
44
45
For ` Total ` we want all of the 64 bits set to ` 1 ` to get the sum of grains on all sixty-four squares.
45
46
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
48
49
which is decimal ` 4 ` .
49
50
By subtracting ` 1 ` we get ` 3 ` , which is the total amount of grains on the two squares.
50
51
51
- | Square | Binary Value | Decimal Value |
52
- | ------- | ------------ | ------------- |
53
- | 3 | 0100 | 4 |
52
+ | Square | Binary Value | Decimal Value |
53
+ | ------ | ------------ | ------------- |
54
+ | 3 | 0100 | 4 |
54
55
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 |
59
60
60
61
## Shortening
61
62
0 commit comments