Skip to content

Commit

Permalink
Update mentoring.md
Browse files Browse the repository at this point in the history
removing space before colon
adding a direct implementation for total
adding common issues and suggestions section
  • Loading branch information
CornierQuentin authored Apr 8, 2024
1 parent 793edd7 commit cd0bc23
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions tracks/cpp/exercises/grains/mentoring.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# Mentoring

## First function
## Reasonable solutions

The first way to implement this function is to use a for loop :
### First function

The first way to implement this function is to use a for loop:
```cpp
unsigned long long square(int index) {
unsigned long long result = 1;
for (int i = 0; i < index; i++) result *= 2;
return result;
}
```
Another good way to do this is to use a recursive function :
Another good way to do this is to use a recursive function:
```cpp
unsigned long long square(int index) {
if (index == 1) return 1;
Expand All @@ -19,7 +21,7 @@ unsigned long long square(int index) {
```
This way, the code is shorter and in my mind, easier to read.

The last option and the more direct one is :
The last option and the more direct one is:
```cpp
unsigned long long square(int index)
{
Expand All @@ -28,19 +30,19 @@ unsigned long long square(int index)
```
This code use left bitshifting. Left bitshifting consists in adding 0 at the end of the binary writing of the number.
If you do ```5 << 2```, you are taking 5, which is 101 in binary, and you shift the bits by 2, so you end up with 10100 and that equal to 20.
If you do `5 << 2`, you are taking 5, which is 101 in binary, and you shift the bits by 2, so you end up with 10100 and that equal to 20.
## Second function
At first sight, we want to use the first function to do it easily :
### Second function
At first sight, we want to use the first function to do it easily:
```cpp
unsigned long long total() {
unsigned long long sum = 0;
for (int i = 1; i <= 64; i++) sum += square(i);
return sum;
}
```
However with this solution, each time you call the function square with the index i, you do i products.
In the following solution, you only do 64 products and by adding to the sum each time you multiply by 2 :
However with this solution only works with a direct implementation of the first function, else each time you call the function square with the index i, you do i products.
In the following solution, you only do 64 products and by adding to the sum each time you multiply by 2:
```cpp
unsigned long long total() {
unsigned long long total = 0;
Expand All @@ -54,3 +56,24 @@ unsigned long long total() {
return total;
}
```
Moreover, this is also possible to use a direct implementation for this function:
```cpp
unsigned long long total() {
return ~0ULL;
}
```
With this way we use the fact that a ULL is 64 bits long and we use the ~ to do the complement.

## Common issues

- Not using unsigned long long types
- Using ULLONG_MAX, for the second function, which works, however this is like returning directly the good number
- Using std::pow that implies conversion from float to int and could lead to errors mainly in the total function
- Hard coding the solution for the total

## Common suggestions

- Use the square function in total only if square use a direct implementation
- The optimal way is to use bitwise operations


0 comments on commit cd0bc23

Please sign in to comment.