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
|
1246 Discussions
|
Please Login in order to post a comment
MySQL (This is my approach as a person just getting deeper into SQL):
DELIMITER // CREATE PROCEDURE PRIME() BEGIN DECLARE I INT DEFAULT 2; DECLARE V INT DEFAULT 1; DECLARE C INT DEFAULT 0; DECLARE RESULT VARCHAR(10000) DEFAULT ' '; WHILE I <= 1000 DO SET V = 1; SET C = 0; WHILE V <= I DO IF MOD (I, V) = 0 THEN SET C = C + 1; END IF; SET V = V + 1; END WHILE; IF C <= 2 THEN SET RESULT = CONCAT(RESULT, I, '&'); END IF; SET I = I + 1; END WHILE; SET RESULT = SUBSTRING(RESULT, 1, CHAR_LENGTH(RESULT) - 1); SELECT RESULT AS PrimeNumbers; END // DELIMITER ; CALL PRIME();
MySQL:-
SET @N = 1000; WITH RECURSIVE numbers AS ( SELECT 2 AS num UNION ALL SELECT num + 1 FROM numbers WHERE num + 1 <= @N ), primes AS ( SELECT num FROM numbers n1 WHERE NOT EXISTS ( SELECT 1 FROM numbers n2 WHERE n2.num < n1.num AND n1.num % n2.num = 0 AND n2.num > 1 ) ) SELECT GROUP_CONCAT(num SEPARATOR '&') AS primes FROM primes;
delimiter $$
create procedure prime () begin declare i, d, c int; declare result varchar (1000); set i = 2; set result = ' ';
end
For MySQL
The query looks long but I just wanted to type it in a organized and clean manner. Hope u get to understand