-
Notifications
You must be signed in to change notification settings - Fork 162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Algorithms on Primality Test #251
Changes from 5 commits
92654db
bc9bd32
8e98e75
6e3a4d3
4f8fbf0
3f8bd12
73ace60
62a3c5d
02bc991
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Given a number n, check if it is prime or not. This method is a probabilistic method and is based on below Fermat’s Little Theorem. | ||
|
||
Fermat's Little Theorem: | ||
If n is a prime number, then for every a, 1 <= a < n, | ||
|
||
a^n-1 ≡ 1 (mod n) | ||
OR | ||
a^n-1 % n = 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also here |
||
|
||
|
||
Example: | ||
|
||
Since 5 is prime, 2^4 ≡ 1 (mod 5) [or 2^4%5 = 1], | ||
|
||
3^4 ≡ 1 (mod 5) and 4^4 ≡ 1 (mod 5). | ||
|
||
Since 7 is prime, 2^6 ≡ 1 (mod 7), | ||
|
||
3^6 ≡ 1 (mod 7), 4^6 ≡ 1 (mod 7) | ||
5^6 ≡ 1 (mod 7) and 6^6 ≡ 1 (mod 7). | ||
|
||
We will take help of this Fermat's Little Theorem as a function to calculate a no is prime or not. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
/* Iterative Function to calculate (a^n)%p in O(logy) */ | ||
int power(int a, unsigned int n, int p) | ||
{ | ||
int res = 1; // Initialize result | ||
a = a % p; // Update 'a' if 'a' >= p | ||
|
||
while (n > 0) | ||
{ | ||
// If n is odd, multiply 'a' with result | ||
if (n & 1) | ||
res = (res*a) % p; | ||
|
||
// n must be even now | ||
n = n>>1; // n = n/2 | ||
a = (a*a) % p; | ||
} | ||
return res; | ||
} | ||
|
||
// If n is prime, then always returns true, If n is | ||
// composite than returns false with high probability | ||
// Higher value of k increases probability of correct result. | ||
|
||
bool isPrime(unsigned int n, int k) | ||
{ | ||
// Corner cases | ||
if (n <= 1 || n == 4) return false; | ||
if (n <= 3) return true; | ||
|
||
// Try k times | ||
while (k>0) | ||
{ | ||
// Pick a random number in [2..n-2] | ||
// Above corner cases make sure that n > 4 | ||
int a = 2 + rand()%(n-4); | ||
|
||
// Fermat's little theorem | ||
if (power(a, n-1, n) != 1) | ||
return false; | ||
|
||
k--; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
// Driver Program | ||
int main() | ||
{ | ||
int k = 3; | ||
isPrime(11, k)? cout << " true\n": cout << " false\n"; | ||
isPrime(15, k)? cout << " true\n": cout << " false\n"; | ||
return 0; | ||
} | ||
|
||
|
||
Output: | ||
|
||
true | ||
false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Given a positive integer, check if the number is prime or not. A prime is a natural number greater than 1 that has no | ||
positive divisors other than 1 and itself. | ||
|
||
Examples of first few prime numbers are {2, 3, 5, 7,...} | ||
|
||
Examples: | ||
|
||
Input: n = 11 | ||
|
||
Output: true | ||
|
||
Input: n = 15 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make all input / output bold and tilde for n assignment |
||
Output: false | ||
|
||
Input: n = 1 | ||
|
||
Output: false | ||
|
||
A simple solution is to iterate through all numbers from 2 to n-1 and for every number check if it divides n. If we find | ||
any number that divides, we return false. Instead of checking till n, we can check till √n because a larger factor of n | ||
must be a multiple of smaller factor that has been already checked. The algorithm can be improved further by observing | ||
that all primes are of the form 6k ± 1, with the exception of 2 and 3. This is because all integers can be expressed | ||
as (6k + i) for some integer k and for i = ?1, 0, 1, 2, 3, or 4; 2 divides (6k + 0), (6k + 2), (6k + 4); and 3 divides | ||
(6k + 3). So a more efficient method is to test if n is divisible by 2 or 3, then to check through all the numbers of | ||
form 6k ± 1. | ||
|
||
Time complexity of this solution is O(√n). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// A optimized school method based C++ program to check if a number is prime or not. | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
bool isPrime(int n) | ||
{ | ||
// Corner cases | ||
if (n <= 1) return false; | ||
if (n <= 3) return true; | ||
|
||
// This is checked so that we can skip | ||
// middle five numbers in below loop | ||
if (n%2 == 0 || n%3 == 0) return false; | ||
|
||
for (int i=5; i*i<=n; i=i+6) | ||
if (n%i == 0 || n%(i+2) == 0) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
|
||
// Driver Program | ||
int main() | ||
{ | ||
isPrime(23)? cout << " true\n": cout << " false\n";//use of ternary operator | ||
isPrime(35)? cout << " true\n": cout << " false\n"; | ||
return 0; | ||
} | ||
|
||
Output: | ||
|
||
true | ||
false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a tilde here like this
a^n-1 = 1 mod(n)