当前位置:网站首页>L1-028 judging prime number (Lua)

L1-028 judging prime number (Lua)

2022-07-07 19:17:00 Just be interesting

subject

The goal of this question is very simple , Is to determine whether a given positive integer is a prime number .

Input format :
Enter a positive integer on the first line N(≤ 10), And then N That's ok , Each line gives a less than 2
31
A positive integer that needs to be judged .

Output format :
For each positive integer to be judged , If it's a prime , Output in one line Yes, Otherwise output No.

sample input :
2
11
111
sample output :
Yes
No

Code

function isPrime(x)
    if x == 0 or x == 1 then
        return false
    end
    
    local i = 2
    while i <= x / i do
        if x % i == 0 then
            return false
        end
        
        i = i + 1
    end
    
    return true
end

local n = io.read()

for i = 1, n do
    local num = tonumber(io.read())

    if isPrime(num) then
        print("Yes")
    else
        print("No")
    end
end
原网站

版权声明
本文为[Just be interesting]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071515232660.html