Skip to content

Instantly share code, notes, and snippets.

@kidpixo
Created June 19, 2023 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kidpixo/127476d70bdedaefd1d9ac57c3cc03e2 to your computer and use it in GitHub Desktop.
Save kidpixo/127476d70bdedaefd1d9ac57c3cc03e2 to your computer and use it in GitHub Desktop.
sumDigits naturals product
# first 10 naturals
nat = np.arange(1,10)
# multiply them
nat_df = pd.DataFrame(
columns=nat,
index=nat,
data=np.matmul(nat[:,np.newaxis],nat[:,np.newaxis].T))
def sumDigits(no):
return 0 if no == 0 else int(no % 10) + sumDigits(int(no / 10))
# sum all the digits
print(nat_df.applymap(sumDigits))
# result :
# 1 2 3 4 5 6 7 8 9
# 1 1 2 3 4 5 6 7 8 9
# 2 2 4 6 8 1 3 5 7 9
# 3 3 6 9 3 6 9 3 6 9
# 4 4 8 3 7 2 6 10 5 9
# 5 5 1 6 2 7 3 8 4 9
# 6 6 3 9 6 3 9 6 12 9
# 7 7 5 3 10 8 6 13 11 9
# 8 8 7 6 5 4 12 11 10 9
# 9 9 9 9 9 9 9 9 9 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment