We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Print Prime Numbers
Print Prime Numbers
Sort by
recency
|
1361 Discussions
|
Please Login in order to post a comment
WITH RECURSIVE numbers AS ( SELECT 2 AS n UNION ALL SELECT n + 1 FROM numbers WHERE n + 1 <= 1000 ), primes AS ( SELECT n FROM numbers AS outer_num WHERE NOT EXISTS ( SELECT 1 FROM numbers AS inner_num WHERE inner_num.n < outer_num.n AND inner_num.n > 1 AND MOD(outer_num.n, inner_num.n) = 0 ) ) SELECT GROUP_CONCAT(n SEPARATOR '&') AS prime_numbers FROM primes;
MS SQL Server
Step 1 - internal most queries - get all numbers 1-1000, join on identical recursive table, where left table number greater than right table. Step 2 - for each number on left table, add indicator to see if, any instance where division/right table number = round(division) - that means not a prime number. Partition by number on left table, such that if any exist, mark all of them as not-prime (1). Step 3 - extract distinct values (not_prime=0), concatence, add the digit 2 in front.
S