Somma delle cifre 10

Small Basic Challenge – 05/2013 Small 1

Write a program that finds all the numbers less than 1000 that contain only digits that sum to 10, e.g. 55, 73, 137, but not 423.
How many are there?

Small Basic Challenge – 11/2012 Math 1

Find all the numbers less than 1000, where the sum of the digits is 15, for example 96 or 177.
How many are there?

1

Quali numeri…

SPECIALE = 10

for n in range(1, 1000):
    nc = n//100
    nd = (n//10)%10
    nu = n%10
    somma = nc+nd+nu
    if(somma == SPECIALE):
        print(n, end=' ')

2

Quanti numeri…

SPECIALE = 10
QUANTI = 0
for n in range(1, 1000):
    nc = n//100
    nd = (n//10)%10
    nu = n%10
    somma = nc+nd+nu
    if(somma == SPECIALE):
        QUANTI += 1
        print(n, end=' ')
print()
print("Quanti?", QUANTI)

3

Quanti numeri per ogni somma?

SMAX = 27
QUANTI = (SMAX+1)*[0]
for n in range(0, 1000):
    nc = n//100
    nd = (n//10)%10
    nu = n%10

    somma = nc+nd+nu
    QUANTI[somma] += 1
 
for i in range(SMAX+1):
    print("%2d %2d" %(i, QUANTI[i]))

Noti qualche simmetria nei risultati?

matplotlib

import matplotlib.pyplot as plt

SMAX = 27
N = range(0, SMAX+1)
QUANTI = (SMAX+1)*[0]
for n in range(0, 1000):
    nc = n//100
    nd = (n//10)%10
    nu = n%10
    somma = nc+nd+nu
    QUANTI[somma] += 1

plt.bar(N,QUANTI)
plt.show()