### Project Euler

#### F# - Sieve Of Eratosthenes. Useful when solving some Euler Project challanges.

posted Aug 31, 2010, 11:29 AM by Peter Henell

 let sieveOfErat max = let rec sieve l r = let r = List.Cons (List.hd l, r) let l = List.filter(fun x -> (x % r.Head <> 0I && x <> r.Head)) l match r.Head with | n when n > bigint.FromInt32(int (Math.Ceiling(Math.Sqrt (big_int.ToDouble max)))) -> List.append l r | n -> match (List.length l) with | 0 -> r | n -> sieve l r sieve [2I..max] [] let sieveOfEratSeq max = let rec sieve l r = let r = Seq.cons (Seq.hd l) r let l = Seq.filter(fun x -> (x % (Seq.hd r) <> 0I && x <> (Seq.hd r))) l match (Seq.hd r) with | n when n > bigint.FromInt32(int (Math.Ceiling(Math.Sqrt (big_int.ToDouble max)))) -> Seq.append l r | n -> match (Seq.length l) with | 0 -> r | n -> sieve l r sieve {2I..max} Seq.empty

#### Problem 2 in F#

posted Aug 27, 2009, 6:35 AM by Peter Henell

 let problem2 = let rec fib n = match n with | 0 -> 0 | 1 | 2 -> 1 | n -> (fib (n - 1)) + (fib (n - 2)) {1..35} |> Seq.map (fun x -> fib x) |> Seq.filter ( fun z -> (z < 1000000 && z % 2 = 0)) |> Seq.sumByInt(fun y -> y)

#### Problem 1 in F#

posted Aug 27, 2009, 1:17 AM by Peter Henell

 let summering = {1..999} |> Seq.filter(fun x -> ((x % 3 = 0 ) || (x % 5 = 0))) |> Seq.sumByInt (fun x -> x)

#### Problem in 5 F#

posted Aug 27, 2009, 1:17 AM by Peter Henell

 let rec divisible n = let divbyall20 k = let lista = Seq.map (fun x -> k % x ) {1..20} if (Seq.sumByInt (fun x -> x) lista) = 0 then true else false if divbyall20 n then n else divisible (n + 20)printf "Problem 5: %d\n" (divisible 20)

#### Problem 1 - Erlanglösning

posted Aug 27, 2009, 1:16 AM by Peter Henell

 lists:foldl(fun(X, S) -> X + S end, 0, [X || X <- lists:seq(1, 1000), (X rem 5 =:= 0) and (X rem 3 =:= 0)]).

#### Project Euler problem 28

posted Aug 27, 2009, 1:15 AM by Peter Henell

 DROP TABLE rowCREATE TABLE row(n INT, x1 BIGINT, y1 BIGINT, x2 BIGINT, y2 BIGINT);GODROP FUNCTION calcx1GOCREATE FUNCTION calcx1(@n INT) RETURNS BIGINTASBEGINDECLARE @sum BIGINTSET @sum = 0WHILE @n > 0BEGIN SET @sum = @sum + (8 * @n) SET @n = @n - 1END RETURN @sum + 1ENDGODECLARE @num INTSET @num = 1DECLARE @x1 BIGINTDECLARE @y1 BIGINTDECLARE @x2 BIGINTDECLARE @y2 BIGINTWHILE @num < 500BEGIN SET @x1 = dbo.calcX1(@num) SET @y1 = (SELECT @x1 - ((8 * @num) / 2) AS y1) SET @x2 = (SELECT @x1 - (@num * 2)) SET @y2 = (SELECT @y1 - (@num * 2)) INSERT INTO row(n, x1, y1, x2, y2) VALUES(@num, @x1, @y1, @x2, @y2) SET @num = @num + 1ENDSELECT * FROM rowSELECT SUM(x1) + SUM(y1) + SUM(x2) + SUM(y2) + 1 AS answer FROM row

#### Project euler problem 34

posted Aug 27, 2009, 1:12 AM by Peter Henell   [ updated Aug 27, 2009, 1:15 AM ]

 drop table problem34CREATE TABLE problem34 (numValue BIGINT, facSum BIGINT, isspecial bit);GOALTER FUNCTION dbo.Factorial ( @iNumber BIGINT )RETURNS BIGINTASBEGINDECLARE @i BIGINT SET @i = CASE @iNumber WHEN 1 THEN 1 WHEN 2 THEN 2 WHEN 3 THEN 6 WHEN 4 THEN 24 WHEN 5 THEN 120 WHEN 6 THEN 720 WHEN 7 THEN 5040 WHEN 8 THEN 40320 WHEN 9 THEN 362880 WHEN 0 THEN 1 ELSE 1 / 0 ENDRETURN (@i)ENDGOGOALTER FUNCTION CalcFacSum(@input BIGINT) RETURNS BIGINTASBEGIN DECLARE @ret BIGINT DECLARE @str VARCHAR(20) SET @ret = 0 SET @str = CONVERT(VARCHAR(20), @input) WHILE LEN(@str) > 0 BEGIN SET @ret = @ret + dbo.Factorial(CONVERT(BIGINT, LEFT(@str, 1))) SET @str = RIGHT(@str, LEN(@str) - 1) END return @retENDGODECLARE @counter BIGINTDECLARE @tmpFacSum BIGINTDECLARE @isSpecial BITSET @tmpFacSum = 0SET @counter = 3WHILE @counter < 3628800 -- 9999999BEGIN SET @tmpFacSum = dbo.CalcFacSum(@counter) SET @isSpecial = CASE @tmpFacSum WHEN @counter THEN 1 ELSE 0 END IF @isSpecial = 1 INSERT INTO problem34 VALUES(@counter, @tmpFacSum, @isSpecial) SET @counter = @counter + 1ENDselect * from problem34SELECT SUM(facSum) from problem34--40730

1-7 of 7