Good afternoon. Write a program that prints the number that divides into both numbers without a remainder.

Don't understand why the while loop works logical or operation, it should be and.

While 'the variable d will not be divisible by the first number' And 'variable d will not be divisible by the second number, THEN perform the cycle. Well then why is it "OR" is used? Although the meaning should be "And"?

Don't understand why the while loop works logical or operation, it should be and.

While 'the variable d will not be divisible by the first number' And 'variable d will not be divisible by the second number, THEN perform the cycle. Well then why is it "OR" is used? Although the meaning should be "And"?

```
a = int(input())
b = int(input())
d = 1
while d % a != 0 or d % b != 0:
d = d + 1
print(d)
```

asked June 14th 19 at 18:27

3 answers

answered on

Solution

Because the point of the algorithm is that d increases until, it will not be divisible by a and b. the while loop ends False. Thus it is necessary to construct the expression gives False when d is a divisor of a and b. That is, the inverse of the condition about which You think.

answered on

Solution

While 'the variable d will not be divisible by the first number' And 'variable d will not be divisible by the second number, THEN perform the cycle.

No, the statement is incorrect.

The while loop runs while the condition is true. In your case either true

`d % a != 0`

, is either true `d % b != 0`

Ruby has until that works as you describe

answered on June 14th 19 at 18:33

If this is not the laboratory work for the key-too much "head", then:

Oh and just for fun - implementing the same loop using iterators:

Please note that overkill is better to start with max(a, b) and not 1 (if natural)

PS LCM = Least Common Multiple

```
from math import gcd
lcm = a * b / gcd(a, b)
```

Oh and just for fun - implementing the same loop using iterators:

```
from itertools import count, dropwhile
lcm = next(dropwhile(lambda d: d % a != 0 or d % b != 0, count(max(a, b))))
```

Please note that overkill is better to start with max(a, b) and not 1 (if natural)

PS LCM = Least Common Multiple

Find more questions by tags Python