In Lecture 8, at exp3, why is Mr. Grimson using, if (b%2)*2 == b, to check if the number is odd or even. When running it with this if condition it never enters the if, but goes to the else instead. Would not be better to use: if (b%2)==0... def exp3(a,b): if b == 1: print 'first if' return a if (b%2)*2 == b: print 'second if' return exp3(a*a, b/2) else: print 'else' return a*exp3(a, b-1).
I think you might have misread/misunderstood. It's b/2*2==b. That is, division, not modulo.
Okay my bad, I've just watched the lecture you were referring to, and indeed that's what the prof wrote! But yup, it never enters the loop. I'm guessing it's because he would've gotten away with it in some other language he knows (maybe Lisp seeing as he was a Lisp hacker as he often mentions). Then again, it does talk about remainder, so it looks like he knows it's modulo... But in any case, I do think b%2 == 0 is a better (as in, more straightforward) test than (b/2)*2 == b.
I meant "never enter the if", not the loop.
Join our real-time social learning platform and learn together with your friends!