用T-SQL语言 编程输出100以内的素数
时间: 2024-01-06 21:03:10 浏览: 80
SELECT n
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY number) AS n, number
FROM (
SELECT DISTINCT number
FROM (
SELECT number, SUM(CASE WHEN divisor = 1 OR divisor = number THEN 1 ELSE 0 END) AS cnt
FROM (
SELECT number, divisor = CASE WHEN number % x = 0 THEN x ELSE 0 END
FROM (
SELECT number = ROW_NUMBER() OVER (ORDER BY a.number, b.number)
FROM master..spt_values a, master..spt_values b
) AS numbers
CROSS JOIN (
SELECT x = ROW_NUMBER() OVER (ORDER BY (SELECT 1))
FROM master..spt_values a, master..spt_values b
) AS divisors
WHERE number <= 100
) AS factors
GROUP BY number
) AS counts
WHERE cnt = 2
) AS primes
) AS numbered_primes
WHERE n <= 25;
阅读全文