Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update magic-combination.md #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Problem: Magic Numbers
# Problem: Magic Combination

Write a program that enters a single integer **magic** number and produces all possible **6-digit numbers** for which the product of their digits is equal to the magical number.
Write a program that enters a **magic** integer number and generates all possible **6-digit numbers** for which the product of their digits is equal to the magical number.

Example: "Magic number" → 2
- 111112 → 1 \* 1 \* 1 \* 1 \* 1 \* 2 = 2
Expand All @@ -10,11 +10,11 @@ Example: "Magic number" → 2
- 121111 → 1 \* 2 \* 1 \* 1 \* 1 \* 1 = 2
- 211111 → 2 \* 1 \* 1 \* 1 \* 1 \* 1 = 2

## Input Data
## Input

The input is read from the console and consists of **one integer** within the range [**1 … 600 000**].

## Output Data
## Output

Print on the console **all magic numbers**, separated by **space**.

Expand All @@ -24,44 +24,44 @@ Print on the console **all magic numbers**, separated by **space**.
| --- | --- | --- | --- | --- | --- |
|2|111112 111121 111211 112111 121111 211111|8|111118 111124 111142 111181 111214 111222 111241 111412 111421 111811 112114 112122 112141 112212 112221 112411 114112 114121 114211 118111 121114 121122 121141 121212 121221 121411 122112 122121 122211 124111 141112 141121 141211 142111 181111 211114 211122 211141 211212 211221 211411 212112 212121 212211 214111 221112 221121 221211 222111 241111 411112 411121 411211 412111 421111 811111|531441|999999|

## Solution using a "Fr" Loop
## Solution using a For Loop

**The solution** follows **the same** concept (again we need to generate all combinations for the n element). Following these steps, try to solve the problem yourself.
**The solution** follows **the same** principle (again we have to generate all combinations for a number of elements). Try to solve the problem yourself by following these steps.

- Declare and initialize **variable** of **`int`** type and read the **input** from the console.
- Nest **six `for` loops** one into another, for every single digit of the searched 6-digit numbers.
- In the last loop, using **`if`** construction, check if the **product** of the six digits is **equal** to the **magic number**.
- Declare and initialize **variable** of type **`int`** and read the **input** from the console.
- Nest **six `for` loops** into each other, one for each digit of the required 6-digit numbers.
- In the last loop, use an **`if`** construction to check if the **product** of the six digits is **equal** to the **magic number**.

![](/assets/chapter-7-exam-preparation-images/02.magic-numbers-1.png)

## Solution using a "While" Loop

In the previous chapter we reviewed other loop constructions. Let's look at the sample solution of the same problem using the **`while`** loop.

First, we need to store the **input magical number** in a suitable variable. Then we will initialize 6 variables – one for each of the six digits of **the searched numbers**.
First, we need to store the **input magical number** in a suitable variable. Then we initialize 6 variables – one for each of the six digits of **the searched numbers**.

![](/assets/chapter-7-exam-preparation-images/02.magic-numbers-2.png)

### Writing a While Loop

Then we will start writing **`while`** loops.
Then we start writing **`while`** loops.

- We will initialize **first digit**: **`d1 = 0`**.
- We will set a **condition for each** loop: the digit will be less than or equal to 9.
- In the **beginning** of each loop we set a value of the **next** digit, in this case: **`d2 = 0`**. In the nested **`for`** loops we initialize the variables in the inner loops at each increment of the outer ones. We want to do the same here.
- At **the end** of each loop we will increase the digit by one: **`d++`**.
- In the **innermost** loop we will make **the check** and if necessary, we will print on the console.
- We initialize the **first digit**: **`d1 = 0`**.
- We set a **condition for each** loop: the digit has to be less than or equal to 9.
- At the **start** of each loop we set the value of the **next** digit, in this case: **`d2 = 0`**. In the nested **`for`** loops we initialize the variables in the inner loops at each increment of the outer ones. We want to do the same here.
- At **the end** of each loop we increase the digit by one: **`d++`**.
- In the **innermost** loop we do **the check** and, if necessary, print on the console.

![](/assets/chapter-7-exam-preparation-images/02.magic-numbers-3.png)

### Infinite While Loop

Let's remove the **`if`** check from the innermost loop. Now, let's initialize each variable outside of the loops and delete the rows **`dx = 0`**. After we run the program, we only get 10 results. Why? What if you use **`do-while`**? In this case this loop does not look appropriate, does it?Think why. Of course, you can solve the problem using an **infinite loop**.
Let's remove the **`if`** check from the innermost loop. Now, let's initialize each variable outside of the loops and delete the rows **`dx = 0`**. After we run the program, we get only 10 results. Why? What if we use **`do-while`**? In this case this loop does not look appropriate, does it? Think about why. Of course, you can solve the problem using an **infinite loop**.

![](/assets/chapter-7-exam-preparation-images/02.magic-numbers-4.png)

As we can see, we can solve a problem using different types of loops. Of course, each task has its most appropriate choice. In order to practice each type of loops – try to solve each of the following problems with all the learned loops.
As we can see, we can solve a problem using different types of loops. Of course, each task has its most appropriate choice. In order to practice each type of loop, try to solve each of the following problems by using all the loops we've learned so far.

## Testing in the Judge System
## Test in the Judge System

Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/515#1](https://judge.softuni.org/Contests/Practice/Index/515#1).