Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how get list of all the possible combinations of 4 digit numbers whose individual digit sum is 13 and last digit is 5 in that number

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

nmz = [1165,    1345,   1435,   1615,   1705,   2245,   2425,   3055 ..... ]

errors are:

  1. no output as expected
  2. 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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading