-
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
92654db
Create README.md
Priyangshuyogi bc9bd32
Create SrcCode.cpp
Priyangshuyogi 8e98e75
Update README.md
Priyangshuyogi 6e3a4d3
Create README.md
Priyangshuyogi 4f8fbf0
Create SrcCode.cpp
Priyangshuyogi 3f8bd12
Update README.md
Priyangshuyogi 73ace60
Update README.md
Priyangshuyogi 62a3c5d
Update README.md
Priyangshuyogi 02bc991
Update README.md
Priyangshuyogi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
Competitive Coding/Math/Primality Test/Fermat Method/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
64 changes: 64 additions & 0 deletions
64
Competitive Coding/Math/Primality Test/Fermat Method/SrcCode.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
28 changes: 28 additions & 0 deletions
28
Competitive Coding/Math/Primality Test/Optimized School Method/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(root(n)). |
35 changes: 35 additions & 0 deletions
35
Competitive Coding/Math/Primality Test/Optimized School Method/SrcCode.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
use tilde here ``