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-07-complex-loops.md
+20-20Lines changed: 20 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ In the current chapter, we will also take a look at the **`break`** operator and
11
11
12
12
## Loops with a Step
13
13
14
-
In the **"Loops (Repetitions)"** chapter we learned how the **`for`** loop works and we already know when and to what purpose to use it. In the present chapter we will **take a look** at a particular and a very important **part of this structure** - its **step** or as it is also known **step**.
14
+
In the **"Loops (Repetitions)"** chapter we learned how the **`for`** loop works and we already know when and to what purpose to use it. In the present chapter we will **take a look** at a particular and a very important **part of this structure** - its **step**.
15
15
16
16
### Loop with a Step – Explanation
17
17
@@ -23,7 +23,7 @@ Most often we have a **size of `1`** and in this case, instead of writing **`i +
23
23
24
24
Here is a series of sample problems, the solution of which will help us better understand the use of a **step** in a **`for`** loop.
25
25
26
-
### Problem: Numbers from 1 to N with a step of 3
26
+
### Problem: Numbers 1...N with Step 3
27
27
28
28
Write a program that prints the numbers **from 1 to n** with a **step of 3**. For example, **if n = 100**, then the output would be: **1, 4, 7, 10, …, 94, 97, 100**.
29
29
@@ -39,7 +39,7 @@ We can solve the problem using the following sequence of actions (algorithm):
39
39
40
40
You can test your solution at the following link: [https://judge.softuni.org/Contests/Practice/Index/937#0](https://judge.softuni.org/Contests/Practice/Index/937#0).
41
41
42
-
### Problem: Numbers from N to 1 in Reverse
42
+
### Problem: Numbers N...1
43
43
44
44
Write a program that prints the numbers **from n to 1 in reverse** (step -1). For example, **if n = 100**, then the output will be: **100, 99, 98, …, 3, 2, 1**.
45
45
@@ -57,7 +57,7 @@ We can solve the problem in the following manner:
57
57
58
58
You can test your solution at the following link: [https://judge.softuni.org/Contests/Practice/Index/937#1](https://judge.softuni.org/Contests/Practice/Index/937#1).
59
59
60
-
### Problem: Numbers from 1 to 2^n with a For Loop
60
+
### Problem: Powers of Two
61
61
62
62
In the following example, we will use the standard size 1 step.
63
63
@@ -86,9 +86,9 @@ Here is how we can solve the problem:
86
86
87
87
You can test your solution at the following link: [https://judge.softuni.org/Contests/Practice/Index/937#3](https://judge.softuni.org/Contests/Practice/Index/937#3).
88
88
89
-
## While loop
89
+
## While Loop
90
90
91
-
The next type of loops that we will familiarize, which are called **`while` loops**. The special thing about them is that they repeat a command block **while a condition is met**. Their structure is a bit different than that of the **`for`** loops, however, they boast a simpler syntax.
91
+
The next type of loops that we will familiarize are called **`while` loops**. The special thing about them is that they repeat a command block **while a condition is met**. Their structure is a bit different than that of the **`for`** loops, however, they boast a simpler syntax.
92
92
93
93
94
94
### While Loop – Explanation
@@ -100,7 +100,7 @@ In programming, the **`while` loop** is used when we want to **repeat** the exec
100
100
Here is a series of sample problems, the solutions of which will help us better understand the use of the **`while`** loop.
101
101
102
102
103
-
### Problem: Sequence of Numbers 2k+1
103
+
### Problem: Sequence 2k+1
104
104
105
105
Write a program that prints **all numbers ≤ n** in the series: **1, 3, 7, 15, 31, …**, assuming that each number is generated according to the following formula nextNumber = **previousNumber \* 2 + 1**.
106
106
@@ -119,7 +119,7 @@ Here is a sample implementation of this idea:
119
119
120
120
You can test your solution here: [https://judge.softuni.org/Contests/Practice/Index/937#4](https://judge.softuni.org/Contests/Practice/Index/937#1).
121
121
122
-
### Problem: Number in Range [1 … 100]
122
+
### Problem: Number in Range [1...100]
123
123
124
124
Enter an integer in the range [**1 … 100**]. If the entered number is **invalid**, enter **another**. In this case, an invalid number would be any number that is **outside** the given range.
125
125
@@ -195,7 +195,7 @@ We will solve the problem by implementing **Euclid's algorithm**:
195
195
196
196
You can test your solution here: [https://judge.softuni.org/Contests/Practice/Index/937#6](https://judge.softuni.org/Contests/Practice/Index/937#6).
197
197
198
-
## Do-while loop
198
+
## Do-While Loop
199
199
200
200
The next type of loop we will study is the **`do-while`** loop. By structure it resembles the **`while`**, but with a significant difference. The **`do-while`** will execute its body **at least once**. Why is this? In the **`do-while`** loop structure, the **condition** is always checked **after** the body, which ensures that the **first loop iteration** will **execute** the code and the check for **the end of the loop** will be applied to each **subsequent** iteration of the **`do-while`**.
201
201
@@ -204,7 +204,7 @@ The next type of loop we will study is the **`do-while`** loop. By structure it
204
204
Now we should proceed with the usual set of practical problems, that will help us better understand the **`do-while`** loop.
205
205
206
206
207
-
### Problem: Calculating Factorial
207
+
### Problem: Factorial
208
208
209
209
For natural **n** number, calculate **n! = 1 \* 2 \* 3 \* … \* n**. For example, if **n = 5**, then the result would be: **5!** = 1 \* 2 \* 3 \* 4 \* 5 = **120**.
210
210
@@ -224,7 +224,7 @@ Here is how we can calculate factorial in more detail:
224
224
225
225
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/937#7](https://judge.softuni.org/Contests/Practice/Index/937#7).
226
226
227
-
### Problem: Summing the Digits of a Number
227
+
### Problem: Sum Digits
228
228
229
229
Sum up the digits of the integer **positive** number **n**. For example if **n = 5634**, then the output would be: 5 + 6 + 3 + 4 = **18**.
230
230
@@ -274,7 +274,7 @@ We already know that the infinite loop executes a certain code infinitely, but w
274
274
<td>The operator <b><code>break</code></b> stops a loop's execution at the point it is called and the execution continues from the first line after the end of the loop. This means that the current iteration of the loop will not be completed accordingly and the rest of the code in the body of the loop will not be executed.</td>
275
275
</tr></table>
276
276
277
-
### Problem: Prime Number Checking
277
+
### Problem: Check Prime
278
278
279
279
The next problem we are going to solve is to **check whether a given number is prime**, but before that, we should remember what are prime numbers.
280
280
@@ -299,14 +299,14 @@ Here are some examples of composite numbers:
299
299
<td>We can optimize the algorithm by instead of checking until <code><strong>n-1</strong></code>, checking divisors only until <code><strong>√n</strong></code>. Think of the reasons why this is so.</td>
300
300
</tr></table>
301
301
302
-
### Problem: Check for a Prime Number. Break Operator
302
+
### Problem: Enter Even Number
303
303
304
304
You are tasked to write a function that takes a single input **n** integer and checks if it is prime. This can be implemented by checking if **n** is divisible by any numbers in the range between 2 and √n.
305
305
306
306
The steps of the **"prime checking algorithm"** are given below in bigger detail:
307
307
308
308
* We declare the variable **`n`**, to which we assign the integer passed to our function.
309
-
* We create a **`prime`** boolean with and an initial value of **`true`**. We assume that a number is prime until proven otherwise.
309
+
* We create a **`prime`** boolean with, and an initial value of **`true`**. We assume that a number is prime until proven otherwise.
310
310
* We create a **`for`** loop, with the initial value set to 2, for a condition the **current value `<= √n`**. The step is set to 1.
311
311
* In the **body of the loop**, we check if **`n`**, divided by the **current value** has a remainder. If there is **no reminder** from the division, then we change **`prime`** to **`false`** and exit the loop through the **`break`** operator.
312
312
* Depending on the value of **`prime`** we print whether the input number is prime (**`true`**) or composite (**`false`**).
@@ -315,13 +315,13 @@ Here is a sample implementation of the prime checking algorithm, described above
What remains is to add a **condition that checks if the input number is greater than 1**, because, by definition numbers such as 0, 1, -1 and -2 are not prime.
318
+
What remains is to add a **condition that checks if the input number is greater than 1**, because, by definition numbers such as 0, 1, -1, and -2 are not prime.
319
319
320
320
#### Testing in The Judge System
321
321
322
322
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/937#9](https://judge.softuni.org/Contests/Practice/Index/937#9).
323
323
324
-
### Problem: The break Operator in an Infinite Loop
324
+
### Problem: Break Sum
325
325
326
326
Write a function, which checks whether a given number **n** is even and if so - print it on the console. An even number can be divided by 2 without a remainder. If the number is invalid, we will print that the current number is not even and the user will need to input a new number.
327
327
@@ -380,7 +380,7 @@ Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/937#
380
380
381
381
In this chapter, we got familiar with a few new types of loops that can perform repetitions with more complex programming logic. Let's solve a few practical problems using these new constructs.
382
382
383
-
### Problem: Fibonacci Numbers
383
+
### Problem: Fibonacci
384
384
385
385
Fibonacci's numbers in mathematics form a sequence that looks like this: **1, 1, 2, 3, 5, 8, 13, 21, 34, …**.
386
386
@@ -425,7 +425,7 @@ Example implementation:
425
425
426
426
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/937#12](https://judge.softuni.org/Contests/Practice/Index/937#12).
427
427
428
-
### Problem: Numbers Pyramid
428
+
### Problem: Number Pyramid
429
429
430
430
Print the **numbers 1 … n in a pyramid** as per the below example. On the first row, we print one number, at the second we print two, at the third, we print three, and so on, until the numbers are over. On the last line, we print as many numbers as we get until we get to **n**.
431
431
@@ -437,7 +437,7 @@ Print the **numbers 1 … n in a pyramid** as per the below example. On the firs
437
437
438
438
#### Hints and Guidelines
439
439
440
-
We can solve the problem with **two nested loops** (by rows and columns) with printing in them and leaving when the last number is reached. Here is the idea, written in more detail:
440
+
We can solve the problem with **two nested loops** (by rows and columns) by printing in them and leaving when the last number is reached. Here is the idea, written in more detail:
441
441
442
442
* We declare a variable **`n`**, to which we assign the integer value passed to our function.
443
443
* We declare a variable **`num`** with an initial value of **1**. It will hold the count of printed numbers. At each iteration, we will **increment** it by **1** and will add it to the current row.
@@ -460,7 +460,7 @@ Here is an example implementation:
460
460
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/937#13](https://judge.softuni.org/Contests/Practice/Index/937#13).
461
461
462
462
463
-
### Problem: Numbers Table
463
+
### Problem: Number Table
464
464
465
465
Print the numbers 1 … n in a table as per the examples below:
0 commit comments