Skip to content

Commit eb92fca

Browse files
dakshdeepHERESSwiniarskiyangc95Dusch4593CBID2
authored
[Concept Entry] Created Terms folder and added Euclidean algorithm (#2455)
* added loops.md in kotlin folder * added more info and a introduction paragraph for loops * Added data-types.md in the kotlin folder * Update content/kotlin/concepts/loops/loops.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/loops/loops.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/loops/loops.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/loops/loops.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/loops/loops.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/loops/loops.md Co-authored-by: SSwiniarski <[email protected]> * Update loops.md * Update loops.md * Update loops.md * changed the sequence of for while and do while loops * del * changed tags * Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <[email protected]> * Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <[email protected]> * Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <[email protected]> * Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <[email protected]> * Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <[email protected]> * Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <[email protected]> * Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <[email protected]> * Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <[email protected]> * Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <[email protected]> * Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <[email protected]> * Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <[email protected]> * Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <[email protected]> * Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <[email protected]> * Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <[email protected]> * Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <[email protected]> * changed the sentence in line 278 * Update data-types.md * added variables.md in kotlin folder * Update content/kotlin/concepts/variables/variables.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/variables/variables.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/variables/variables.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/variables/variables.md Co-authored-by: SSwiniarski <[email protected]> * Update variables.md * del * added hashmap.md in kotlin folder * did some changes * added remove function of hashmap * Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <[email protected]> * Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: Brandon Dusch <[email protected]> * Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: Brandon Dusch <[email protected]> * Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: Brandon Dusch <[email protected]> * Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: Brandon Dusch <[email protected]> * Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: Brandon Dusch <[email protected]> * del * adding eluclidean algotrithm * added euclidean algorith method 1 * completed all 3 methods and time complexity * changing the title numbers * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: Christine Belzie <[email protected]> * Suggested changes * Update euclidean-algorithm.md * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: Christine Belzie <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: Christine Belzie <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: Christine Belzie <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: Christine Belzie <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: SSwiniarski <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: SSwiniarski <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: SSwiniarski <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: SSwiniarski <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: SSwiniarski <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: SSwiniarski <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: SSwiniarski <[email protected]> * Pseudo changes * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: SSwiniarski <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: SSwiniarski <[email protected]> * Changing tags * Update euclidean-algorithm.md * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz <[email protected]> * review edits --------- Co-authored-by: SSwiniarski <[email protected]> Co-authored-by: Christine Yang <[email protected]> Co-authored-by: Brandon Dusch <[email protected]> Co-authored-by: Christine Belzie <[email protected]> Co-authored-by: caupolicandiaz <[email protected]>
1 parent d4a8619 commit eb92fca

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
---
2+
Title: 'Euclidean Algorithm'
3+
Description: 'A simple and efficient method for finding the highest common factor (HCF), also known as the greatest common divisor (GCD), of two numbers.'
4+
Subjects:
5+
- 'Computer Science'
6+
Tags:
7+
- 'Algorithms'
8+
- 'Arithmetic'
9+
CatalogContent:
10+
- 'learn-java'
11+
- 'paths/computer-science'
12+
---
13+
14+
The **Euclidean algorithm** is a recursive algorithm that will find the highest common factor (HCF) of two numbers. The HCF is the largest value that will evenly divide (leave no remainder) both values. It is based on the observation that the HCF can be found by iteratively comparing two values, substituting the difference of the values alternatively, until the values are equivalent. This convergent value is the HCF.
15+
16+
There are multiple methods to solve and find the highest common factor (HCF) of two numbers. In this explanation, let's explore a basic approach first and then move on to the Euclidean algorithm.
17+
18+
## Method 1
19+
20+
In the basic approach, the purpose is to find the GCD. To do this, find the minimum value between the two given numbers. Then, divide both numbers by the minimum value. If either of the divisions results in a remainder, decrease the minimum value by one and continue dividing. This process repeats until the minimum value can divide both numbers evenly. At this point, the minimum value is the HCF.
21+
22+
The following code illustrates this method in Java:
23+
24+
```java
25+
public static void main(String[] args) {
26+
System.out.println(gcd(10,15));
27+
}
28+
public static int gcd(int a,int b){
29+
int minValue = Math.min(a,b);
30+
while(minValue>0){
31+
if(a%minValue==0 && b%minValue==0){
32+
break;
33+
}
34+
minValue--;
35+
}
36+
return minValue;
37+
}
38+
}
39+
```
40+
41+
The output for the above code will be:
42+
43+
```shell
44+
5
45+
```
46+
47+
## Method 2: A Basic Euclidean Algorithm Approach
48+
49+
In this method, the aim is to compare both `a` and `b`. The process begins by identifying the larger value and subtracting the smaller number from the larger number. The larger number is now replaced with the result of the subtraction. These steps repeat until the values are equal. This convergent value is the HCF.
50+
51+
The following code illustrates this method in Java:
52+
53+
```java
54+
class Euclidean1 {
55+
static int gcd(int a, int b) {
56+
57+
while (a != b) {
58+
if (a > b)
59+
a = a - b;
60+
else
61+
b = b - a;
62+
}
63+
64+
return a;
65+
}
66+
67+
public static void main(String[] args) {
68+
69+
int a = 15, b = 20;
70+
71+
System.out.println(gcd(a, b));
72+
}
73+
}
74+
```
75+
76+
The output for the above code will be:
77+
78+
```shell
79+
5
80+
```
81+
82+
The step by step execution of the above code is as follows:
83+
84+
- Since `b` is greater than `a` (20 > 15), here `b` is replaced with `b - a`, which gives results in `b` = 20 - 15 = 5.
85+
- Now `a` is 15 and `b` is 5.
86+
- The result is `a` = 15 - 5 = 10 after replacing `a` with `a - b`.
87+
- The values are now `a` = 10 and `b` = 5.
88+
- The process continues, `a` is replaced with `a - b`, resulting in `a` = 10 - 5 = 5.
89+
- Now `a` and `b` are both equal to 5.
90+
- At this point, the while loop exits, and `a` is returned. Therefore, the HCF of 15 and 20 is 5.
91+
92+
## Method 3: A Recursive Euclidean Approach
93+
94+
In this method, a recursive approach is used to implement the Euclidean algorithm for finding the greatest common divisor (GCD) of two integers, `a` and `b`. The method implements a function that takes `a` and `b` as integer parameters and returns an integer as the result. The algorithm begins by checking if `b` is equal to `0`. If it is, then it means that `a` is the GCD, and it returns `a` as the result. However, if `b` is not `0`, it indicates that there is a remainder when `a` is divided by `b`. In this case, the method calls itself recursively with the arguments `b` and `a % b`. This recursive call continues until `b` equals `0`, triggering the base case and yielding the GCD.
95+
96+
For more information on recursion, refer to this [resource](https://www.codecademy.com/learn/java-algorithms/modules/recursion-apcs/cheatsheet). The following code illustrates this method in Java:
97+
98+
```java
99+
public class Euclidean2 {
100+
public static void main(String[] args) {
101+
System.out.println(EuclideanOptimized(150, 500));
102+
103+
}
104+
static int EuclideanOptimized(int a, int b){
105+
if(b==0){
106+
return a;
107+
}
108+
return EuclideanOptimized(b,a%b);
109+
}
110+
}
111+
```
112+
113+
The output for the above code will be:
114+
115+
```shell
116+
50
117+
```
118+
119+
The step by step execution of this example is as follows:
120+
121+
- Given the input of two integers: `a` = 150 and `b` = 500, the code steps into the `EuclideanOptimized` function. The first `if` statement encountered checks if `b` is equal to `0`. In this particular case, `b` is not equal to `0`, resulting in the program exiting the `if` statement.
122+
- Then the code proceeds to return `EuclideanOptimized`, but with the arguments `(b, a % b)`.
123+
- In the first recursive cycle, the value of `a % b` will be 150. Since 150 is smaller than 500, it cannot be divided evenly by 500. Therefore, the remainder is equal to the original number, which is 150. Consequently, the next arguments for `EuclideanOptimized` are `(500, 150)`.
124+
- The function is restarted with the arguments `(500, 150)`. Upon entering the function, the `if` statement is encountered. However, since `b` is not equal to `0`, the program moves on to the next recursive cycle with the arguments `(150, 50)` ( 500 % 150 yields 50).
125+
- Once again, the function is initiated with arguments `(150, 50)`. As in previous iterations, `b` is not equal to `0`, and thus the program moves on to the next recursive cycle. The second argument in this case is determined by calculating the modulus 150 % 50, which yields the argument values `(50, 0)`.
126+
- In this recursive cycle, when the code enters the `if` statement, the condition `b == 0` is satisfied. Therefore, the function will return `a`, which has a value of 50, the highest common factor for the original arguments.
127+
128+
## Time Complexities
129+
130+
- Method 1:
131+
132+
- The first example uses a simple iterative approach to find the greatest common divisor (GCD) of two numbers. It starts by finding the minimum value between `a` and `b`, then iterates from that value down to one, checking if it divides both `a` and `b`. Therefore, the time complexity of this code is _O(min(a, b))_.
133+
134+
- Method 2:
135+
136+
- The second example also calculates the GCD using an iterative approach known as the Euclidean algorithm. It repeatedly subtracts the smaller number from the larger number until the two numbers become equal (the GCD). The time complexity of this algorithm depends on the number of iterations required to reach the GCD. In the worst case, where one number is a multiple of the other, the time complexity is _O(max(a, b))_.
137+
138+
- Method 3:
139+
140+
- The third example is an optimized version of the Euclidean algorithm that uses recursion. It calculates the GCD by repeatedly taking the modulus of `a` with `b` and calling itself with the new values (`b` and `a%b`) until `b` equals `0`. The time complexity of this optimized Euclidean algorithm is _O(log(max(a, b)))_ since the algorithm reduces the values quickly by taking the modulus.
141+
142+
To summarize, the time complexities of the three codes are as follows:
143+
144+
- Method 1: _O(min(a, b))_
145+
- Method 2: _O(max(a, b))_
146+
- Method 3: _O(log(max(a, b)))_
147+
148+
> **Note:** These time complexities typically represent the worst-case scenarios and assume that the `a` and `b` values are relatively large. In practice, the actual time taken by the algorithms can vary depending on the input values.

0 commit comments

Comments
 (0)