#potencia: num int -> num #x elevado a y, y >= 0 #ej: potencia(2,3)->8, potencia(1.5,2)->2.25 def potencia(x,y): assert type(x)==int or type(x)==float assert type(y)==int and y>=0 assert not(x==0 and y==0) if y==0: return 1 else: return x*potencia(x,y-1) assert potencia(2,8)==256 assert potencia(1.5,2)==2.25 #potencia: num int -> num #x^y como x^y/2 * x^y/2 #ej: potencia(2,-3)->0.125, potencia(1.5,2)->2.25 def potencia(x,y): assert type(x)==int or type(x)==float assert type(y)==int and y >= 0 assert not(x==0 and y==0) if y==0: return 1 else: p=potencia(x,y//2) if y%2 ==0: return p*p else: return x*p*p assert potencia(2,8)==256 assert potencia(2,9)==512 #potencia: num int -> num #x elevado a y #ej: potencia(2,-3)->0.125, potencia(1.5,2)->2.25 def potencia(x,y): assert type(x)==int or type(x)==float assert type(y)==int and y>=0 assert not(x==0 and y==0) def _potencia(x,y): if y==0: return 1 p=_potencia(x,y//2) if y%2==0: return p*p else: return x*p*p return _potencia(x,y) assert potencia(2,8)==256 assert potencia(2,9)==512