Project Euler 002–TSQL

10 02 2011

Problem 2

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Solution

–By considering the terms in the Fibonacci sequence\

–whose values do not exceed four million, find the

–sum of the even valued terms.

 

–this is a trivial query to write

select SUM(number) from Number

where number < 4000000

and isfibonacci = 1

and number % 2 = 0

 

Discussion

Once again, I have a large database of numbers with properties associated with them.  That makes this solution fairly uninteresting. 

If you have questions, leave a comment or please find me on Twitter (@azzlsoft) or email (rich@azzlsoft.com).

Advertisements




Project Euler 001 – TSQL

4 02 2011

Problem 1

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

Solution

— I have a table called Number

— that has about 3 million

— integers starting at 1

— this problem is pretty

— simple from a query perspective

 

SELECT SUM(number) FROM Number

WHERE number < 1000

AND ((number % 3 = 0) OR (number % 5 = 0))

Discussion

When it’s possible to fill a table in a reasonable amount of time using brute force techniques, I will.  If the technique for filling the table is interesting I will demonstrate that.  I realize that in many cases this won’t be possible so I will have to use whatever “trick” is required to solve the problem before the sun burns out.

If you have questions, leave a comment or find me on Twitter (@azzlsoft) or email (rich@azzlsoft.com).