Skip to content

Commit 65595cd

Browse files
committed
Chapter 9.1 - Grammarly + Upper Letter Titles
1 parent 9d30cd9 commit 65595cd

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

chapter-09-problems-for-champions.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ We have two sequences:
1313
- **a sequence of Tribonacci** (by analogy with the Fibonacci sequence), where each number is **the sum of the previous three** (with given three numbers)
1414
- a sequence generated by a **numerical spiral** defined by looping like a spiral (right, bottom, left, top, right, bottom, left, top, etc.) of a matrix of numbers starting from its center with a given starting number and incremental step, by storing the current numbers in the sequence each time we make a turn.
1515

16-
Write a program that finds the first number that appears **in both sequences defined in the aforementioned way**.
16+
Write a program that finds the first number that appears **in both sequences defined in an aforementioned way**.
1717

1818
### Problem
1919

2020
Let **the Tribonacci sequence** start with **1**, **2** and **3**. This means that **the first sequence** will contain the numbers 1, 2, 3, 6, 11, 20, 37, 68, 125, 230, 423, 778, 1431, 2632, 4841, 8904, 16377, 30122, 55403, 101902, and so on.
2121

22-
At the same time, let the **numbers in the spiral** begin with **5** and the spiral increases by **2** at each step.
22+
At the same time, let the **numbers in the spiral** begin with **5**, and the spiral increases by **2** at each step.
2323

2424
<img src="assets/chapter-9-1-images/01.Crossing-sequences-01.png" style="float: right; height: 300px;" />
2525

26-
Then **the second sequence** will contain the numbers 5, 7, 9, 13, 17, 23, 29, 37 and so on. We see that **37** is the first number to be found in the Tribonacci sequence and the spiral one, and that is the desired solution to the problem.
26+
Then **the second sequence** will contain the numbers 5, 7, 9, 13, 17, 23, 29, 37, and so on. We see that **37** is the first number to be found in the Tribonacci sequence and the spiral one, and that is the desired solution to the problem.
2727

2828
### Input Data
2929

@@ -59,7 +59,7 @@ On the single line of the output, we must print **the smallest number that occur
5959

6060
The problem seems quite complicated, so we will break it into simpler sub-problems.
6161

62-
#### Processing the Input
62+
#### Processing The Input
6363

6464
The first step in solving the problem is to read and process the input. Input data consists of **5 integers**: **3** for the Tribonacci sequence and **2** for the numerical spiral:
6565

@@ -75,7 +75,7 @@ For the Tribonacci sequence, we will always **collect the previous three values*
7575

7676
#### Generating Numerical Spiral
7777

78-
We need to think of **a relation** between numbers in the numerical spiral so we can easily generate every next number without having to look at matrices and loop through them. If we carefully look at the picture from the description, we will notice that **every 2 "turns" in the spiral, the numbers we skip are increased by 1**, i.e. from *5 to 7* and from *7 to 9*, not a single number is skipped, but we directly **add with the step** of the sequence. From *9 to 13* and from *13 to 17* we skip a number, i.e. we add the step twice. From *17 to 23* and from *23 to 29* we skip two numbers, i.e. we add the step three times and so on.
78+
We need to think of **a relationship** between numbers in the numerical spiral so we can easily generate every next number without having to look at matrices and loop through them. If we carefully look at the picture from the description, we will notice that **every 2 "turns" in the spiral, the numbers we skip are increased by 1**, i.e. from *5 to 7* and from *7 to 9*, not a single number is skipped, but we directly **add with the step** of the sequence. From *9 to 13* and from *13 to 17* we skip a number, i.e. we add the step twice. From *17 to 23* and from *23 to 29* we skip two numbers, i.e. we add the step three times and so on.
7979

8080
Thus, we see that for the first two we have **`the last number + 1 * the step`**, the next two we add with the **`2 * the step`** and so on. Every time we want to get to the next number of the spiral, we will have to make such calculations:
8181

@@ -155,19 +155,19 @@ For looping through the dates, we will take advantage of the functionality that
155155

156156
Once we have the start date, we want to create a **loop that runs until we exceed the final year** (or until we pass December 31 in the final year if we compare the full dates), increasing each day by 1 day.
157157

158-
To increase by one day in each rotation, we will use a method of **`Date` - `setDate(…)`**, which will add one day to the current date. The method will take care instead of us, to decide where to skip the next month, how many days there is a month and everything around the leap years:
158+
To increase by one day in each rotation, we will use a method of **`Date` - `setDate(…)`**, which will add one day to the current date. The method will take care instead of us, to decide where to skip the next month, how many days there is a month, and everything around the leap years:
159159

160160
![](/assets/chapter-9-1-images/02.Magic-dates-03.png)
161161

