0% found this document useful (0 votes)
7 views2 pages

Assignment 10 202101448

The document contains a C++ program that checks if a given number is prime using the Miller-Rabin primality test. It includes functions for modular exponentiation and primality testing, and prompts the user to input a number. The program outputs whether the number is prime or not based on the test results.

Uploaded by

202101448
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Assignment 10 202101448

The document contains a C++ program that checks if a given number is prime using the Miller-Rabin primality test. It includes functions for modular exponentiation and primality testing, and prompts the user to input a number. The program outputs whether the number is prime or not based on the test results.

Uploaded by

202101448
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Name : Arjav Gandhi

Student ID : 202101448

Code :

#include <bits/stdc++.h>
using namespace std;
int power(int a, int n, int
p)
{ int res =
1; a = a %
p;
while (n >
0)
{ if (n & 1)
res = (res * a) % p;
n = n >> 1;
a = (a * a) % p;
}
return res;
} bool isPrime(int n,
int k)
{
if (n <= 1 || n ==
4) return false;
if (n <= 3)
return true;
while (k >
0)
{ int a = 2 + rand() %
(n - 4);

if (__gcd(n, a) != 1)
return false;
if (power(a, n - 1, n) !=
1) return false;
k-
-;
}
return true;
}
int main()
{ int k = 3; int Q; cout << "Enter the number : "; cin >>
Q; isPrime(Q, k) ? cout << "It's a prime number\n" : cout << "It is
not a prime number\n";
return
0;
}

OUTPUT :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy