Guest User

Untitled

a guest
Dec 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. Sans fonction :
  2. #Functions
  3.  
  4.      
  5. #Main program
  6. n = int(input("Entrez un nombre : "))
  7. compt = 0
  8. while not n==1:
  9.     if n > 0:
  10.         if n % 2 == 0:
  11.             n = n//2
  12.             compt += 1
  13.             print(n)
  14.         else:
  15.             n = n*3+1
  16.             compt += 1
  17.             print(n)
  18.     else:
  19.         print("Erreur le nombre entré est incorrecte")
  20.  
  21. print("Il y a eu", compt, "occurences")
  22.  
  23.    
  24.            
  25.  
  26. Avec fonction :
  27.  
  28. #Functions
  29. def resol(n):
  30.     compt = 0
  31.     while not n==1:
  32.         if n > 0:
  33.             if n % 2 == 0:
  34.                 n = n//2
  35.                 compt += 1
  36.                 print(n)
  37.             else:
  38.                 n = n*3+1
  39.                 compt += 1
  40.                 print(n)
  41.         else:
  42.             print("Erreur le nombre entré est incorrecte")
  43.     return(compt)
  44.    
  45.    
  46. #Main program
  47. n = int(input("Entrez un nombre : "))
  48. compt = resol(n)
  49. print("Il y a eu", compt, "occurences")
Add Comment
Please, Sign In to add comment