- 
                Notifications
    You must be signed in to change notification settings 
- Fork 4.3k
[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 92 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
        
          
          
            149 changes: 149 additions & 0 deletions
          
          149 
        
  ...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,149 @@ | ||
| --- | ||
| Title: 'Euclidean algorithm' | ||
| Description: 'The Euclidean algorithm is a simple and efficient method for finding the highest common factor (HCF), also known as the greatest common divisor (GCD), of two numbers.' | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| Subjects: | ||
| - 'Computer Science' | ||
| Tags: | ||
| - 'Algorithms' | ||
| - 'Hcf' | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| CatalogContent: | ||
| - 'paths/computer-science' | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| --- | ||
|  | ||
| The `Euclidean algorithm` is a recursive algorithm that allows developers to find the highest common factor (HCF) of two numbers. It is based on the observation that if two numbers are given, say A and B, where B divides A evenly (leaving no remainder), then B is the HCF of A and B. In other words, the larger number is divisible by the smaller number without any remainder. | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| There can be 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`. | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| ## 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 it by both numbers. If the number does not divide both numbers evenly, decrease it by `1` and continue dividing. Lastly, repeat this process until the minimum value can divide both numbers evenly. At this point, the minimum value is the HCF (highest common factor). | ||
|  | ||
| ```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 | ||
| ``` | ||
|  | ||
| ## Approach with Euclidean algorithm | ||
|  | ||
| Here two methods are shown using Euclidean algorithm to find the HCF of two numbers. So let's take two numbers as 'a' and 'b' as our inputs. | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| ## Method 2 | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| In this method, the aim is to compare both 'a' and 'b'. Whichever number is greater, the smaller number is subtracted from the larger number. It also updates the value of the larger number accordingly. If 'a' is greater than 'b', replace 'a' with 'a - b', and if 'b' is greater than 'a', replace 'b' with 'b - a'. Repeat this step until 'a' becomes equal to 'b'. Once 'a' and 'b' are equal, return 'a' as the answer. At this point, the value of 'a' (which is equal to 'b') represents the highest common factor (HCF) of the original values of 'a' and 'b'. | ||
|  | ||
| ```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)); | ||
|  | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| } | ||
| } | ||
| ``` | ||
|  | ||
| The output for the above code will be: | ||
|  | ||
| ```shell | ||
| 5 | ||
| ``` | ||
|  | ||
| When debugging the code above step by step, one can analyze the code as follows: | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| - Since 'b' is greater than 'a' (20 > 15), here 'b' is replaced with 'b - a', which gives us 'b = 20 - 15 = 5'. | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| - 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 is continued as 'a' is replaced with 'a - b', resulting in 'a = 10 - 5 = 5'. | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| - Now 'a' and 'b' are both equal to 5. | ||
| - At this point, the while loop exits, and we return 'a' as our answer, which is 5. | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| - Therefore, the highest common factor (HCF) of (15, 20) is 5. | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| ## Method 3 | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| 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 takes 'a' and 'b' as integer parameters and returns an integer as the result. The GCD method starts 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' eventually becomes 0, triggering the base case and resulting in the discovery of the GCD. | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| For more information on recursion, refer to this [resource](https://www.codecademy.com/learn/java-algorithms/modules/recursion-apcs/cheatsheet). | ||
|  | ||
| ```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 | ||
| ``` | ||
|  | ||
| Let's debug the code above step by steps: | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| - Given the input of two integers: `a = 150` and `b = 500`, the code proceeds to enter 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. | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| - Then the code proceeds to return `EuclideanOptimized`, but with the arguments changed to `(b, a % b)`. | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| - 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 exits the `if` statement. The next recursive cycle will use the arguments `(150, 50)`, which are obtained by calculating the modulus of `500 % 150`. | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| - Once again, the function is initiated with arguments `(150, 50)`. Upon entering the function, the `if` statement is encountered. Similarly to previous iterations, `b` is not equal to 0, and thus the program exits the `if` statement. The new arguments for the next recursive cycle are determined by calculating the modulus of `150 % 50`, resulting in `(50, 0)`. | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| - In this recursive cycle, when the code enters the `if` statement, the condition `b == 0` is satisfied. Therefore, the function will return `a`, and the highest common factor (HCF) for the input will be 50. | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| ## Time complexities | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| - Method 1: | ||
|  | ||
| - The first code 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 1, checking if it divides both `a` and `b`. Therefore, the time complexity of this code is O(min(a, b)). | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| - Method 2: | ||
|  | ||
| - The second code 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)). | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| - Method 3: | ||
|  | ||
| - The third code 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` becomes 0. The time complexity of this optimized Euclidean algorithm is also O(log(max(a, b))) since the algorithm reduces the values quickly by taking the modulus. | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| To summarize, the time complexities of the three codes are as follows: | ||
|  | ||
| - Code 1: O(min(a, b)) | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| - Code 2: O(max(a, b)) | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| - Code 3: O(log(max(a, b))) | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| 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. | ||
|         
                  dakshdeepHERE marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
  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.