【MySQL】hackerrank错题集

Print Prime Numbers

Write a query to print all prime numbers less than or equal to . 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 would be:
2&3&5&7

Correct Answer

  select GROUP_CONCAT(n1 SEPARATOR '&') from (
      select n1 from (select n1, count(*) as factors from (
          select @a := @a+1 as n1 from information_schema.tables t1, information_schema.tables t2 limit 1000)   t1, 
          (select @b := @b+1 as n2 from information_schema.tables t1, information_schema.tables t2 limit 1000)   t2 
          where t1.n1%t2.n2 = 0 group by t1.n1) t 
      where factors = 2 order by n1)t3;
Draw The Triangle 1

P(R) represents a pattern drawn by Julia in R rows. The following pattern represents P(5):
* * * * *
* * * *
* * *
* *
*
Write a query to print the pattern P(20)

Correct Answer

  select repeat('* ', @number := @number - 1) from information_schema.tables;

推荐阅读更多精彩内容