Fonction d' Akerman

Résolu
MOUNIRDD Messages postés 1 Statut Membre -  
_Ritchi_ Messages postés 21130 Date d'inscription   Statut Contributeur Dernière intervention   -
Bonjour,
si'l vous plais, j ais besoin d 'un code itératif de la fonction d' Akermane
merci

2 réponses

  1. jisisv Messages postés 3678 Statut Modérateur 936
     
    #! /usr/bin/python
    
    def ackermann(m,n, verbose = False):
        """computes the value of an ackermann function for the input integers m and n
           the ackermann function being
           A(m,n)=n+1               if m=0
                 =A(m-1,1)          if m>0 and n=1
                 =A(m-1,A(m,n-1)    if m>0 and n>0"""
        if m == 0 :
            if verbose :
                print (n+1)
            return n+1
        elif  n == 0 :
            if verbose :
                print ("ackermann(%d , %d)" % ( m - 1, 1) )                                        
            return ackermann(m-1, 1)
        else :
            if verbose :
                print ("ackerman(%d, ackermann(%d, %d)" %  (m - 1, m, n - 1) ) 
            return ackermann(m - 1,ackermann(m, n - 1)) 
    
    print(ackermann(3, 5))


    Attention, cette fonction est récursive et rapidement exponentielle !

    1
    1. _Ritchi_ Messages postés 21130 Date d'inscription   Statut Contributeur Dernière intervention   6 135
       
      Bonjour jisisv
      Joli code !
      Ritchi
      0