I performed the task of Project Euler, in which you want to find the smallest number that is evenly divisible by the numbers from 1 to 20 inclusive. I realized that you can use a loop with range, but never figured out how, decided otherwise.

Later in the review found such script, but have not figured out how it works. Please explain.

As i increases, if the second condition(in the inner loop) does not work? First executes the outer loop or the inner?

Later in the review found such script, but have not figured out how it works. Please explain.

As i increases, if the second condition(in the inner loop) does not work? First executes the outer loop or the inner?

```
i = 1
for k in (range(1, 21)):
if i % k > 0:
for j in range(1, 21):
if (i*j) % k == 0:
i *= j
break
print (i)
```

asked March 19th 20 at 08:29

3 answers

answered on

Solution

First flash the first cycle to the condition when it is running - when the second cycle to the condition as soon as it runs - i changes and goes to the first cycle. i didn't change until the script does not proceed to the second cycle, the second cycle it moves only when the condition in the first.

For such things are sometimes useful to use print at each iteration of each cycle, to see where what happens if it is difficult to understand it immediately.

For such things are sometimes useful to use print at each iteration of each cycle, to see where what happens if it is difficult to understand it immediately.

answered on March 19th 20 at 08:33

I realized that you can use a loop with range, but never figured out how, decided otherwise.

The number, which evenly divided by some other number called the Least Common Multiple (LCM). This is taught in school

So how is it at school?

```
from math import gcd
a = 2
for b in range(3, 21):
a = a * b // gcd(a, b)
print(a)
```

answered on March 19th 20 at 08:35

Two

6

12

60

420

840

2520

27720

360360

720720

12252240

232792560

6

12

60

420

840

2520

27720

360360

720720

12252240

232792560

i j k

2 2 2

6 3 3

12 2 4

60 5 5

420 7 7

840 2 8

2520 9 3

27720 11 11

360360 13 13

720720 2 16

12252240 17 17

232792560 19 19 commented on March 19th 20 at 08:38

2 2 2

6 3 3

12 2 4

60 5 5

420 7 7

840 2 8

2520 9 3

27720 11 11

360360 13 13

720720 2 16

12252240 17 17

232792560 19 19 commented on March 19th 20 at 08:38

Find more questions by tags Python

And here another:

On the interval [1, 20] there are eight Prime numbers: 2, 3, 5, 7, 11, 13, 17, 19.

The maximum power of two in the interval [1, 20] - 2^4, three - 3^2, five and more - 5^1 and on.

`print(16 * 9 * 5 * 7 * 11 * 13 * 17 * 19)`

- arnoldo_Ferry89 commented on March 19th 20 at 08:39This fact (about the power of a Prime) as something obvious - it's simple, from where they are to knock to take more than the maximum, but to a lesser does not.

Simply because of all the possible solutions you brought here is not the worst but pretty dumb, and the answer, marked with the solution here, the code does not explain.

You can do without the jokes, but when the issue shows that the author does not understand neither in Python nor in mathematics, it is causing some irritation - where to start the explanation? - arnoldo_Ferry89 commented on March 19th 20 at 08:45