diff --git a/Competitive Coding/Math/Primality Test/Fermat Method/README.md b/Competitive Coding/Math/Primality Test/Fermat Method/README.md new file mode 100644 index 000000000..0eb2a8741 --- /dev/null +++ b/Competitive Coding/Math/Primality Test/Fermat Method/README.md @@ -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 + + +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. diff --git a/Competitive Coding/Math/Primality Test/Fermat Method/SrcCode.cpp b/Competitive Coding/Math/Primality Test/Fermat Method/SrcCode.cpp new file mode 100644 index 000000000..9e011608d --- /dev/null +++ b/Competitive Coding/Math/Primality Test/Fermat Method/SrcCode.cpp @@ -0,0 +1,64 @@ + +#include +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 diff --git a/Competitive Coding/Math/Primality Test/Optimized School Method/README.md b/Competitive Coding/Math/Primality Test/Optimized School Method/README.md new file mode 100644 index 000000000..0bf8c2b10 --- /dev/null +++ b/Competitive Coding/Math/Primality Test/Optimized School Method/README.md @@ -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 + +**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)). diff --git a/Competitive Coding/Math/Primality Test/Optimized School Method/SrcCode.cpp b/Competitive Coding/Math/Primality Test/Optimized School Method/SrcCode.cpp new file mode 100644 index 000000000..a12867135 --- /dev/null +++ b/Competitive Coding/Math/Primality Test/Optimized School Method/SrcCode.cpp @@ -0,0 +1,35 @@ +// A optimized school method based C++ program to check if a number is prime or not. + +#include +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