forked from Tecmax/JavaAndroidPractise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp014.hs
More file actions
24 lines (20 loc) · 641 Bytes
/
Copy pathp014.hs
File metadata and controls
24 lines (20 loc) · 641 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
{-
- Solution to Project Euler problem 14
- Copyright (c) Project Nayuki. All rights reserved.
-
- https://www.nayuki.io/page/project-euler-solutions
- https://github.com/nayuki/Project-Euler-solutions
-}
main = putStrLn (show ans)
ans = indexMax (map collatz [0..10^6])
collatz :: Integer -> Int
collatz 0 = 0
collatz 1 = 1
collatz n = (collatz (if mod n 2 == 0 then (div n 2) else (n * 3 + 1))) + 1
indexMax :: [Int] -> Int
indexMax = indexMax' 0 (-1) 0
indexMax' _ j _ [] = j -- Current index, max index, max value
indexMax' i j v (x:xs) =
if (j == -1 || x > v)
then (indexMax' (i+1) i x xs)
else (indexMax' (i+1) j v xs)