How can I generate a list of 3-digit numbers for which the sum of their digits equal 17?
For example assuming number XYZ
, we would have X+Y+Z=17
This is how far I got:
numbers = list(range(1, 1000))
How can I generate a list of 3-digit numbers for which the sum of their digits equal 17?
For example assuming number XYZ
, we would have X+Y+Z=17
This is how far I got:
numbers = list(range(1, 1000))
If you need more comment, and/or if what I've done is not what you asked (somehow), tell me. :)
It appears that you want the sum of the digits to be 17 and not the sum of the numbers as the "but I want the first number + the second number + the third number = 17 ?" implies.
So, take a look at this:
You can first generate a list of numbers that matches your condition like,
So to make this a random list, you can use
random
package.So
test_list
wil be a completely random list with all the possible three digit numbers in it. So this could be the fastest way to generate a random list matching your condition. (shuffle)Hope this helps!
Disclaimer: This method will be random and you can have duplicates. If you want an exhaustive list of unique numbers, it will not work, and you might prefer Ev. Kounis' solution.
Here we go, pick 2 digits which sum is between 8 and 17, then calculate the required 3rd one to add up to 17.
The sum of the first 2 digits needs to be:
8 or more because you can have 9 max for the last digit
17 or less because you can't have negative numbers for the last digit
This way:
outputs something like this: