Project Euler‎ > ‎

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


Comments