Answer the question
In order to leave comments, you need to log in
How to optimize the program?
A, B, C, D = map(int, input().split())
k = 0
for count in range(A, B):
if count % C != 0 and count % D != 0:
k += 1
print(k)
Answer the question
In order to leave comments, you need to log in
Is it possible to verbally formulate the task? Because there is little that can be optimized ...
Unless you try to calculate the number of suitable numbers without enumeration.
For example, the smallest number greater than or equal to A divisible by C would be
AC = (A // C + (1 if A % C else 0)) * C
Similarly, the largest number less than B divisible by C would be
BC = ((B-1) // C) * C
Then the number of numbers divisible by C in the interval A...B will be
XC = (BC - AC) // C + 1
By analogy, you can find XD for D, after which the desired number of numbers can be estimated as (B - A) - XC - XD.
But the difficulty is that some numbers can be divisible by both C and D. If so, then the specified estimate will be more correct, and it will be necessary to calculate XCD = (BCD - ACD) // (CD) + 1, where BCD and ACD is calculated similarly to the above AC and BC, only for the value of CD - the least common multiple of C and D. As a result, we get the answer (B - A) - XC - XD + XCD.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question