Algorithm


Problem Name: Sql - Print Prime Numbers

Problem Link: https://www.hackerrank.com/challenges/print-prime-numbers/problem?isFullScreen=true  

In this HackerRank Functions in SQL problem solution,

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

 

 

Code Examples

#1 Code Example with SQL

Code - SQL




    SELECT GROUP_CONCAT(NUMB SEPARATOR '&')
    FROM (
        SELECT @num:=@num+1 as NUMB FROM
        information_schema.tables t1,
        information_schema.tables t2,
        (SELECT @num:=1) tmp
    ) tempNum
    WHERE NUMB<=1000 AND NOT EXISTS(
        SELECT * FROM (
            SELECT @nu:=@nu+1 as NUMA FROM
                information_schema.tables t1,
                information_schema.tables t2,
                (SELECT @nu:=1) tmp1
                LIMIT 1000
            ) tatata
        WHERE FLOOR(NUMB/NUMA)=(NUMB/NUMA) AND NUMA1
    );

Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Symmetric Pairs in SQL solution in Hackerrank
Next
[Solved] Interviews in SQL solution in Hackerrank