aboutsummaryrefslogtreecommitdiff
path: root/haskell/wip/145-how_many_reversible_numbers_are_there_below_onebillion.hs
blob: 77ef6fae71318092a653411ab783f7f824d888dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
------
-- How many reversible numbers are there below one-billion?
-- Problem 145
--
-- Some positive integers n have the property that the sum [ n + reverse(n) ] consists
-- entirely of odd (decimal) digits. For instance, 36 + 63 = 99 and 409 + 904 = 1313.
-- We will call such numbers reversible; so 36, 63, 409, and 904 are reversible.
-- Leading zeroes are not allowed in either n or reverse(n).
-- There are 120 reversible numbers below one-thousand.
-- How many reversible numbers are there below one-billion (10^9)?
------


main = do
    let notZeroEndedNums = filter (\x -> x `mod` 2 /= 0) [1..10 ^ 7]
        revs = filter reversible [1,3..10 ^ 6] --notZeroEndedNums
    print (length revs)
    -- print $ length $ filter id $ map reversible notZeroEndedNums
    -- print $ [r2 - r1 | (r1, r2) <- zip revs (tail revs)]

-- countReversible :: Int -> Int
-- countReversible  = 0


reversible :: Int -> Bool
reversible x = allOddDigits (x + rx)
    where rx = read . reverse . show $ x

allOddDigits :: Int -> Bool
allOddDigits x
    | x `mod` 2 == 0 = False
    | x `div` 10 == 0 = True
    | otherwise = allOddDigits (x `div` 10)