3077 - Suma Prefixe

From Bitnami MediaWiki
Revision as of 11:14, 1 April 2023 by Pop Giulia (talk | contribs)

def calculeaza_suma_prefixe(numar, prefix=0):

   if numar == 0:
       return prefix
   
   suma = 0
 
   for cifra in range(1, numar % 10 + 1):
       suma += calculeaza_suma_prefixe(numar // 10, prefix + cifra)
   
   return suma


n = int(input("Introduceți un număr natural nenul: "))


suma_prefixe = calculeaza_suma_prefixe(n) print("Suma tuturor prefixelor distincte pentru numărul", n, "este", suma_prefixe)