A brute force Haskell solution, that is much faster than the flawr one. module Main (main) where import Data.Bits import Data.Word import Data.List import qualified Data.IntSet as S pairs' :: Int -> Word64 -> Int pairs' n s = fromIntegral $ foldl' (.|.) 0 $ map mk [1..n] where mk d = let mask = 1 `shiftL` d - 1 pc = popCount $! xor (s `shiftR` (n - d)) (s .&. mask) in if pc <= 1 then mask + 1 else 0 f :: Int -> Int f n = S.size $ S.fromList $ map (pairs' n) [0 .. (2 ^ (n - 1))] main :: IO () main = mapM_ (print . f) [1..]