recursive method in Java

logistic_guy

Senior Member
Joined
Apr 17, 2024
Messages
1,504
Write a recursive method that returns the number of 1\displaystyle 1’s in the binary representation of N\displaystyle N. Use the fact that this is equal to the number of 1\displaystyle 1’s in the representation of N/2\displaystyle N/2, plus 1\displaystyle 1, if N\displaystyle N is odd.
 
Write a recursive method that returns the number of 1\displaystyle 1’s in the binary representation of N\displaystyle N. Use the fact that this is equal to the number of 1\displaystyle 1’s in the representation of N/2\displaystyle N/2, plus 1\displaystyle 1, if N\displaystyle N is odd.
Please show us what you have tried and exactly where you are stuck.

Please follow the rules of posting in this forum, as enunciated at:

READ BEFORE POSTING

Please share your work/thoughts about this problem
 
In this game, I will have to import the Scanner\displaystyle \text{Scanner} class to allow the user to enter a number.

Say, the user entered 26=11010\displaystyle 26 = 11010, my program should output: There are 3\displaystyle 3 ones (1’s\displaystyle 1\text{'s}).
 
In this game, I will have to import the Scanner\displaystyle \text{Scanner} class to allow the user to enter a number.

Say, the user entered 26=11010\displaystyle 26 = 11010, my program should output: There are 3\displaystyle 3 ones (1’s\displaystyle 1\text{'s}).
The process of counting the ones will be done recursively through a function. In other words, a function will call itself multiple times and it will not stop until it reaches a base case.

Our base case will be:

If the number is 0\displaystyle 0 or 1\displaystyle 1, stop.
 
When the user enters a number larger than 1\displaystyle 1, I want that number eventually to get smaller and smaller until it reaches the base case. To handle such a process, I will divide that number by 2\displaystyle 2 continuously.
 
Let us make a simulation for a user who entered the number 26=11010\displaystyle 26 = 11010.

counter=0\displaystyle \text{counter} = 0

Step 1\displaystyle \text{Step} \ \bold{1}
if 26=0\displaystyle 26 = 0 or 26=1\displaystyle 26 = 1, stop \displaystyle \rightarrow false, then continue.

Step 2\displaystyle \text{Step} \ \bold{2}
if 26\displaystyle 26 is even, divide by 2\displaystyle 2 \displaystyle \rightarrow true, 262=13\displaystyle \frac{26}{2} = 13

Step 3\displaystyle \text{Step} \ \bold{3}
if 13=0\displaystyle 13 = 0 or 13=1\displaystyle 13 = 1, stop \displaystyle \rightarrow false, then continue.

Step 4\displaystyle \text{Step} \ \bold{4}
if 13\displaystyle 13 is odd, divide by 2\displaystyle 2 and add one to the counter \displaystyle \rightarrow true, 132=6\displaystyle \left\lfloor\frac{13}{2}\right\rfloor = 6 (counter=1)\displaystyle (\text{counter} = 1)

Step 5\displaystyle \text{Step} \ \bold{5}
if 6=0\displaystyle 6 = 0 or 6=1\displaystyle 6 = 1, stop \displaystyle \rightarrow false, then continue.

Step 6\displaystyle \text{Step} \ \bold{6}
if 6\displaystyle 6 is even, divide by 2\displaystyle 2 \displaystyle \rightarrow true, 62=3\displaystyle \frac{6}{2} = 3

Step 7\displaystyle \text{Step} \ \bold{7}
if 3=0\displaystyle 3 = 0 or 3=1\displaystyle 3 = 1, stop \displaystyle \rightarrow false, then continue.

Step 8\displaystyle \text{Step} \ \bold{8}
if 3\displaystyle 3 is odd, divide by 2\displaystyle 2 and add one to the counter \displaystyle \rightarrow true, 32=1\displaystyle \left\lfloor\frac{3}{2}\right\rfloor = 1 (counter=2)\displaystyle (\text{counter} = 2)

Step 9\displaystyle \text{Step} \ \bold{9}
if 1=0\displaystyle 1 = 0 or 1=1\displaystyle 1 = 1, stop \displaystyle \rightarrow true, if the number is 1\displaystyle 1, add one to the counter (counter=3)\displaystyle (\text{counter} = 3)

And that's how we got the number of ones =3\displaystyle = 3. In fact, that counter\displaystyle \text{counter} does not exist. The function just calls itself and keeps track of the ones when the number is odd. I put the counter\displaystyle \text{counter} just to give your brain an imaginary visualization of the process.
Some of you may be wondering, how all this magic happens. In simple words, I have used the concept of the Hamming Weight.

💪😛
 
We have the whole idea in the pocket, we just need to implement it in Java.

In the next days, I will brainstorm the components that I need in my program, then I will slowly show my code.

Be in touch, and don't be greedy!

💪👻
 
Top