Hãy luôn nhớ cảm ơn và vote 5*
nếu câu trả lời hữu ích nhé!
C++:
#include <iostream>
using namespace std;
int main()
{
int N;
cout << "Nhap so N: ";
cin >> N;
if (N < 2)
{
cout << "NO" << endl;
}
else if (N == 2)
{
cout << "YES" << endl;
}
else
{
for (int i = 2; i * i <= N; i++)
{
if (N % i == 0)
{
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
}
return 0;
}
Pascal:
program CheckPrimeNumber;
function IsPrime(n: integer): boolean;
var
i: integer;
is_prime: boolean;
begin
is_prime := true;
if (n = 1) or (n = 2) then
begin
IsPrime := true;
exit;
end;
for i := 2 to trunc(sqrt(n)) do
begin
if (n mod i = 0) then
begin
is_prime := false;
break;
end;
end;
IsPrime := is_prime;
end;
var
n: integer;
begin
readln(n);
if IsPrime(n) then
writeln('YES')
else
writeln('NO');
end.
Python:
import math
def is_prime(n: int) -> bool:
if n == 1 or n == 2:
return True
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
n = int(input())
if is_prime(n):
print('YES')
else:
print('NO')
Hãy giúp mọi người biết câu trả lời này thế nào?
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a;
bool check(ll k)
{
if(k<2) return false;
if(k==2||k==3) return true;
for(ll i=2;i<=sqrt(k);i++)
if(k%i==0) return false;
return true;
}
int main()
{
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>a;
if (check(a))
cout<<"YES";
else
cout<<"NO";
}
Hãy giúp mọi người biết câu trả lời này thế nào?
Bảng tin