-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path515.hs
More file actions
40 lines (32 loc) · 974 Bytes
/
Copy path515.hs
File metadata and controls
40 lines (32 loc) · 974 Bytes
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
34
35
36
37
38
39
40
{-# OPTIONS_GHC -O3 #-}
{-# LANGUAGE BangPatterns #-}
import Control.Applicative ((<$>))
import Data.List
import Data.Maybe
import Data.Array
f :: Integer -> Integer -> Integer
f n 1 = n
f n !k =
let !a = f n (k-1)
in if odd a then
3 * a + 1
else
a `div` 2
-- solve :: Integer -> Int
-- solve n = (+1) . fromJust . findIndex (== 1) . map (f n) $ [1..]
cycleLength :: Integer -> Integer
-- Tailrec
cycleLength = loop 1
where loop !accum 1 = accum
loop !accum n | odd n = loop (accum + 1) (3 * n + 1)
| otherwise = loop (accum + 1) (n `div` 2)
memoized :: Integer -> Integer
memoized = solve'
where solve' n | n <= top = (memoArray ! n)
| otherwise = cycleLength n
top = 1000
!memoArray = listArray (1,top) [cycleLength x | x <- [1..top]]
main = do
cases <- lines <$> getContents
mapM_ (putStrLn . show . memoized . read) cases
return ()