Sort by

recency

|

1361 Discussions

|

  • + 0 comments
    WITH RECURSIVE numbers(n) AS (
        SELECT 2
        UNION ALL
        SELECT n + 1 FROM numbers WHERE n < 1000
    ),
    divisors(d) AS (
        SELECT 2
        UNION ALL
        SELECT d + 1 FROM divisors WHERE d < 1000
    )
    SELECT GROUP_CONCAT(n SEPARATOR '&')
    FROM numbers
    WHERE NOT EXISTS (
        SELECT 1 FROM divisors
        WHERE divisors.d < numbers.n AND numbers.n % divisors.d = 0
    );
    
  • + 0 comments

    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;

  • + 0 comments
    DECLARE @i INT = 3
    DECLARE @result VARCHAR(7000) = "2"
    
    WHILE @i <= 1000
    BEGIN
        DECLARE @prime INT = 1
        DECLARE @j INT = 2
        
        WHILE @j < @i
        BEGIN
            IF(@i%@j = 0)
            BEGIN
                SET @prime = 2
                BREAK
            END
            SET @j = @j + 1
        END
        
        IF(@prime = 1)
        BEGIN
            SET @result = @result + "&" + CAST(@i AS VARCHAR)
        END
        
        SET @i = @i + 1
    END
    PRINT(@result)
    
  • + 0 comments

    MS SQL Server

    DECLARE @i INT =2;
    DECLARE @max INT =1000;
    DECLARE @sqrt DECIMAL (10,4)= 0; 
    DECLARE @wholenum INT = 0;
    DECLARE @j INT =2;
    DECLARE @isPrime BIT;
    DECLARE @output VARCHAR(MAX)=''
    
    WHILE @i <= @max 
    BEGIN
       SET @sqrt= SQRT(@i)
       SET @wholenum= FLOOR(@sqrt)
       SET @isPrime = 1
       
       WHILE @j <=@wholenum
       BEGIN
          IF @i % @j = 0 
          BEGIN
           SET @isPrime = 0
          BREAK;
          END
          SET @j= @j+1 
       END 
       
      
    IF @isPrime = 1
    BEGIN 
        IF LEN(@output) > 0
            SET @output = @output + '&' + CAST(@i AS VARCHAR);
        ELSE
            SET @output = CAST(@i AS VARCHAR);
    END
    
       
       SET @i = @i+1 
       SET @j=2
    END
    
    
    PRINT @output
    
  • + 0 comments
    select 2||'&'||  listagg(cn,'&') within group( order by cn)
     from 
        (select distinct cn
         from 
            (with cte as 
                    (select level as cn  
                     from dual connect by level<= 1000),
                cte2 as     (select level as cn  
                     from dual connect by level<= 1000) 
            --
                select cte.cn, 
                       cte2.cn as cn2,
                       cte.cn/cte2.cn,
                       case when round(cte.cn/cte2.cn) = cte.cn/cte2.cn then 1 else
                            0 end as ind,
                       max(case when round(cte.cn/cte2.cn) = cte.cn/cte2.cn then 1 else
                            0 end) over (partition by cte.cn) as is_not_prime
                from cte
                 join cte2 on cte.cn > cte2.cn
                 where cte2.cn > 1)
            where is_not_prime = 0)
     ;
    

    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