Search code examples
recursionpseudocode

Find the result of a function


There is a code of an unknown function:

function Magic(number)
    r = number mod 2
    print r
    if number > 1
        Magic(number / 2)

(written in pseudo-code)

The question is: what integer number should be passed in order to receive the following answer

0 1 1 0 0 1

The main problem is that I can't figure out how mod is working in pseudocode. Should 5,5 mod 3 = 2.5 or 2


Solution

  • First of all, here is executable python code for this problem.

    def Magic(number):
    r = number % 2
    print r
    if number > 1:
        Magic(number / 2)
    
    Magic(15)       
    

    However, the pattern in this problem is that the function is returning the REVERSE binary number for the given input number. So, in this instance, the easiest solution would be to take 0 1 1 0 0 1, reverse it to 100110, and calculate the value of that binary number, which is 32 + 4 + 2 = 38. Using this methodology, you can calculate the required number or the expected output for any given input.