[Tutor] use of assignment expression
Peter Otten
__peter__ at web.de
Mon Aug 17 04:15:06 EDT 2020
Manprit Singh wrote:
> Dear Sir,
> My question was about to write a program to find the smallest n digit
> number, divisible by number x.
> There are now two solutions to this problem:
>
> n = 5
> x = 83
> no = 10**(n-1)
> while True:
> if (no / x).is_integer():
This check may raise an OverflowError or, worse, give the wrong result:
>>> (10**300/3).is_integer()
True
> print(no)
> break
> no = no + 1
> ------------------------------------------------
> n = 5
> x = 83
> no = 10**(n-1)
> if no % x == 0:
> print(no)
> else:
> print((no + x) - ((no + x) % x))
>
>
>
> Will give the answer 10043. What I have concluded from all the previous
> mails, that assignment expression can be avoided in this case .The first
> solution is not in more mathematical terms, but the second solution is
> more mathematical. Need your comments further.
If you understand the second solution, go with it. While the first solution
works if you avoid the use of float it may be slow for big x.
More information about the Tutor
mailing list