Sunday, August 14, 2011

Problem 14

prob14.hs Problem 14
Filename: prob14.hs
--The following iterative sequence is defined for the set of positive integers:
--
--n  n/2 (n is even)
--n  3n + 1 (n is odd)
--
--Using the rule above and starting with 13, we generate the following sequence:
--
--13  40  20  10  5  16  8  4  2  1
--It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.
--
--Which starting number, under one million, produces the longest chain?
--
--NOTE: Once the chain starts the terms are allowed to go above one million.

module Prob14
    where
      import List 

      iterativeSequence 1 = 1 : []
      iterativeSequence n = n : iterativeSequence (isGenerator n)

      isGenerator n
          | even n = (n `div` 2)
          | odd n = (3*n + 1)

      workingList = [1..number]
      --modified_list (n:ns) (x:xs) = (n:ns) \\ (x:xs)

      resultList n (x:xs) = (x:xs) \\ (reverse(iterativeSequence n))
      

      --workingList \\ iterativeSequence 3

      number = 8
      result = map length (map iterativeSequence [1..number])
          
--
--      factor n (p:ps) 
--          | p*p > n        = [n]
--          | n `mod` p == 0 = p : factor (n `div` p) (p:ps)
--          | otherwise      = factor n ps
--
--      primes = 2 : filter ((==1) . length . primeFactors) [3,5..]
--      primeFactors n = factor n primes
--      triangleNumbers = scanl1 (+) [1..]
--      answer = head (filter ((> 500) . numDivisors) triangleNumbers)
--      numDivisors n = product (map (+1) (map (length) (group (primeFactors n))))
--



syntax highlighted by Code2HTML, v. 0.9.1

No comments:

Post a Comment