Some years ago I entered a programming contest and this was one of the problems: Binary Granny
Summary: Given a positive integer N find 2 positive integers such that $$ x + y = N $$Let X and Y be the number of bits set in the base 2 representation of x and y respectively(Hamming weight of x and y). Maximize X + Y.
For some reason I had a hard time at first in python(mostly because I was using an stupid algorithm that was too slow with large N) so I decided to try to solve it using Haskell. Eventually I managed to solve it somehow with very good performance with this Haskell code:
import IO
import Control.Monad
read_number_of_cases :: IO Integer
read_number_of_cases = do
s <- getLine
let number_of_cases = read s :: Integer
return number_of_cases
main :: IO ()
main = do
hSetBuffering stdin LineBuffering
number_of_cases <- read_number_of_cases
forM_ [1..number_of_cases] (\i -> do
line <- getLine
let number = read line :: Integer
a000120 0 = 0
a000120 n = a000120 n' + m where (n', m) = divMod n 2
middle = if even number
then truncate(toRational(number)/2)::Integer
else truncate(toRational(number)/2)::Integer
max_p1 = if (a000120 middle)>=(truncate(logBase 2 (fromIntegral middle))+1)
then middle
else truncate(2**fromIntegral(truncate(logBase 2 (fromIntegral middle)))-1)
max_p2 = if (a000120 number)>=(truncate(logBase 2 (fromIntegral number))+1)
then number
else truncate(2**fromIntegral(truncate(logBase 2 (fromIntegral number)))-1)
result = if max_p1 > max_p2
then a000120(max_p1) + a000120(number - max_p1)
else a000120(max_p2) + a000120(number - max_p2)
putStrLn ("Case #"++show i++": "++(show result)))`
(A000120 calculates the Hamming weight https://oeis.org/A000120)
What this code does is: $$ middle = \left\lfloor {\frac{N}{2}} \right\rfloor\\ max_{p1}=\begin{cases} middle,& \text{if } Hamming(middle)\geq \left\lfloor log_2(middle) \right\rfloor + 1\\ \left\lfloor 2^{ \left\lfloor (log_2(middle) )\right\rfloor}-1 \right\rfloor, & \text{otherwise} \end{cases}\\ max_{p2}=\begin{cases} middle,& \text{if } Hamming(N)\geq \left\lfloor log_2(N) \right\rfloor + 1\\ \left\lfloor 2^{ \left\lfloor (log_2(N) )\right\rfloor}-1 \right\rfloor, & \text{otherwise} \end{cases}\\ result=\begin{cases} Hamming(max_{p1})+Hamming(N-max_{p1}),& \text{if } max_{p1} \gt max_{p2} \\ Hamming(max_{p2})+Hamming(N-max_{p2}), & \text{otherwise} \end{cases} $$
That worked, but to be honest I have no idea why, or if it would work for every case. So the question is: Is there an easy way to prove that it is correct? Or incorrect if that is the case?