Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Questão 1.16 #7

Open
luizf-s opened this issue Feb 16, 2021 · 1 comment
Open

Questão 1.16 #7

luizf-s opened this issue Feb 16, 2021 · 1 comment

Comments

@luizf-s
Copy link

luizf-s commented Feb 16, 2021

A solução proposta pela @anabastos está com o square no local errado:

(defn square [x] (* x x))

(defn expt [b n a]
   (cond (= n 0) a
         (even? n) (square (expt b (/ n 2) a)) ;; square no local errado
         :else (expt b (- n 1) (* b a))))

Na segunda cláusula, deveria ser:

(defn square [x] (* x x))

(defn expt [b n a]
  (cond (= n 0)   a
        (even? n) (expt (square b) (/ n 2) a) ;; linha modificada
        :else     (expt b (- n 1) (* b a))))

Pode-se verificar mostrando as respostas:

(println (expt 3 6 1))
(println (expt 2 10 1))
(println (expt 5 2 1))
(println (expt 5 3 1))

Também verificando como é computado o estado para uma operação como expt(3, 6, 1)

b=3    n=6  a=1
b=9    n=3  a=1
b=9    n=2  a=9
b=81   n=1  a=9
b=81   n=0  a=729
@anabastos
Copy link
Member

@luizfelipesalg
Muito obrigada pela correção!!!! Depois vou dar uma reolhada no que já foi resolvido.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants