Tuesday, June 19, 2007

Calculating Pi Using Only Pure SQL

Suppose table Numbers has numbers from 1 to 1000.
The problem: using pure SQL find the closest approximation to Pi number as a ratio of two integers. The integers should not exceed 999.
The solution is:

SELECT TOP 100 m, n, c FROM(

SELECT n1.Number m, n2.Number n, ABS(SIN(1.0*n1.Number/n2.Number)) c

FROM Numbers n1, Numbers n2

WHERE n1.Number BETWEEN 100 AND 999

AND n2.Number BETWEEN 100 AND 999) t

ORDER BY c


The answer is 355/113.

0 Comments:

Post a Comment

<< Home