mysql:输出1000以内的素数

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

判断素数:从2到n-1判断有没有能整除n的数。如果有,则不是素数,否则,是素数。可以判断从2到sqrt(n)是否能整除n,可以降低时间复杂度。

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 NUMA *NUMA <= NUMB
    AND NUMA > 1
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值