K
Khách

Hãy nhập câu hỏi của bạn vào đây, nếu là tài khoản VIP, bạn sẽ được ưu tiên trả lời.

18 tháng 11 2025

Dưới đây là code C++ kiểm tra số nguyên tố bằng thuật toán Miller–Rabin (bản deterministic cho số 64-bit), dùng được an toàn cho mọi số nguyên 64-bit:

Miller–Rabin C++ (đầy đủ, chính xác cho 64-bit)



#include <bits/stdc++.h>
using namespace std;

using ull = unsigned long long;
using u128 = __uint128_t;

// Hàm nhân modular an toàn tránh tràn 64-bit
ull mul_mod(ull a, ull b, ull mod) {
    return (u128)a * b % mod;
}

// Hàm lũy thừa modular
ull pow_mod(ull a, ull d, ull mod) {
    ull result = 1;
    while (d) {
        if (d & 1) result = mul_mod(result, a, mod);
        a = mul_mod(a, a, mod);
        d >>= 1;
    }
    return result;
}

// Kiểm tra Miller-Rabin cho một cơ sở a
bool check_composite(ull n, ull a, ull d, int s) {
    ull x = pow_mod(a, d, n);
    if (x == 1 || x == n - 1) return false;

    for (int r = 1; r < s; r++) {
        x = mul_mod(x, x, n);
        if (x == n - 1) return false;
    }
    return true; // hợp số
}

// Miller–Rabin (deterministic cho 64-bit)
bool isPrime(ull n) {
    if (n < 2) return false;
    for (ull p : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37})
        if (n % p == 0) return n == p;

    // Viết n−1 = d * 2^s
    ull d = n - 1;
    int s = 0;
    while ((d & 1) == 0) {
        d >>= 1;
        s++;
    }

    // Các cơ sở đảm bảo đúng với mọi n < 2^64
    for (ull a : {2ULL, 325ULL, 9375ULL, 28178ULL, 450775ULL, 9780504ULL, 1795265022ULL}) {
        if (a % n == 0) continue; 
        if (check_composite(n, a, d, s)) return false;
    }
    return true; // số nguyên tố
}

int main() {
    ull n;
    cout << "Nhap n: ";
    cin >> n;

    if (isPrime(n)) cout << n << " la so nguyen to\n";
    else cout << n << " khong phai so nguyen to\n";
}

🎯 Ưu điểm của đoạn code này

  • ✔ Không bị tràn số khi nhân (dùng __uint128_t)
  • ✔ Đảm bảo chính xác tuyệt đối cho mọi số 64-bit
  • ✔ Chạy rất nhanh, kiểm tra số hàng chục chữ số trong vài micro–giây
18 tháng 11 2025

ai lấy kí tự đặc biệt thì tham khảo vào đây

