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 row
CREATE TABLE row(n INT, x1 BIGINT, y1 BIGINT, x2 BIGINT, y2 BIGINT);




GO
DROP FUNCTION calcx1
GO
CREATE FUNCTION calcx1(@n INT) RETURNS BIGINT
AS
BEGIN

DECLARE @sum BIGINT
SET @sum = 0

WHILE @n > 0
BEGIN
SET @sum = @sum + (8 * @n)
SET @n = @n - 1
END
RETURN @sum + 1
END
GO


DECLARE @num INT
SET @num = 1
DECLARE @x1 BIGINT
DECLARE @y1 BIGINT

DECLARE @x2 BIGINT
DECLARE @y2 BIGINT

WHILE @num < 500
BEGIN
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 + 1
END

SELECT * FROM row
SELECT 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 problem34
CREATE TABLE problem34 (numValue BIGINT, facSum BIGINT, isspecial bit);


GO

ALTER FUNCTION dbo.Factorial ( @iNumber BIGINT )
RETURNS BIGINT
AS
BEGIN
DECLARE @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
END

RETURN (@i)
END
GO


GO
ALTER FUNCTION CalcFacSum(@input BIGINT) RETURNS BIGINT
AS
BEGIN
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 @ret
END
GO

DECLARE @counter BIGINT
DECLARE @tmpFacSum BIGINT
DECLARE @isSpecial BIT

SET @tmpFacSum = 0
SET @counter = 3

WHILE @counter < 3628800 -- 9999999
BEGIN
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 + 1
END

select * from problem34

SELECT SUM(facSum) from problem34
--40730

1-7 of 7