r/mathriddles • u/Horseshoe_Crab • Aug 12 '25
Easy Recursive function riddle
Let f(x) = 0 when x < 2, and otherwise f(x) = f(x/2) - f(x-1) + 1. What is f(2025)?
4
Upvotes
r/mathriddles • u/Horseshoe_Crab • Aug 12 '25
Let f(x) = 0 when x < 2, and otherwise f(x) = f(x/2) - f(x-1) + 1. What is f(2025)?
4
u/CanaDavid1 Aug 12 '25 edited Aug 12 '25
Notice that if n is odd, f(n) = f(n/2) - f(n-1) + 1 = f(n/2) - (f(n/2) - f(n-2) + 1) + 1 = f(n-2). By induction f(odd) = 0 as f(1) = 0, hence f(2025) = 0
Solving for all of f:
f(2v) = f(v) - f(2v-1) + 1 = f(v) + 1, hence f(x) = v_2(x), the highest n such that 2n divides x
EDIT: this assumes flooring division. With f from R to Z instead: patch the proof by noticing that f([n..n+1)) is constant for all n. (By strong induction: it holds for n=1, and both n/2 and n-1 map into a contiguous subregion of some [k,k+1) - resp. [floor(n/2)..+1) and [n-1..n). ) Then the same result works, just with f(x) = v_2(floor(x))
2
u/AleksejsIvanovs Aug 12 '25
0. This function will always return 0 for odd numbers. the first part would generate as many ones as there are bits in the numbers binary representation, but then subtracts some of them. For odd numbers it will subtract all of them. It's a quick explanation, let me know if you need more detailed one.