Skip to content

Commit 2273ff4

Browse files
authored
Merge pull request #206 from Mukulbaid63/pf_2
Euclid's Algorithm added
2 parents 6d14a0e + 63e2547 commit 2273ff4

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Java program to demonstrate working of extended
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class Euclid
6+
{
7+
/*Recursive function to calculate gcd using Euclid's algo*/
8+
public static int gcd(int a,int b)
9+
{
10+
if (a==0)
11+
return b;
12+
13+
return gcd(b%a, a);
14+
}
15+
16+
public static void main(String[] args)
17+
{
18+
Scanner sc=new Scanner(System.in);
19+
20+
/*Taking a & b as user input*/
21+
int a=sc.nextInt();
22+
int b=sc.nextInt();
23+
24+
System.out.println("GCD : " + gcd(a,b)); //Output
25+
26+
}
27+
28+
}
29+

Java/Euclid's GCD Algorithm/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
# Sample Input:
3+
4+
10 15
5+
6+
7+
8+
# Sample Output:
9+
10+
5
11+
12+
13+
14+
# Sample Input:
15+
16+
20 48
17+
18+
19+
20+
# Sample Output:
21+
22+
4
23+
24+
25+
26+
# Explaination:
27+
TC 1:
28+
10 = 2 * 5
29+
30+
15 = 3 * 5
31+
32+
The Greatest common divisor is 5
33+
34+
35+
36+
TC 2:
37+
20 = 2 * 2 * 5
38+
39+
48 = 2 * 2 * 2 * 2 * 3
40+
41+
The Greatest common divisor is 2 * 2 = 4
42+
43+
44+
45+
The algorithm is based on below facts:
46+
47+
48+
49+
--If we subtract smaller number from larger (we reduce larger number), GCD doesn’t change. So if we keep subtracting repeatedly the larger of two, we end up with GCD.
50+
51+
--Now instead of subtraction, if we divide smaller number, the algorithm stops when we find remainder 0.
52+
53+
54+
55+
# Time Complexity:
56+
57+
<h3>O(log min(a,b))</h3>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Relevance in Cryptography:
2+
RSA, which based on the great difficulty of integer factorization, is the most widely-used public-key cryptosystem used in electronic commerce. Euclid algorithm and extended Euclid algorithm are the best algorithms to solve the public key and private key in RSA. Extended Euclid algorithm in IEEE P1363 is improved by eliminating the negative integer operation, which reduces the computing resources occupied by RSA, hence has an important application value.
3+
4+
5+

0 commit comments

Comments
 (0)