亗 ϟ 卍 ☯ ム ㋰ 〄 ﮩ٨ـﮩﮩ٨ـ♡ﮩ٨ـﮩﮩ٨ـ ╰‿╯ ×͜× ×͡× ┊ ×᷼× ☂ ´꒳` ౨ৎ ᶻ 𝗓 𐰁 .ᐟ ݁ ˖Ი𐑼⋆ ༗ 〆 ㊎

18 tháng 11 2025



#include <bits/stdc++.h>
using namespace std;

using ull = unsigned long long;
using u128 = __uint128_t;

// Hàm nhân modular an toàn tránh tràn 64-bit
ull mul_mod(ull a, ull b, ull mod) {
return (u128)a * b % mod;
}

// Hàm lũy thừa modular
ull pow_mod(ull a, ull d, ull mod) {
ull result = 1;
while (d) {
if (d & 1) result = mul_mod(result, a, mod);
a = mul_mod(a, a, mod);
d >>= 1;
}
return result;
}

// Kiểm tra Miller-Rabin cho một cơ sở a
bool check_composite(ull n, ull a, ull d, int s) {
ull x = pow_mod(a, d, n);
if (x == 1 || x == n - 1) return false;

for (int r = 1; r < s; r++) {
x = mul_mod(x, x, n);
if (x == n - 1) return false;
}
return true; // hợp số
}

// Miller–Rabin (deterministic cho 64-bit)
bool isPrime(ull n) {
if (n < 2) return false;
for (ull p : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37})
if (n % p == 0) return n == p;

// Viết n−1 = d * 2^s
ull d = n - 1;
int s = 0;
while ((d & 1) == 0) {
d >>= 1;
s++;
}

// Các cơ sở đảm bảo đúng với mọi n < 2^64
for (ull a : {2ULL, 325ULL, 9375ULL, 28178ULL, 450775ULL, 9780504ULL, 1795265022ULL}) {
if (a % n == 0) continue;
if (check_composite(n, a, d, s)) return false;
}
return true; // số nguyên tố
}

int main() {
ull n;
cout << "Nhap n: ";
cin >> n;

if (isPrime(n)) cout << n << " la so nguyen to\n";
else cout << n << " khong phai so nguyen to\n";
}

11 tháng 3 2022

#include <bits/stdc++.h>

using namespace std;

bool kt;

int n,i;

int main()

{

cin>>n;

kt=true;

for (i=2; i*i<=n; i++)

if (n%i==0) kt=false;

if ((kt==true) and (n>1)) cout<<"YES";

else cout<<"NO";

return 0;

}

 

23 tháng 10 2017

uses crt;

const tb=30000;

var tt, dg, sp: real;

ht, dc: string;

begin

clrscr;

write('Nhap ho ten khach hang: ');

readln(ht);

write('Nhap dia chi khach hang: ');

readln(dc);

write('Nhap don gia: ');

readln(dg);

write('Nhap so phut su dung: ');

readln(sp);

tt:=dg*sp+tb;

write('Quy khach phai tra: ',tt,' dong.');

readln

end.

Chúc em học tốt!

15 tháng 2 2017

uses crt;

var i,n ,dem:integer;

begin

clrscr;

Write( 'nhap so nguyen duong n: ');readln(n);

dem:=0;

for i:=1 to n do if n mod i = 0 then dem:=dem+1;

if(n<2) or (dem< >2) then writeln (n,' khong phai la so nguyen to')

else if dem=2 then writeln(n,' la so nguyen to');

readln

end.

2 tháng 5 2017

program song_to;

uses crt;

var x,i,dem,tcs,t:longint;

function tongchuso(x:longint):longint;

begin

tcs:=0;

while x<>0 do

begin

tcs:=tcs+x mod 10;

x:=x div 10;

end;

tongchuso:=tcs;

end;

begin

clrscr;

write('nhap vao mot so:');readln(x);

dem:=0;

for i:=1 to x do

if x mod i=0 then dem:=dem+1;

if dem=2 then

begin

t:=0;

for i:=1 to tongchuso(x) do

if tongchuso(x) mod i =0 then t:=t+1;

if t=2 then write(x,' la so song to');

end

else write(x,' k la so song to');

readln;

end.banh

13 tháng 12 2020

uses crt;

var a:array[1..100]of integer;

i,n,t,kt,j:integer;

begin

clrscr;

write('Nhap n='); readln(n);

for i:=1 to n do

begin

write('A[',i,']='); readln(a[i]);

end;

writeln('Cac so vua nhap la: ');

for i:=1 to n do 

  write(a[i]:4);

writeln;

t:=0;

for i:=1 to n do 

  t:=t+a[i];

writeln('Tong cua day so la: ',t);

kt:=0;

for j:=2 to trunc(sqrt(t)) do 

  if t mod j=0 then 

    begin

kt:=1;

break;

end;

if kt=0 then writeln(t,' la so nguyen to')

else writeln(t,' khong la so nguyen to');

readln;

end.

13 tháng 12 2020

Cậu ơi sai khúc write('A[',i,']='); readln(a[i]); rồi nó cứ bị sao sao á

 

13 tháng 12 2020

uses crt;

var a:array[1..100]of integer;

i,n,t,kt:integer;

begin

clrscr;

write('Nhap n='); readln(n);

for i:=1 to n do

begin

write('A[',i,']='); readln(a[i]);

end;

writeln('Cac so ban vua nhap la: ');

for i:=1 to n do 

write(a[i]:4);

writeln;

t:=0;

for i:=1 to n do

t:=t+a[i];

writeln('Tong cac so vua nhap la: ',t);

if t>=2 then 

begin

kt:=0;

for i:=2 to trunc(sqrt(t)) do 

  if t mod i=0 then 

begin

kt:=1;

break;

end;

 if kt=0 then writeln(t,' la so nguyen to')

else writeln(t,' khong la so nguyen to');

end

else writeln(t,' khong la so nguyen to');

readln;

end.

25 tháng 1 2021

uses crt;

var a,x:longint;

{------------------ham-kiem-tra-so-nguyen-to---------------}

function ktnt(x:longint):boolean;

var kt:boolean;   

i:longint;

begin   

kt:=true;   

for i:=2 to trunc(sqrt(x)) do     

if x mod i=0 then       

begin           

kt:=false;           

break;       

end;   

if kt=true then ktnt:=true   

else ktnt:=false;

end;

{---------------chuong-trinh-chinh-------------------}

begin

clrscr;

repeat 

write('Nhap a='); readln(a);

until a>10;

if ktnt(a)=true then   

begin     

x:=a;     

repeat         

a:=a div 10;         

if ktnt(a)=true then a:=a div 10;     

until a<10;     

if (ktnt(a)=true) and (a>1) then writeln(x,' la so sieu nguyen to')     

else writeln(x,' khong la so sieu nguyen to');   

end

else writeln(a,' khong la so sieu nguyen to');

readln;

end.

bn ơi cấu hỏi hơi bị khó hiểu xíu 

21 tháng 5 2020

Bài 1:

uses crt;

var n,i,t:integer;

begin

clrscr;

write('Nhap n='); readln(n);

t:=0;

for i:=0 to n do

if i mod 2=0 then t:=t+a[i];

writeln('Tong cac so chan nho hon hoac bang ',n,' la: ',t);

readln;

end.

Bài 2:

uses crt;

var i,n,dem:integer;

begin

clrscr;

repeat

write('Nhap n=');readln(n);

until n>0;

dem:=0;

for i:=0 to n do

if i mod 3=0 then inc(dem);

writeln('So so chia het cho 3 nho hon hoac bang ',n,' la: ',dem);

readln;

end.

Bài 3:

uses crt;
var a:real;
n,i:integer;
begin
clrscr;
repeat
write('Nhap n='); readln(n);
until n>0;
a:=0;
for i:=1 to n do
a:=a+1/(i*(2*i+1));
writeln('Ket qua la: ',a:4:2);
readln;
end.

Bài 4:

uses crt;

var n,i,kt:integer;

begin

clrscr;

Repeat

write('Nhap n='); readln(n);

Until n>1;

kt:=0;

for i:=2 to n-1 do

if n mod i=0 then kt:=1;

if kt=0 then writeln(n,' la so nguyen to')

else writeln(n,' khong la so nguyen to');

readln;

end.

21 tháng 5 2020

Mai tớ cũng vậy :")

2 tháng 5 2021

Mình chỉ viết được pascal được không?

Var  n, i : integer;

     Begin

          write(‘Nhập số n = ‘);

          readln(n);

          i := 2;

          while  (n  mod  i <> 0) and (i < n)  do

              i := i + 1;

          if  i < n  then  write(n, ‘ là số nguyên tố.’)

          else  write(n, ‘ là hợp.’)

          readln;

     End.

23 tháng 3 2021

vậy nếu n=2 thì sao?

18 tháng 10 2016

câu 1:

var x:string

     y:integer

Câu 2:

var a,b:integer

begin

write('hay nhap gia tri cua a')

read(a)

wrietln('hãy nhập giá trị b')

read(b)

t:=a+b

writeln('tông cua a va b la',t)

 

18 tháng 10 2016

? đây là toán mà