- lets begin with looking at this number 123
- recall that the operation we used to grab the last number is num % 10 . adn really the definition of the modules operator says, some number num = 10 * d + r . and that remainder value r is what gets returned to us when we do n % 10. for example the number 17 is equivalent to 10 * 1 + 7 and that is why 17 % 10 will give us 7.
- ok if that makes sense, lets use the mod operator to get the indiivudal digits of this number num which is equal to 123. recall that to get the last digit we do num % 10, followed by recursively doing n / 10, to gain access to the rest of the number.
- now, lets think about how we would build this number back up given the digits 1 , 2, and 3, and we want to build up the number 123.
- so you can see that the way we build a number back up is always by doing 10 * some number d + a remainder, r. and you can notice in this pattern that the value of d is always equivalent to the number that Is already inside the number UNLESS there is just a single digit, which would simply be a 0 value for d. so in 123 here you can see ….
- awesome, so using this knowledge of how to build a number back up, let's see how this can be applied to our problem, Evens. so we are given some number and we want to return a number that only contains the even digits that were present in our original number. so we have evens(8342116) and we in this case return a number that only contains the even digits, which is 8426. so you can imagine it might be difficult to just take a number, adn try to remove odd digits out of it in random places. Like this is not a string or an array that we can just replace values at certain indexes
- so now we have to be creative and use our mechanism of building a number back up! we want to say hey, I have seen this even number 8, and this even number 4, and 2, adn 6 and I want to build these up together in the correct ordering of course.
- so when we have 8 and we want to build it up and we start with this single digit 8, how did we begin building up the number! well we know from the 123 example, when we wanted to start by building up the number 1, we use the formula 10 * 0 + 8, because we knew so far there were no numbers built up yet, so you can imagine that this would be the base case. when we want to build up a number that does not yet exists yet i.e that has no digits in it yet, we know that we want to multiply 10 * 0.
- now lets look at the number 4 here. we know that to add 4 to 8 and to append that digit to this number containing 8, we wanted to do 10 * (8) + 4
- ….. demonstrate for the rest of the numbers
- ok so we know our base case is the 0 case, and lets take a look at what the recursive case would be! so I will draw a diagram for how we want to build up the number 8426
- show the digit formula table
- now since we know we just have the tools to get the last digit, we will kind of go backwards, use recursion to start at the end of our original number and append that number and make sure to leave room in front of it for the numbers we wil check later.
- so lets dive into our tree diagram! we know that we start with a call to even (8432116) and we need to know an important fact which is that even numbers always end up with a last digit of 0, 2,4,6,or 8!
- so if we have even(8432116) we want to have a recursive case that checks if the last digit is even so we say if(n % 2 == 0 ) then we want to grab that last digit and use it to build up our number! we know that we can build up the number by doing 10 * d + (n % 10) . but what would this d value be? we know that by looking at our digit table with the formula, we can see the value for d that we need to build this number 6 to the end is going to be whatever we compute the value before 6 to be. meaning that d needs to be a recursive call to the rest of the digits to see what they will return!
- and lets highlight our other recursive case which is that if the number we are on is odd! well, we know we only want to append and work with even numbers, so if the number is odd we are just going to skip it! and we can simulate skipping the number by saying hey, forget this digit, adn give me the rest of the digit, and we know we did that computation by doing n / 10!
- adn lastly, we know our base case was that if the digit we have a single digit, then we return 0! and since we are recursively passing in n / 10 for both the odd case and the even case, there will come a point where we have some single digit such as 8 , when we said the d value Is 0 because no numbers came before it. so a call to Evens(8 / 10 ) would be equivalent to Evens(0) by integer java floor division and we would hit our base case and start returning back upwards until we are done!
- and of course if we have a negative value, we dont want that disturbing us so we are simply going to print out a negative value and recursively call with the negation of that which is just the positive of that number and we enter the method as normal!