162-
In JavaScript, we have to use the method **`getFullYear()`** to get the year in the same format as it is in the input data. If we use the method **`getYear()`** we will get the number of years passed from 1900 until the searched date, which will not help us with our problem. In conclusion, our loop should look like that:
162+
In JavaScript, we have to use the method **`getFullYear()`** to get the year in the same format as it is in the input data. If we use the method **`getYear()`** we will get the number of years passed from 1900 until the searched data, which will not help us with our problem. In conclusion, our loop should look like that:
163163

164164
![](/assets/chapter-9-1-images/02.Magic-dates-04.png)
165165

166166
**Caution**: we can do the same thing using **`for` loop**, the initialization of the date will be in the first past of the **`for`**, the condition stays the same, and the step is incremented with one day.
167167

168168
#### Calculating Date Weight
169169

170-
Each date consists of exactly **8 characters (digits)** – **2 for the day** (**`d1`**, **`d2`**), **2 for the month** (**`d3`**, **`d4`**) and **4 for the year** (**`d5`** to **`d8`**). This means that we will always have the same calculation every time, and we can benefit from this **to define the formula statically** (i.e. not to use loops, referring to different numbers from the date, but write the whole formula). To be able to write it, we will need **all digits from the date** in individual variables to make all the necessary multiplications. By using the division and partition operations on the individual components of the date, using the **`getDay()`**, **`getMonth()`** and **`getFullYear()`** methods, we can retrieve each digit. We have to pay attention to **`getMonth()`**, because the method returns a number between 0 (January) and 11 (December) and we have to add **+1**, to get the months between **`[1-12]`**. Another thing that we have to be careful with is the division by 10 (**`/ 10`**), which won't be an integer division, that's why after each operation we have to round specifically to the lowest integer number by using **`Math.floor(…)`**:
170+
Each date consists of exactly **8 characters (digits)** – **2 for the day** (**`d1`**, **`d2`**), **2 for the month** (**`d3`**, **`d4`**), and **4 for the year** (**`d5`** to **`d8`**). This means that we will always have the same calculation every time, and we can benefit from this **to define the formula statically** (i.e. not to use loops, referring to different numbers from the date, but write the whole formula). To be able to write it, we will need **all digits from the date** in individual variables to make all the necessary multiplications. By using the division and partition operations on the individual components of the date, using the **`getDay()`**, **`getMonth()`**, and **`getFullYear()`** methods, we can retrieve each digit. We have to pay attention to **`getMonth()`**, because the method returns a number between 0 (January) and 11 (December) and we have to add **+1**, to get the months between **`[1-12]`**. Another thing that we have to be careful with is the division by 10 (**`/ 10`**), which won't be an integer division, that's why after each operation we have to round specifically to the lowest integer number by using **`Math.floor(…)`**:
171171

172172
![](/assets/chapter-9-1-images/02.Magic-dates-05.png)
173173

@@ -262,11 +262,11 @@ Like every problem, we start the solution by **reading and processing the input
262262

263263
![](/assets/chapter-9-1-images/03.Five-special-letters-01.png)
264264

265-
We have several main points in the problem – **generating all combinations** with a length of 5 including the 5 letters, **removing repeating letters** and **calculating weight** for a simplified word. The answer will consist of every word whose weight is within the given range **`[firstNumber, secondNumber]`**.
265+
We have several main points in the problem – **generating all combinations** with a length of 5 including the 5 letters, **removing repeating letters**, and **calculating weight** for a simplified word. The answer will consist of every word whose weight is within the given range **`[firstNumber, secondNumber]`**.
266266

267267
#### Generating All Combinations
268268

269-
To generate **all combinations with a length of 1** using 5 symbols, we would use a **loop from 0..4**, as we want each number of the loop to match one character. To generate **any combinations of length 2** using 5 characters (i.e. "aa", "ab", "ac", …, "ba", …), we would create **two nested loops each running through the digits from 0 to 4**, as we will once again make sure that each digit matches a specific character. We will repeat this step 5 times, so we will finally have 5 nested loops with indexes **`i1`**, **`i2`**, **`i3`**, **`i4`** and **`i5`**:
269+
To generate **all combinations with a length of 1** using 5 symbols, we would use a **loop from 0..4**, as we want each number of the loop to match one character. To generate **any combinations of length 2** using 5 characters (i.e. "aa", "ab", "ac", …, "ba", …), we would create **two nested loops each running through the digits from 0 to 4**, as we will once again make sure that each digit matches a specific character. We will repeat this step 5 times, so we will finally have 5 nested loops with indexes **`i1`**, **`i2`**, **`i3`**, **`i4`**, and **`i5`**:
270270

271271
![](/assets/chapter-9-1-images/03.Five-special-letters-02.png)
272272

0 commit comments

Comments
 (0)