Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Raises a number to an Int power
- (^!) :: Num a => a -> Int -> a
- (^!) x n = x^n
- -- Takes the integer square root
- squareRoot :: Integer -> Integer
- squareRoot 0 = 0
- squareRoot 1 = 1
- squareRoot n =
- let twopows = iterate (^!2) 2
- (lowerRoot, lowerN) =
- last $ takeWhile ((n>=) . snd) $ zip (1:twopows) twopows
- newtonStep x = div (x + div n x) 2
- iters = iterate newtonStep (squareRoot (div n lowerN) * lowerRoot)
- isRoot r = r^!2 <= n && n < (r+1)^!2
- in head $ dropWhile (not . isRoot) iters
- -- Maps amount of paint and inner circle radius to a ring count
- circlesFromPaint :: Integer -> Integer -> Integer
- circlesFromPaint p r =
- let underroot = (8*p) + (4*(r*r) - 4*r) + 1
- introot = squareRoot underroot
- overfour = introot - 2*r + 1
- divbyfour = div overfour 4
- in divbyfour
- -- Generate strings of the format "Case #n" starting from 1
- caseStrs :: [String]
- caseStrs = map ((++) "Case #" . show) [1..]
- -- Run the circlesFromPaint equation on a line of input
- runLine :: String -> Integer
- runLine line =
- let info = fmap read (words line)
- r = info !! 0
- p = info !! 1
- in circlesFromPaint p r
- -- Run the program
- main = do
- cases <- getLine
- contents <- getContents
- mapM_ putStrLn (zipWith (\x y -> x ++ " " ++ show y) caseStrs (map runLine (lines contents)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement