and the increase in max depth (which, per SICP, is an indicator of the space/memory required when analyzing a tree-recursive process like count-change) is so linear it’s a straight line
there’s a pattern in the increases of calls to for higher inputs. Like it starts going up by 1, then by 2, then by 4, and further increases after that.
possibly the pattern of increase would be more clear if i looked at all the computational steps instead of just focusing on cc. i wanted to try that first cuz i wanted to try something simpler. the increase in space seems linear at least
looked at some more data. count-change's number of steps definitely seems to increase massively as the input number increases, though I’m still not confident how to characterize that increase
So far my impression is that SICP seems kinda like a math textbook that uses computer science examples rather than the other way around. I know that’s not literally true but that’s how it seems from the perspective of my current skills. (Maybe it’s kinda like, if you don’t know English well then any random English book can feel like a test of vocab/grammar/comprehension)
I’m finding that even reading other people’s solutions to problems seems to have mathematical prerequisites I don’t meet and I’m getting stuck cuz of that. It’s one thing to not be able to come up with a solution on my own but if I’m struggling to understand answers then that seems like a big problem.
It’s possible that I should work on improving my math skills. Idk.
Exercise 1.16: Design a procedure that evolves an iterative exponentiation process that uses successive squaring and uses a logarithmic number of steps, as does fast-expt. (Hint: Using the observation that (b^{n/2})^2=(b^2)^{n/2}, keep, along with the exponent n and the base b, an additional state variable a, and define the state transformation in such a way that the product ab^n is unchanged from state to state. At the beginning of the process a is taken to be 1, and the answer is given by the value of a at the end of the process. In general, the technique of defining an invariant quantity that remains unchanged from state to state is a powerful way to think about the design of iterative algorithms.)
I was a bit confused as to what they were asking for here at first. I found something online which said “The goal is to use the algorithm of fast-exp […] but avoid using the recursive call as an argument to something else.”
Here is a partially pseudocode solution (cuz there is a step missing)
(define (new-expt b n)
(new-expt-iter b n a))
(define (new-expt-iter b n a)
(cond ((= n 0)
a)
((even? n)
(new-expt-iter b (/ n 2)(MAGIC GOES HERE?)))
(else
(new-expt-iter b (- n 1)(* b a)))))
Assuming I am vaguely on the right track, I think what I am missing is how to “define the state transformation in such a way that the product ab^n is unchanged from state to state.”
I wound up starting to read someone else’s answer and now see the problem.
I got stuck on wanting to try to solve the problem without changing the base value b. I think my issue was that I read this part of the hint…
Using the observation that (b^{n/2})^2=(b^2)^{n/2}, keep, along with the exponent n and the base b, an additional state variable a, and define the state transformation in such a way that the product ab^n is unchanged from state to state.
…and for some reason I decided that the base needed to remain constant. I didn’t apply this idea to any of the other values, though.
Anyways here’s the answer:
(define (new-expt b n)
(new-expt-iter b n 1))
(define (new-expt-iter b n a)
(cond ((= n 0)
a)
((even? n)
(new-expt-iter (* b b)(/ n 2) a))
(else
(new-expt-iter b (- n 1)(* b a)))))
I managed to avoid spending lots of time ineffectively spinning my wheels on this. I came back to it a couple of times over some period of time and tried thinking about it some before finally deciding to look at the answer. I’ve also been working on some stuff I think is related (like trying to make sure I’ve actually mastered the Simply Scheme material and working on my basic math skills)