summaryrefslogtreecommitdiff
path: root/testsuite/tests/dph/sumnats/Main.hs
blob: 9e18e335a9955a04c16d9cf2f60443ec7900ca04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

import SumNatsVect (sumNats)

-- Solution for 1st Euler problem
-- Add all the natural numbers below 1000 that are multiples of 3 or 5.

solutionLists maxN
 = let	sumOnetoN n = n * (n+1) `div` 2
	sumStep s n = s * sumOnetoN (n `div` s)
   in	sumStep 3 (maxN - 1) + sumStep 5 (maxN - 1) - sumStep 15 (maxN - 1)

solutionLists2 maxN
  = sum	[ x 	| x <- [0.. maxN - 1]
		, (x `mod` 3 == 0) || (x `mod` 5 == 0) ]

main 
 = do	let n	= 1000
	print $ solutionLists  n
	print $ solutionLists2 n
	print $ sumNats n