The function receives a number and returns the number of bits that would have been required to be “on” in order to represent the input number in binary base. For example, the number 5 is represented as 101 in binary and therefore requires two bits to be “on”. I need to know if the function I wrote is tail recursion. If not, how can I turn it to tail recursion? Thanks!
My function:
(define (numOfBitsOn number)
(define (numOfBitsOn-2 number acc)
(if (> number 0)
(if (odd? number)
(numOfBitsOn-2(/(- number 1) 2) (+ acc (modulo number 2)))
(numOfBitsOn-2 (/ number 2) acc))
acc))
(numOfBitsOn-2 number 0))