The first part is easy. It can be done in one Insert...Select if you just use the Row_Number() function.
IF OBJECT_ID(N'tempdb..#Numbers') IS NOT NULL
DROP TABLE #Numbers;
CREATE TABLE #Numbers (Number INT PRIMARY KEY);
INSERT INTO #Numbers (Number)
SELECT TOP 60000 ROW_NUMBER() OVER (ORDER BY T1.OBJECT_ID)
FROM sys.all_columns AS T1
CROSS JOIN sys.all_columns AS T2;
Keep adding references to the system view till you get at least 60k rows. (Could be one instance, could be more. Will depend on the database you're doing this in.)
The third part, will depend almost completely on the CPU.
On a low-end machine, I got this to run in 13 seconds:
SELECT Number
FROM #Numbers AS N1
WHERE Number > 1
AND NOT EXISTS
(SELECT 1
FROM #Numbers AS N2
WHERE N1.Number%N2.Number = 0
AND N2.Number > 1
AND N2.Number <= N1.Number/2);
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon