Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

3N+1/임인택: Difference between revisions

From ZeroWiki
imported>Unknown
No edit summary
 
(Repair batch-0001 pages from live compare)
 
Line 9: Line 9:
   
   
  threeNPlusOne numbers =
  threeNPlusOne numbers =
  mergeList numbers (map maxCycleLength numbers) []
  mergeList numbers (map maxCycleLength numbers) []
   
   
  -- list merge ( merge [[1,2],[3,4]] [5,6] == [[1,2,5], [3,4,6]])
  -- list merge ( merge [[1,2],[3,4]] [5,6] == [[1,2,5], [3,4,6]])
  mergeList [] listB targetList = targetList
  mergeList [] listB targetList = targetList
  mergeList listA listB targetList =
  mergeList listA listB targetList =
  mergeList (tail listA) (tail listB) (targetList ++ [(head listA ++ [head listB])] )
  mergeList (tail listA) (tail listB) (targetList ++ [(head listA ++ [head listB])] )
   
   
  maxCycleLength fromto =
  maxCycleLength fromto =
  head (List.sortBy (flip compare) (gatherCycleLength (head fromto) (head (tail fromto)) []) )
  head (List.sortBy (flip compare) (gatherCycleLength (head fromto) (head (tail fromto)) []) )
   
   
  gatherCycleLength num to gathered =
  gatherCycleLength num to gathered =
  if num == to
  if num == to
  then gathered
  then gathered
  else gatherCycleLength (num+1) to ( gathered ++ [doCycle num 1])
  else gatherCycleLength (num+1) to ( gathered ++ [doCycle num 1])
   
   
  doCycle 1 count = count
  doCycle 1 count = count
Line 30: Line 30:
  else doCycle (div n 2) (count+1)
  else doCycle (div n 2) (count+1)
== 결과 ==
== 결과 ==
  TNPO> threeNPlusOne [[1,10], [100,200], [201,210]]
  TNPO> threeNPlusOne [[1,10], [100,200], [201,210]]
  [[1,10,20],[100,200,125],[201,210,89]]
  [[1,10,20],[100,200,125],[201,210,89]]
  TNPO>
  TNPO>
== 날적이 ==
== 날적이 ==

Latest revision as of 23:55, 26 March 2026

코드

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

결과

TNPO> threeNPlusOne [[1,10], [100,200], [201,210]]
[[1,10,20],[100,200,125],[201,210,89]]
TNPO>

날적이

  • 시간 측정은 어떻게 하는가? -_-a

3N+1Problem