I'm trying to create a program where I'm creating division problems for the user. I want them all to be 3 digits. For example, 100/20=5. So I just want the dividend to be a 3 digit number that is divisible. Is there a way to create a program that can do that?
CodePudding user response:
Well 3 digits integers are contained between 100 and 999, so you just want to check if they are divisible by some other number using the modulo operator and check if they are in the adequate range. Something like this:
divisor = 14
three_digits_divisible = [x for x in range(100, 999) if x % divisor == 0]
You can also automate it for a range of divisors by using a function that would return the list for a given divisor.
CodePudding user response:
If I understand correctly, you are trying to generate positive integers a,b,c in a way that a/b=c and a is a three digit number.
That means that a is less than 1000, but greater or equal to 100. And also a=b * c.
Choose a random one or two digit number. You can say that it is your b.
Now, above mentioned relations show, that b * c<1000 and b * c >= 100 (substitute a). When you have your b, you can calculate bounds for c.
- c < 1000/b
- c >= 100/b
Choose a random number c in these bounds, and your a is guaranteed to be a three digit number.
This will not generate every possible solution, as b was restricted to be 2 or 1-digit number.