Write a query to print all prime numbers less than or equal to 1000. Print your result on a single line, and use the ampersand (&) character as your separator (instead of a space).
For example, the output for all prime numbers <=10 would be:
2&3&5&7
Use below T-SQL query to achieve the above scenario
SELECT DISTINCT
REPLACE(STUFF(REPLACE((SELECT '~' + CAST(ST1.Number AS VARCHAR) AS 'data()'
FROM (SELECT DISTINCT Number
FROM MASTER..SPT_VALUES AS a
WHERE
A.Number > 1 AND A.Number <= 1000 AND
NOT EXISTS
(
SELECT 1 FROM MASTER..SPT_VALUES AS b WHERE b.Number > 1
AND b.Number < a.Number
AND a.Number % b.Number = 0)) ST1
ORDER BY ST1.Number
FOR XML PATH('')),'~','&'), 1, 1, ''),' ','') as [PRIME_NUMBERS]
FROM MASTER..SPT_VALUES ST2 WHERE ST2.Number > 1 AND ST2.Number <= 1000
Please see the below output of the above query:
2&3&5&7&11&13&17&19&23&29&31&37&41&43&47&53&59&61&67&71&73&79&83&89&97&101&103&107&109&113&127&131&137&139&149&151&157&163&167&173&179&181&191&193&197&199&211&223&227&229&233&239&241&251&257&263&269&271&277&281&283&293&307&311&313&317&331&337&347&349&353&359&367&373&379&383&389&397&401&409&419&421&431&433&439&443&449&457&461&463&467&479&487&491&499&503&509&521&523&541&547&557&563&569&571&577&587&593&599&601&607&613&617&619&631&641&643&647&653&659&661&673&677&683&691&701&709&719&727&733&739&743&751&757&761&769&773&787&797&809&811&821&823&827&829&839&853&857&859&863&877&881&883&887&907&911&919&929&937&941&947&953&967&971&977&983&991&997
You can use below query either to achieve the same result without using SPT_VALUES view of master database.
you can use below query too.
DECLARE @PRIME VARCHAR(max) ='';
WITH nums
AS (SELECT 0 AS NUM
UNION ALL
SELECT num + 1
FROM nums
WHERE num < 9)
SELECT @PRIME = @PRIME + Cast(A.num AS VARCHAR(3)) + '&'
FROM (SELECT A.num + B.num * 10 + C.num * 100 AS NUM
FROM nums A
CROSS JOIN nums B
CROSS JOIN nums C) A
LEFT JOIN (SELECT A.num + B.num * 10 + C.num * 100 AS NUM
FROM nums A
CROSS JOIN nums B
CROSS JOIN nums C) B
ON Sqrt(A.num) >= B.num
AND B.num > 1
WHERE A.num > 1
GROUP BY A.num
HAVING Sum(CASE
WHEN A.num % B.num = 0 THEN 1
ELSE 0
END) = 0
ORDER BY A.num
PRINT Substring(@PRIME, 0, Len(@PRIME))
For example, the output for all prime numbers <=10 would be:
2&3&5&7
Use below T-SQL query to achieve the above scenario
SELECT DISTINCT
REPLACE(STUFF(REPLACE((SELECT '~' + CAST(ST1.Number AS VARCHAR) AS 'data()'
FROM (SELECT DISTINCT Number
FROM MASTER..SPT_VALUES AS a
WHERE
A.Number > 1 AND A.Number <= 1000 AND
NOT EXISTS
(
SELECT 1 FROM MASTER..SPT_VALUES AS b WHERE b.Number > 1
AND b.Number < a.Number
AND a.Number % b.Number = 0)) ST1
ORDER BY ST1.Number
FOR XML PATH('')),'~','&'), 1, 1, ''),' ','') as [PRIME_NUMBERS]
FROM MASTER..SPT_VALUES ST2 WHERE ST2.Number > 1 AND ST2.Number <= 1000
Please see the below output of the above query:
2&3&5&7&11&13&17&19&23&29&31&37&41&43&47&53&59&61&67&71&73&79&83&89&97&101&103&107&109&113&127&131&137&139&149&151&157&163&167&173&179&181&191&193&197&199&211&223&227&229&233&239&241&251&257&263&269&271&277&281&283&293&307&311&313&317&331&337&347&349&353&359&367&373&379&383&389&397&401&409&419&421&431&433&439&443&449&457&461&463&467&479&487&491&499&503&509&521&523&541&547&557&563&569&571&577&587&593&599&601&607&613&617&619&631&641&643&647&653&659&661&673&677&683&691&701&709&719&727&733&739&743&751&757&761&769&773&787&797&809&811&821&823&827&829&839&853&857&859&863&877&881&883&887&907&911&919&929&937&941&947&953&967&971&977&983&991&997
You can use below query either to achieve the same result without using SPT_VALUES view of master database.
DECLARE @Nums TABLE(Number INT)
DECLARE @NUM INT =1
WHILE (@NUM <= 1000)
BEGIN
INSERT @Nums SELECT @NUM
SET @NUM = @NUM + 1
END
BEGIN
INSERT @Nums SELECT @NUM
SET @NUM = @NUM + 1
END
SELECT DISTINCT
REPLACE(STUFF(REPLACE((SELECT '~' + CAST(ST1.Number AS VARCHAR) AS 'data()'
FROM (SELECT DISTINCT Number
FROM @Nums AS a
WHERE
A.Number > 1 AND A.Number <= 1000 AND
NOT EXISTS
(
SELECT 1 FROM @Nums AS b WHERE b.Number > 1
AND b.Number < a.Number
AND a.Number % b.Number = 0)) ST1
ORDER BY ST1.Number
FOR XML PATH('')),'~','&'), 1, 1, ''),' ','') as [PRIME_NUMBERS]
FROM @Nums ST2 WHERE ST2.Number > 1 AND ST2.Number <= 1000
REPLACE(STUFF(REPLACE((SELECT '~' + CAST(ST1.Number AS VARCHAR) AS 'data()'
FROM (SELECT DISTINCT Number
FROM @Nums AS a
WHERE
A.Number > 1 AND A.Number <= 1000 AND
NOT EXISTS
(
SELECT 1 FROM @Nums AS b WHERE b.Number > 1
AND b.Number < a.Number
AND a.Number % b.Number = 0)) ST1
ORDER BY ST1.Number
FOR XML PATH('')),'~','&'), 1, 1, ''),' ','') as [PRIME_NUMBERS]
FROM @Nums ST2 WHERE ST2.Number > 1 AND ST2.Number <= 1000
you can use below query too.
DECLARE @PRIME VARCHAR(max) ='';
WITH nums
AS (SELECT 0 AS NUM
UNION ALL
SELECT num + 1
FROM nums
WHERE num < 9)
SELECT @PRIME = @PRIME + Cast(A.num AS VARCHAR(3)) + '&'
FROM (SELECT A.num + B.num * 10 + C.num * 100 AS NUM
FROM nums A
CROSS JOIN nums B
CROSS JOIN nums C) A
LEFT JOIN (SELECT A.num + B.num * 10 + C.num * 100 AS NUM
FROM nums A
CROSS JOIN nums B
CROSS JOIN nums C) B
ON Sqrt(A.num) >= B.num
AND B.num > 1
WHERE A.num > 1
GROUP BY A.num
HAVING Sum(CASE
WHEN A.num % B.num = 0 THEN 1
ELSE 0
END) = 0
ORDER BY A.num
PRINT Substring(@PRIME, 0, Len(@PRIME))
No comments:
Post a Comment