Advertisement
robertocapannelli

sommaInteriCompresi

Jul 6th, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. #Scrivere una funzione ricorsiva che, assegnati due interi N1 ed N2,
  2. #restituisca la somma di tutti gli interi compresi tra N1 ed N2
  3.  
  4. def func(a,b, sum):
  5.     if a == b-1:
  6.         return sum
  7.     elif a == b:
  8.         return false
  9.     if a < b:
  10.         a += 1
  11.         sum = sum + a
  12.         return func(a,b, sum)
  13.  
  14. #Seconda versione
  15. def func(a,b):
  16.     if a == b-1 or a == b:
  17.         return false
  18.     if a < b:
  19.         a += 1      
  20.         return a + func(a,b)
  21.  
  22. #terza versione
  23. def func2(a,b):
  24.     if a == b:
  25.         return false
  26.     if a == b-1:
  27.         return 0
  28.     if a < b:
  29.         return a + 1 + fuc2(a+1,b)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement