How get all the possible combinations of 4 digit numbers whose individual digit sum is 13 and last digit is 5 in that number?
I am trying it in python as:
nmz = []
for i in range(1000,10000):
if ((sum(int(j) for j in str(i))==13) & (i % 10)):
nmz = nmz.append(i)
return(nmz)
else:
continue
I am getting errors and not able to get expected output:
nmz = [1165, 1345, 1435, 1615, 1705, 2245, 2425, 3055 ..... ]
errors are:
- no output as expected
- SyntaxError: ‘return’ outside function
>Solution :
There’s no point in starting the range at 1000 because it doesn’t fulfil the significant criterion that the number should end with 5. So, start at 1005 and increment by 10. Therefore:
mylist = []
for i in range(1_005, 10_000, 10):
if sum(int(d) for d in str(i)) == 13:
mylist.append(i)
print(mylist)
Output:
[1075, 1165, 1255, 1345, 1435, 1525, 1615, 1705, 2065, 2155, 2245, 2335, 2425, 2515, 2605, 3055, 3145, 3235, 3325, 3415, 3505, 4045, 4135, 4225, 4315, 4405, 5035, 5125, 5215, 5305, 6025, 6115, 6205, 7015, 7105, 8005]