Difference between revisions of "Talk:Euler problems/1 to 10"

From HaskellWiki
Jump to navigation Jump to search
 
m
(8 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
Should the "solution" of problem 10 not contain a way how the primes are constructed? In itself its no solution.
 
Should the "solution" of problem 10 not contain a way how the primes are constructed? In itself its no solution.
 
[[User: hk|hk]]
 
[[User: hk|hk]]
  +
*It's in problem 3 earlier on the page. The <tt>primes</tt> function is needed for the solution of many problems. [[User:Quale|Quale]] 16:55, 26 February 2008 (UTC)
  +
:It's barely a solution anyway, IMHO. They've changed the problems to all primes below *two* million now, and it takes about 3 minutes to run for me (Athlon64 3200+). [[User:Exscape|Exscape]] 18:29, 27 May 2008 (UTC)
  +
  +
  +
The solution for problem 7 has an off-by-one error, no? Doesn't (!!) start indexing at 0?
  +
  +
  +
The solution for problem 8 is not generic and requires check the last digits.
  +
Below shows the problem and suggested correction for groupsOf.
  +
<haskell>
  +
groupsOf _ [] = []
  +
groupsOf n xs =
  +
take n xs : groupsOf n ( tail xs )
  +
  +
groupsOf' n xs
  +
| length group < n = []
  +
| otherwise = group : groupsOf' n ( tail xs )
  +
where group = take n xs
  +
  +
digits = [1,1,1,1,1,0,9,9,9,9]
  +
  +
max5 = maximum $ map product $ groupsOf 5 digits -- 6561 (wrong)
  +
max5' = maximum $ map product $ groupsOf' 5 digits -- 1 (correct)
  +
</haskell>
  +
  +
:Why not use <hask>tails</hask> and pattern matching like below?
  +
:<haskell>maximum [product xs | xs@[_,_,_,_,_] <- map (take 5) (tails digits)]</haskell>
  +
:-- [[User:RayNbow|RayNbow]] 22:08, 24 January 2010 (UTC)
  +
  +
::Or just take and tails: <haskell>maximum [product . take 5 $ d | d <- take 995 $ tails digits]</haskell>
  +
::--[[User:Gorlum0|Gorlum0]] 17:00, 25 February 2011 (UTC)

Revision as of 17:00, 25 February 2011

Should the "solution" of problem 10 not contain a way how the primes are constructed? In itself its no solution. hk

  • It's in problem 3 earlier on the page. The primes function is needed for the solution of many problems. Quale 16:55, 26 February 2008 (UTC)
It's barely a solution anyway, IMHO. They've changed the problems to all primes below *two* million now, and it takes about 3 minutes to run for me (Athlon64 3200+). Exscape 18:29, 27 May 2008 (UTC)


The solution for problem 7 has an off-by-one error, no? Doesn't (!!) start indexing at 0?


The solution for problem 8 is not generic and requires check the last digits. Below shows the problem and suggested correction for groupsOf.

groupsOf _ [] = []
groupsOf n xs = 
    take n xs : groupsOf n ( tail xs )

groupsOf' n xs 
  | length group < n = []
  | otherwise        = group : groupsOf' n ( tail xs )
    where group = take n xs 

digits = [1,1,1,1,1,0,9,9,9,9]

max5 = maximum $ map product $ groupsOf 5 digits   -- 6561 (wrong)
max5' = maximum $ map product $ groupsOf' 5 digits --  1 (correct)
Why not use tails and pattern matching like below?
maximum [product xs | xs@[_,_,_,_,_] <- map (take 5) (tails digits)]
-- RayNbow 22:08, 24 January 2010 (UTC)
Or just take and tails:
maximum [product . take 5 $ d | d <- take 995 $ tails digits]
--Gorlum0 17:00, 25 February 2011 (UTC)