No older revisions available
No older revisions available
코드 ¶
~cpp
--TNPO
module TNPO
where
import List
threeNPlusOne numbers =
mergeList numbers (map maxCycleLength numbers) []
-- list merge ( merge [[1,2],[3,4]] [5,6] == [[1,2,5], [3,4,6]])
mergeList [] listB targetList = targetList
mergeList listA listB targetList =
mergeList (tail listA) (tail listB) (targetList ++ [(head listA ++ [head listB])] )
maxCycleLength fromto =
head (List.sortBy (flip compare) (gatherCycleLength (head fromto) (head (tail fromto)) []) )
gatherCycleLength num to gathered =
if num == to
then gathered
else gatherCycleLength (num+1) to ( gathered ++ [doCycle num 1])
doCycle 1 count = count
doCycle n count =
if mod n 2 == 1
then doCycle (3*n+1) (count+1)
else doCycle (div n 2) (count+1)
결과 ¶
~cpp
TNPO> threeNPlusOne [[1,10], [100,200], [201,210]]
[[1,10,20],[100,200,125],[201,210,89]]
TNPO>