You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: chapter-09-problems-for-champions.md
+10-10Lines changed: 10 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -13,17 +13,17 @@ We have two sequences:
13
13
-**a sequence of Tribonacci** (by analogy with the Fibonacci sequence), where each number is **the sum of the previous three** (with given three numbers)
14
14
- 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.
15
15
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**.
17
17
18
18
### Problem
19
19
20
20
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.
21
21
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.
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.
27
27
28
28
### Input Data
29
29
@@ -59,7 +59,7 @@ On the single line of the output, we must print **the smallest number that occur
59
59
60
60
The problem seems quite complicated, so we will break it into simpler sub-problems.
61
61
62
-
#### Processing the Input
62
+
#### Processing The Input
63
63
64
64
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:
65
65
@@ -75,7 +75,7 @@ For the Tribonacci sequence, we will always **collect the previous three values*
75
75
76
76
#### Generating Numerical Spiral
77
77
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.
79
79
80
80
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:
81
81
@@ -155,19 +155,19 @@ For looping through the dates, we will take advantage of the functionality that
155
155
156
156
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.
157
157
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:
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:
**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.
167
167
168
168
#### Calculating Date Weight
169
169
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(…)`**:
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]`**.
266
266
267
267
#### Generating All Combinations
268
268
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`**:
0 commit comments