Skip to content
loading...

re: Daily Challenge #152 - Strongest Number in an Interval VIEW POST

FULL DISCUSSION
 

Haskell

strongest :: (Int, Int) -> (Int, Int) -> (Int, Int)
strongest (strongestIndex, strongestValue) (index, value)
    | value > strongestValue = (index, value)
    | value == strongestValue && index < strongestIndex = (index, value)
    | otherwise = (strongestIndex, strongestValue)


strength :: Int -> Int
strength 0 = 0
strength number 
    | odd number = 0
    | otherwise = 1 + strength (div number 2)


strongestBetween :: Int -> Int -> Int
strongestBetween lowerbound upperbound =
    fst $ foldl1 strongest $ zip interval $ map strength interval

    where
        interval :: [Int]
        interval =
            [lowerbound..upperbound]


main :: IO ()
main = do
    print $ strongestBetween 1 2        -- 2
    print $ strongestBetween 5 10       -- 8
    print $ strongestBetween 48 56      -- 48
    print $ strongestBetween 129 193    -- 192

Try it.

code of conduct - report abuse