Skip to content

Instantly share code, notes, and snippets.

@arthurbacci
Created January 23, 2023 21:53
Show Gist options
  • Save arthurbacci/32e9e77cb0558d20d91ec31e0ef8ec2d to your computer and use it in GitHub Desktop.
Save arthurbacci/32e9e77cb0558d20d91ec31e0ef8ec2d to your computer and use it in GitHub Desktop.
import Control.Monad.State.Lazy
import Text.Printf
aprox :: Double -> State Double Double
aprox t = state (\y ->
let add = (t - x) * (1.0 - y) / (10.0 - x)
x = 10.0 ** y
in (t - x, y + add))
combine_x_times :: (Monad m) => Int -> m a -> m a
combine_x_times 1 m = m
combine_x_times n m = (combine_x_times (n - 1) m) >> m
get_mantissa :: Int -> Int
get_mantissa n = round $
10000 * (execState (combine_x_times 50 $ aprox asFrac) 0.00)
where
asFrac = fromIntegral n / 100.0
printColumn :: Int -> Int -> IO ()
printColumn _ 0 = return ()
printColumn n i = do
if odd n then printf "%04d" (get_mantissa n)
else printf "\x1b[7m%04d\x1b[m" (get_mantissa n)
printColumn (n + 1) (i - 1)
printRows :: Int -> IO ()
printRows 100 = return ()
printRows r = do
printf "%02d " r
printColumn (r * 10) 10
putStrLn ""
printRows $ r + 1
main :: IO ()
main = printRows 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment