Euler Project, Question 1

From PostgreSQL wiki

Jump to: navigation, search

Snippets

Editing Euler Project

Works with PostgreSQL

8.4

Written in

SQL

Depends on

Nothing

One way to achieve the solution to question 1.

WITH RECURSIVE t1(a, b) AS (
        VALUES(0,0)
    UNION ALL
        SELECT CASE CAST(b AS BOOLEAN)
                      WHEN b % 3 = 0 THEN b
                      WHEN b % 5 = 0 THEN b
                END,
                b + 1
          FROM t1
         WHERE b < 1000
)
SELECT sum(a) FROM t1
Personal tools