Fonction d' Akerman

Résolu/Fermé
MOUNIRDD Messages postés 1 Date d'inscription samedi 26 novembre 2016 Statut Membre Dernière intervention 28 novembre 2016 - 28 nov. 2016 à 14:31
_Ritchi_ Messages postés 21209 Date d'inscription samedi 17 mars 2007 Statut Contributeur Dernière intervention 7 avril 2024 - 6 déc. 2016 à 19:18
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 dimanche 18 mars 2001 Statut Modérateur Dernière intervention 15 janvier 2017 934
29 nov. 2016 à 21:53
#! /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 21209 Date d'inscription samedi 17 mars 2007 Statut Contributeur Dernière intervention 7 avril 2024 6 058
6 déc. 2016 à 19:18
Bonjour jisisv
Joli code !
Ritchi
0