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
|
1298 Discussions
|
Please Login in order to post a comment
WITH RECURSIVE numbers AS( SELECT 2 AS num UNION ALL SELECT num + 1 FROM numbers WHERE num < 1000 ), Prime AS ( SELECT num FROM numbers n WHERE NOT EXISTS( SELECT 1 FROM numbers n1 WHERE n.num % d.n1.num = 0 AND n.num != n1.num ) )
SELECT GROUP_CONCAT(num SEPARATOR '&') AS pnumbers FROM Prime;
with seq as (select level l1 from dual where level>1 connect by level<=1000) select listagg(s1.l1,'&')within group(order by s1.l1) from seq s1 where not exists (select 1 from seq s2 where s2.l1<=sqrt(s1.l1) and mod(s1.l1,s2.l1)=0);
DELIMITER // CREATE PROCEDURE p1(IN n INT) BEGIN DECLARE i INT DEFAULT 3; DECLARE result VARCHAR(10000) DEFAULT '2'; DECLARE flag INT DEFAULT 0; DECLARE j INT; loop1:LOOP SET flag = 0; SET j = i; loop2:LOOP SET j = j-1; IF j = 1 THEN LEAVE loop2; END IF; IF i%j = 0 THEN SET flag = 1; LEAVE loop2; END IF; END LOOP loop2; IF flag = 0 THEN SET result = CONCAT(result,'&',i); END IF; IF i = n THEN LEAVE loop1; END IF; SET i = i+1; END LOOP loop1; SELECT result; END // DELIMITER ;
CALL p1(1000);
-----------------sql server--------------------- declare @x int; declare @y int; declare @z int; declare @count int; declare @output NVARCHAR(MAX) = ''; SET @z = 1000; SET @x = 2; SET @y = 2; SET @count = 0;
while @x < 1000 begin while @y <= floor(sqrt(@x)) begin if @x % @y = 0 begin SET @count = @count + 1; break; end; SET @y = @y + 1; end; if @count = 0 begin SET @output = CONCAT(@output, CAST(@x as varchar), '&');
end;
SET @output = LEFT(@output, LEN(@output) - 1); print @output;
declare @PrimeNumCnt Int = 1, @PrimeNumUpTo int = 1000, @PrimeNumList nvarchar(4000); while @PrimeNumCnt <= @PrimeNumUpTo begin declare @IsPrime int = 0 Declare @N int = 1 while @N <= @PrimeNumCnt begin set @IsPrime = @IsPrime + (select case when @PrimeNumCnt <> @N and @PrimeNumCnt % @N = 0 then 1 else 0 end) if @IsPrime = 0 begin break end set @N = @N+1 end --select @PrimeNumCnt,@IsPrime if @IsPrime = 1 begin set @PrimeNumList = (select concat(@PrimeNumList,'&',@PrimeNumCnt)) end set @PrimeNumCnt = @PrimeNumCnt+1 end select right(@PrimeNumList,len(@PrimeNumList)-1);