U E D R , A S I H C RSS

3N+1/임인택

코드

~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>

날적이

  • 시간 측정은 어떻게 하는가? -_-a
----
3N+1Problem
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:16
Processing time 0.0112 sec