Fonction d' Akerman

Résolu
MOUNIRDD Messages postés 1 Date d'inscription   Statut Membre Dernière intervention   -  
_Ritchi_ Messages postés 21333 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

jisisv Messages postés 3645 Date d'inscription   Statut Modérateur Dernière intervention   934
 
#! /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
_Ritchi_ Messages postés 21333 Date d'inscription   Statut Contributeur Dernière intervention   6 104
 
Bonjour jisisv
Joli code !
Ritchi
0