Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7ea7b3f

Browse files
Merge pull request #251 from Priyangshuyogi/myone
Algorithms on Primality Test
2 parents 82a0f3a + 02bc991 commit 7ea7b3f

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
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.
2+
3+
Fermat's Little Theorem:
4+
If n is a prime number, then for every a, 1 <= a < n,
5+
6+
a^n-1 ~ 1 mod (n)
7+
OR
8+
a^n-1 % n ~ 1
9+
10+
11+
Example:
12+
13+
Since 5 is prime, 2^4 ≡ 1 (mod 5) [or 2^4%5 = 1],
14+
15+
3^4 ≡ 1 (mod 5) and 4^4 ≡ 1 (mod 5).
16+
17+
Since 7 is prime, 2^6 ≡ 1 (mod 7),
18+
19+
3^6 ≡ 1 (mod 7), 4^6 ≡ 1 (mod 7)
20+
5^6 ≡ 1 (mod 7) and 6^6 ≡ 1 (mod 7).
21+
22+
We will take help of this Fermat's Little Theorem as a function to calculate a no is prime or not.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
/* Iterative Function to calculate (a^n)%p in O(logy) */
6+
int power(int a, unsigned int n, int p)
7+
{
8+
int res = 1; // Initialize result
9+
a = a % p; // Update 'a' if 'a' >= p
10+
11+
while (n > 0)
12+
{
13+
// If n is odd, multiply 'a' with result
14+
if (n & 1)
15+
res = (res*a) % p;
16+
17+
// n must be even now
18+
n = n>>1; // n = n/2
19+
a = (a*a) % p;
20+
}
21+
return res;
22+
}
23+
24+
// If n is prime, then always returns true, If n is
25+
// composite than returns false with high probability
26+
// Higher value of k increases probability of correct result.
27+
28+
bool isPrime(unsigned int n, int k)
29+
{
30+
// Corner cases
31+
if (n <= 1 || n == 4) return false;
32+
if (n <= 3) return true;
33+
34+
// Try k times
35+
while (k>0)
36+
{
37+
// Pick a random number in [2..n-2]
38+
// Above corner cases make sure that n > 4
39+
int a = 2 + rand()%(n-4);
40+
41+
// Fermat's little theorem
42+
if (power(a, n-1, n) != 1)
43+
return false;
44+
45+
k--;
46+
}
47+
48+
return true;
49+
}
50+
51+
// Driver Program
52+
int main()
53+
{
54+
int k = 3;
55+
isPrime(11, k)? cout << " true\n": cout << " false\n";
56+
isPrime(15, k)? cout << " true\n": cout << " false\n";
57+
return 0;
58+
}
59+
60+
61+
Output:
62+
63+
true
64+
false
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Given a positive integer, check if the number is prime or not. A prime is a natural number greater than 1 that has no
2+
positive divisors other than 1 and itself.
3+
4+
Examples of first few prime numbers are {2, 3, 5, 7,...}
5+
6+
Examples:
7+
8+
**Input:** n = 11
9+
10+
**Output:** true
11+
12+
**Input:** n = 15
13+
14+
**Output:** false
15+
16+
**Input:** n = 1
17+
18+
**Output:** false
19+
20+
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
21+
any number that divides, we return false. Instead of checking till n, we can check till √n because a larger factor of n
22+
must be a multiple of smaller factor that has been already checked. The algorithm can be improved further by observing
23+
that all primes are of the form 6k ± 1, with the exception of 2 and 3. This is because all integers can be expressed
24+
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
25+
(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
26+
form 6k ± 1.
27+
28+
Time complexity of this solution is O(root(n)).
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// A optimized school method based C++ program to check if a number is prime or not.
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
bool isPrime(int n)
7+
{
8+
// Corner cases
9+
if (n <= 1) return false;
10+
if (n <= 3) return true;
11+
12+
// This is checked so that we can skip
13+
// middle five numbers in below loop
14+
if (n%2 == 0 || n%3 == 0) return false;
15+
16+
for (int i=5; i*i<=n; i=i+6)
17+
if (n%i == 0 || n%(i+2) == 0)
18+
return false;
19+
20+
return true;
21+
}
22+
23+
24+
// Driver Program
25+
int main()
26+
{
27+
isPrime(23)? cout << " true\n": cout << " false\n";//use of ternary operator
28+
isPrime(35)? cout << " true\n": cout << " false\n";
29+
return 0;
30+
}
31+
32+
Output:
33+
34+
true
35+
false

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /