Hello all
I am trying to solve this problem https://www.spoj.pl/problems/CUBERT7 but getting wrong answer . If some one accepted could post some tricky test cases , it would be great. I used Newton's method [ en.wikipedia.org/wiki/Cube_root7 ] to calculate the cube root .
import Data.List
import qualified Data.Char as DC
format :: [[String]] -> [Integer]
format [] = []
format (x:xs) = if null x then format xs else ( map read x ) ++ format xs
cbrtNewton :: Double -> Double -> Double
cbrtNewton x n = fst $ until ( \(a, b) -> abs( a - b ) <= 1e-12 ) (\(_ , x0) -> ( x0 , ( (n-1) * x0 + x / x0**(n-1) ) /n ) ) ( x ,x/n )
solve :: Integer -> String
solve x = a where
( y , z ) = break ( =='.') . show . cbrtNewton ( fromIntegral x ) $ 3
z' = take 11 ( z ++ "000000000000000000" )
ps = show $ mod ( foldl ( \acc x -> if DC.isDigit x then acc + DC.digitToInt x else acc ) 0 ( y ++ z' ) ) 10
a = ps ++ " " ++ y ++ z'
main = interact $ unlines.map solve . format .map words . tail . lines
created
last reply
- 2
replies
- 983
views
- 2
users
- 2
links