-
Notifications
You must be signed in to change notification settings - Fork 4k
[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
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 31dca9a
added more info and a introduction paragraph for loops
dakshdeepHERE 30d2b99
Merge branch 'main' into loops
dakshdeepHERE fcdd14d
Added data-types.md in the kotlin folder
dakshdeepHERE 5bf7922
Update content/kotlin/concepts/loops/loops.md
dakshdeepHERE 162c7a9
Update content/kotlin/concepts/loops/loops.md
dakshdeepHERE 43b62df
Update content/kotlin/concepts/loops/loops.md
dakshdeepHERE 40eacf3
Update content/kotlin/concepts/loops/loops.md
dakshdeepHERE 48166f2
Update content/kotlin/concepts/loops/loops.md
dakshdeepHERE 22c708d
Update content/kotlin/concepts/loops/loops.md
dakshdeepHERE 5f03889
Update loops.md
dakshdeepHERE 885ea2a
Update loops.md
dakshdeepHERE d4b2ac2
Merge branch 'Codecademy:main' into loops
dakshdeepHERE 0736dd9
Update loops.md
dakshdeepHERE b42143f
changed the sequence of for while and do while loops
dakshdeepHERE f6fd28e
del
dakshdeepHERE 2e70242
Merge branch 'main' into data-types
dakshdeepHERE c219bc5
changed tags
dakshdeepHERE a0cec1e
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE 28c3525
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE 156cbaa
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE 9fbd5e6
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE 6a49a1c
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE ab6ebff
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE 2f1cc3c
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE 49b568f
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE 7e30fba
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE 970a372
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE 4974ee0
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE 37e863f
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE 8f211b0
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE cd47aaa
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE f24382f
Update content/kotlin/concepts/data-types/data-types.md
dakshdeepHERE ff95ae2
changed the sentence in line 278
dakshdeepHERE 9d8e67a
Update data-types.md
dakshdeepHERE 73e5ae9
added variables.md in kotlin folder
dakshdeepHERE 6a9d6b5
Merge branch 'main' into variables
SSwiniarski 6af23b8
Update content/kotlin/concepts/variables/variables.md
dakshdeepHERE 6381b7b
Update content/kotlin/concepts/variables/variables.md
dakshdeepHERE 664148f
Update content/kotlin/concepts/variables/variables.md
dakshdeepHERE 36081dc
Update content/kotlin/concepts/variables/variables.md
dakshdeepHERE b5a42f4
Update variables.md
dakshdeepHERE 289286e
Merge branch 'main' of https://github.com/dakshdeepHERE/docs
dakshdeepHERE 7b09c6f
Merge branch 'variables' of https://github.com/dakshdeepHERE/docs
dakshdeepHERE 8152444
del
dakshdeepHERE cbd9b8f
# This is a combination of 17 commits.
dakshdeepHERE a8c53ab
Merge branch 'main' of https://github.com/dakshdeepHERE/docs
dakshdeepHERE 3d6c895
added hashmap.md in kotlin folder
dakshdeepHERE 8c031b3
did some changes
dakshdeepHERE 5d5485d
added remove function of hashmap
dakshdeepHERE 537b711
Merge branch 'main' into hashmap
SSwiniarski d4e59e3
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE 4a46ec7
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE 683bc4e
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE 0490ebd
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE fa775c0
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE c1254e6
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE 2e7f9d1
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE 8a5c17a
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE e3cf718
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE 9753de6
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE 4d7be2d
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE af7aa34
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE b4c0d25
Merge branch 'main' into hashmap
SSwiniarski 393c027
Merge branch 'main' of https://github.com/dakshdeepHERE/docs
dakshdeepHERE d1fd03b
Merge branch 'main' into hashmap
Dusch4593 b4acdbd
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE 52b68be
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE 48c648f
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE eeebfdc
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE 384fbfb
Update content/kotlin/concepts/hashmap/hashmap.md
dakshdeepHERE 4cd2bf9
Merge branch 'hashmap' of https://github.com/dakshdeepHERE/docs
dakshdeepHERE 6bdb6ec
Merge branch 'Codecademy:main' into main
dakshdeepHERE b8be457
Merge branch 'Codecademy:main' into main
dakshdeepHERE 67d26cb
Merge branch 'Codecademy:main' into main
dakshdeepHERE bdc7933
del
dakshdeepHERE a04b007
merged
dakshdeepHERE 2bd17f4
Merge branch 'Codecademy:main' into main
dakshdeepHERE eec31fc
Merge branch 'Codecademy:main' into main
dakshdeepHERE cf39f8f
Merge branch 'Codecademy:main' into main
dakshdeepHERE afaddda
adding eluclidean algotrithm
dakshdeepHERE e133fcf
added euclidean algorith method 1
dakshdeepHERE fad1a2b
completed all 3 methods and time complexity
dakshdeepHERE 54d0b09
changing the title numbers
dakshdeepHERE b094712
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE d40441a
Suggested changes
dakshdeepHERE f031bca
Update euclidean-algorithm.md
dakshdeepHERE 97bb0ab
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 1ffe09d
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 3003ead
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE c697de1
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 3ee7153
Merge branch 'main' into algorithm1
dakshdeepHERE eaffa1c
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE dfa36e1
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 5b72a2b
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 41a88f6
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 939e8b9
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 79613a8
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 8f27ae0
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 5497a40
Pseudo changes
dakshdeepHERE 7cbfaf9
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE d296763
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 3f9ed0e
Merge branch 'main' into algorithm1
dakshdeepHERE 9c72eb8
Changing tags
dakshdeepHERE 0c38379
Update euclidean-algorithm.md
dakshdeepHERE 7f4a664
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 4bf925f
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 60f1535
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 9354012
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 25a57ff
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 032d2ee
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE de53909
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 90f696b
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE c43b997
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 9ac7886
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE eae1b09
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE cba4985
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 9224cd6
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 1e1b1cb
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 5f5d159
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE d5d4cde
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE c17d7b2
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 0f4b679
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 8e8e136
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 132dda9
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 3bb3560
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE f00142b
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 3b95e8e
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE ffc0104
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 1000185
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE 70d347b
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE aed9a4b
Update content/general/concepts/algorithm/terms/euclidean-algorithm/e…
dakshdeepHERE f8ca014
review edits
caupolicandiaz 30f973f
Merge branch 'main' into algorithm1
SSwiniarski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
148 changes: 148 additions & 0 deletions
148
...ent/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
dakshdeepHERE marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
dakshdeepHERE marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
dakshdeepHERE marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.