Skip to content

[Concept Entry] Created Terms folder and added Euclidean algorithm #2455

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

Merged
merged 134 commits into from
Jun 23, 2023
Merged
Changes from all commits
Commits
Show all changes
134 commits
Select commit Hold shift + click to select a range
e7014e0
added loops.md in kotlin folder
dakshdeepHERE Jan 1, 2023
31dca9a
added more info and a introduction paragraph for loops
dakshdeepHERE Jan 1, 2023
30d2b99
Merge branch 'main' into loops
dakshdeepHERE Jan 2, 2023
fcdd14d
Added data-types.md in the kotlin folder
dakshdeepHERE Jan 2, 2023
5bf7922
Update content/kotlin/concepts/loops/loops.md
dakshdeepHERE Jan 2, 2023
162c7a9
Update content/kotlin/concepts/loops/loops.md
dakshdeepHERE Jan 2, 2023
43b62df
Update content/kotlin/concepts/loops/loops.md
dakshdeepHERE Jan 2, 2023
40eacf3
Update content/kotlin/concepts/loops/loops.md
dakshdeepHERE Jan 2, 2023
48166f2
Update content/kotlin/concepts/loops/loops.md
dakshdeepHERE Jan 2, 2023
22c708d
Update content/kotlin/concepts/loops/loops.md
dakshdeepHERE Jan 2, 2023
5f03889
Update loops.md
dakshdeepHERE Jan 2, 2023
885ea2a
Update loops.md
dakshdeepHERE Jan 2, 2023
d4b2ac2
Merge branch 'Codecademy:main' into loops
dakshdeepHERE Jan 2, 2023
0736dd9
Update loops.md
dakshdeepHERE Jan 2, 2023
b42143f
changed the sequence of for while and do while loops
dakshdeepHERE Jan 2, 2023
f6fd28e
del
dakshdeepHERE Jan 2, 2023
2e70242
Merge branch 'main' into data-types
dakshdeepHERE Jan 2, 2023
c219bc5
changed tags
dakshdeepHERE Jan 4, 2023
a0cec1e
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE Jan 4, 2023
28c3525
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE Jan 4, 2023
156cbaa
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE Jan 4, 2023
9fbd5e6
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE Jan 4, 2023
6a49a1c
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE Jan 4, 2023
ab6ebff
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE Jan 4, 2023
2f1cc3c
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE Jan 4, 2023
49b568f
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE Jan 4, 2023
7e30fba
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE Jan 4, 2023
970a372
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE Jan 4, 2023
4974ee0
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE Jan 4, 2023
37e863f
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE Jan 4, 2023
8f211b0
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE Jan 4, 2023
cd47aaa
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE Jan 4, 2023
f24382f
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE Jan 4, 2023
ff95ae2
changed the sentence in line 278
dakshdeepHERE Jan 4, 2023
9d8e67a
Update data-types.md
dakshdeepHERE Jan 4, 2023
73e5ae9
added variables.md in kotlin folder
dakshdeepHERE Jan 4, 2023
6a9d6b5
Merge branch 'main' into variables
SSwiniarski Jan 6, 2023
6af23b8
Update content/kotlin/concepts/variables/variables.md
dakshdeepHERE Jan 6, 2023
6381b7b
Update content/kotlin/concepts/variables/variables.md
dakshdeepHERE Jan 6, 2023
664148f
Update content/kotlin/concepts/variables/variables.md
dakshdeepHERE Jan 6, 2023
36081dc
Update content/kotlin/concepts/variables/variables.md
dakshdeepHERE Jan 6, 2023
b5a42f4
Update variables.md
dakshdeepHERE Jan 6, 2023
289286e
Merge branch 'main' of https://github.com/dakshdeepHERE/docs
dakshdeepHERE Jan 7, 2023
7b09c6f
Merge branch 'variables' of https://github.com/dakshdeepHERE/docs
dakshdeepHERE Jan 7, 2023
8152444
del
dakshdeepHERE Jan 7, 2023
cbd9b8f
# This is a combination of 17 commits.
dakshdeepHERE Jan 8, 2023
a8c53ab
Merge branch 'main' of https://github.com/dakshdeepHERE/docs
dakshdeepHERE Jan 9, 2023
3d6c895
added hashmap.md in kotlin folder
dakshdeepHERE Jan 9, 2023
8c031b3
did some changes
dakshdeepHERE Jan 9, 2023
5d5485d
added remove function of hashmap
dakshdeepHERE Jan 9, 2023
537b711
Merge branch 'main' into hashmap
SSwiniarski Jan 10, 2023
d4e59e3
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE Jan 10, 2023
4a46ec7
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE Jan 10, 2023
683bc4e
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE Jan 10, 2023
0490ebd
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE Jan 10, 2023
fa775c0
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE Jan 10, 2023
c1254e6
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE Jan 10, 2023
2e7f9d1
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE Jan 10, 2023
8a5c17a
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE Jan 10, 2023
e3cf718
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE Jan 10, 2023
9753de6
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE Jan 10, 2023
4d7be2d
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE Jan 10, 2023
af7aa34
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE Jan 10, 2023
b4c0d25
Merge branch 'main' into hashmap
SSwiniarski Jan 10, 2023
393c027
Merge branch 'main' of https://github.com/dakshdeepHERE/docs
dakshdeepHERE Jan 11, 2023
d1fd03b
Merge branch 'main' into hashmap
Dusch4593 Jan 17, 2023
b4acdbd
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE Jan 18, 2023
52b68be
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE Jan 18, 2023
48c648f
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE Jan 18, 2023
eeebfdc
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE Jan 18, 2023
384fbfb
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE Jan 18, 2023
4cd2bf9
Merge branch 'hashmap' of https://github.com/dakshdeepHERE/docs
dakshdeepHERE Jan 18, 2023
6bdb6ec
Merge branch 'Codecademy:main' into main
dakshdeepHERE Jan 18, 2023
b8be457
Merge branch 'Codecademy:main' into main
dakshdeepHERE Jan 21, 2023
67d26cb
Merge branch 'Codecademy:main' into main
dakshdeepHERE Jan 27, 2023
bdc7933
del
dakshdeepHERE Jan 27, 2023
a04b007
merged
dakshdeepHERE Jan 27, 2023
2bd17f4
Merge branch 'Codecademy:main' into main
dakshdeepHERE Apr 7, 2023
eec31fc
Merge branch 'Codecademy:main' into main
dakshdeepHERE Apr 26, 2023
cf39f8f
Merge branch 'Codecademy:main' into main
dakshdeepHERE Jun 9, 2023
afaddda
adding eluclidean algotrithm
dakshdeepHERE Jun 9, 2023
e133fcf
added euclidean algorith method 1
dakshdeepHERE Jun 10, 2023
fad1a2b
completed all 3 methods and time complexity
dakshdeepHERE Jun 10, 2023
54d0b09
changing the title numbers
dakshdeepHERE Jun 10, 2023
b094712
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 12, 2023
d40441a
Suggested changes
dakshdeepHERE Jun 12, 2023
f031bca
Update euclidean-algorithm.md
dakshdeepHERE Jun 12, 2023
97bb0ab
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 12, 2023
1ffe09d
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 12, 2023
3003ead
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 12, 2023
c697de1
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 12, 2023
3ee7153
Merge branch 'main' into algorithm1
dakshdeepHERE Jun 16, 2023
eaffa1c
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 19, 2023
dfa36e1
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 19, 2023
5b72a2b
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 19, 2023
41a88f6
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 19, 2023
939e8b9
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 19, 2023
79613a8
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 19, 2023
8f27ae0
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 19, 2023
5497a40
Pseudo changes
dakshdeepHERE Jun 19, 2023
7cbfaf9
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 19, 2023
d296763
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 19, 2023
3f9ed0e
Merge branch 'main' into algorithm1
dakshdeepHERE Jun 19, 2023
9c72eb8
Changing tags
dakshdeepHERE Jun 21, 2023
0c38379
Update euclidean-algorithm.md
dakshdeepHERE Jun 22, 2023
7f4a664
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
4bf925f
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
60f1535
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
9354012
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
25a57ff
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
032d2ee
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
de53909
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
90f696b
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
c43b997
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
9ac7886
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
eae1b09
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
cba4985
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
9224cd6
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
1e1b1cb
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
5f5d159
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
d5d4cde
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
c17d7b2
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
0f4b679
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
8e8e136
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
132dda9
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
3bb3560
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
f00142b
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
3b95e8e
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
ffc0104
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
1000185
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
70d347b
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
aed9a4b
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE Jun 23, 2023
f8ca014
review edits
caupolicandiaz Jun 23, 2023
30f973f
Merge branch 'main' into algorithm1
SSwiniarski Jun 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
---
Title: 'Euclidean Algorithm'
Description: 'A simple and efficient method for finding the highest common factor (HCF), also known as the greatest common divisor (GCD), of two numbers.'
Subjects:
- 'Computer Science'
Tags:
- 'Algorithms'
- 'Arithmetic'
CatalogContent:
- 'learn-java'
- 'paths/computer-science'
---

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.

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.

## Method 1

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.

The following code illustrates this method in Java:

```java
public static void main(String[] args) {
System.out.println(gcd(10,15));
}
public static int gcd(int a,int b){
int minValue = Math.min(a,b);
while(minValue>0){
if(a%minValue==0 && b%minValue==0){
break;
}
minValue--;
}
return minValue;
}
}
```

The output for the above code will be:

```shell
5
```

## Method 2: A Basic Euclidean Algorithm Approach

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.

The following code illustrates this method in Java:

```java
class Euclidean1 {
static int gcd(int a, int b) {

while (a != b) {
if (a > b)
a = a - b;
else
b = b - a;
}

return a;
}

public static void main(String[] args) {

int a = 15, b = 20;

System.out.println(gcd(a, b));
}
}
```

The output for the above code will be:

```shell
5
```

The step by step execution of the above code is as follows:

- Since `b` is greater than `a` (20 > 15), here `b` is replaced with `b - a`, which gives results in `b` = 20 - 15 = 5.
- Now `a` is 15 and `b` is 5.
- The result is `a` = 15 - 5 = 10 after replacing `a` with `a - b`.
- The values are now `a` = 10 and `b` = 5.
- The process continues, `a` is replaced with `a - b`, resulting in `a` = 10 - 5 = 5.
- Now `a` and `b` are both equal to 5.
- At this point, the while loop exits, and `a` is returned. Therefore, the HCF of 15 and 20 is 5.

## Method 3: A Recursive Euclidean Approach

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.

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:

```java
public class Euclidean2 {
public static void main(String[] args) {
System.out.println(EuclideanOptimized(150, 500));

}
static int EuclideanOptimized(int a, int b){
if(b==0){
return a;
}
return EuclideanOptimized(b,a%b);
}
}
```

The output for the above code will be:

```shell
50
```

The step by step execution of this example is as follows:

- 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.
- Then the code proceeds to return `EuclideanOptimized`, but with the arguments `(b, a % b)`.
- 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)`.
- 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).
- 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)`.
- 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.

## Time Complexities

- Method 1:

- 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))_.

- Method 2:

- 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))_.

- Method 3:

- 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.

To summarize, the time complexities of the three codes are as follows:

- Method 1: _O(min(a, b))_
- Method 2: _O(max(a, b))_
- Method 3: _O(log(max(a, b)))_

> **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.