From e7014e0025e616100ccc5dd76e92e924b592a226 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Sun, 1 Jan 2023 23:15:30 +0530 Subject: [PATCH 001/111] added loops.md in kotlin folder --- content/kotlin/concepts/loops/loops.md | 102 +++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 content/kotlin/concepts/loops/loops.md diff --git a/content/kotlin/concepts/loops/loops.md b/content/kotlin/concepts/loops/loops.md new file mode 100644 index 00000000000..8f700f42a5d --- /dev/null +++ b/content/kotlin/concepts/loops/loops.md @@ -0,0 +1,102 @@ +--- +Title: 'Loops' +Description: 'In kotlin, a loop is a control structure that allows you to repeat a block of code a specified number of times or until a certain condition is met. Loops are useful for automating repetitive tasks and for processing large amounts of data efficiently.' +Subjects: + - 'Computer Science' +Tags: + - 'loops' + - 'while' + - 'for' + - 'do-while' +CatalogContent: + - 'learn-kotlin' + - 'paths/computer-science' +--- + +## While loop + +The `while` loops used to execute a block of code repeatedly as long as a certain condition is true. The syntax for a `while` loop in Kotlin is: + +```kotlin + fun main(){ + while(condition){ + //code to be execute while the condition is true + } + } +``` + +Here's an example of a `while` loop in Kotlin that prints the numbers from 1 to 10: + +```kotlin + fun main(){ + var i = 1 + while (i <= 10) { + print(" " + i) + i++ + } +} +``` + +The output for the above code will be: + +``` +1 2 3 4 5 6 7 8 9 10 +``` + +## For loop + +The `for` loop is used to iterate over a range of values or an iterable collection. The syntax for a `for` loop in Kotlin is: + +```kotlin +fun main(){ + for (item in collection) { + // code to be executed for each item + } +} +``` + +Here's an example of a `for` loop in Kotlin that prints the numbers from 1 to 10: + +```kotlin +fun main(){ + for(i in 1..10){ + print(i) + } +} +``` + +The output for the above code will be: + +``` +1 2 3 4 5 6 7 8 9 10 +``` + +## Do-While loop + +A `do-while` loop in Kotlin is similar to a `while` loop, but the block of code is always executed at least once before the condition is checked. The syntax for a `do-while` loop is: + +```kotlin +fun main(){ + do { + // code to be executed at least once + } while (condition) +} +``` + +Here's an example of a `do-while` loop in Kotlin that prompts the user to enter a number and prints the decreasing order sequence till 0: + +```kotlin +fun main(){ + var i = 10 + do { + print(" " + i) + i-- + } while (i > 0) +} +``` + +The output for the above code will be: + +``` +10 9 8 7 6 5 4 3 2 1 +``` From 31dca9aeb2fae1d4df295e4d14a3afe0dab9d46d Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Sun, 1 Jan 2023 23:27:42 +0530 Subject: [PATCH 002/111] added more info and a introduction paragraph for loops --- content/kotlin/concepts/loops/loops.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/content/kotlin/concepts/loops/loops.md b/content/kotlin/concepts/loops/loops.md index 8f700f42a5d..18962f5956c 100644 --- a/content/kotlin/concepts/loops/loops.md +++ b/content/kotlin/concepts/loops/loops.md @@ -4,7 +4,7 @@ Description: 'In kotlin, a loop is a control structure that allows you to repeat Subjects: - 'Computer Science' Tags: - - 'loops' + - 'loop' - 'while' - 'for' - 'do-while' @@ -13,6 +13,12 @@ CatalogContent: - 'paths/computer-science' --- +In Kotlin, a loop is a control flow statement that allows you to repeat a block of code a certain number of times or until a certain condition is met. Kotlin provides several types of loops, including: + +- `for` loop: This loop iterates over a range of values or elements in a collection. +- `while` loop: This loop repeatedly executes a block of code as long as a certain condition is true. +- `do-while` loop: This loop is similar to a while loop, but it guarantees that the block of code will be executed at least once before the condition is checked. + ## While loop The `while` loops used to execute a block of code repeatedly as long as a certain condition is true. The syntax for a `while` loop in Kotlin is: From fcdd14d89fda1f5ccf77ebb77be6fe9bdc571992 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 2 Jan 2023 15:30:38 +0530 Subject: [PATCH 003/111] Added data-types.md in the kotlin folder --- .../kotlin/concepts/data-types/data-types.md | 378 ++++++++++++++++++ 1 file changed, 378 insertions(+) create mode 100644 content/kotlin/concepts/data-types/data-types.md diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md new file mode 100644 index 00000000000..4b75c551196 --- /dev/null +++ b/content/kotlin/concepts/data-types/data-types.md @@ -0,0 +1,378 @@ +--- +Title: 'Data Types' +Description: 'Data types are a classification of types of values that can be stored and manipulated in a program.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Primitive Data-Type' + - 'Data Types' + - 'Non Primitive Data-Type' +CatalogContent: + - 'learn-Kotlin' + - 'paths/computer-science' +--- + +`Data types` are a classification of types of data that determine the possible values and operations that can be performed on that data. + +Kotlin has both `primitive` and `non-primitive` data types. + +## Primitive Data-type + +There are the following different types of primitive data types in Kotlin: + +- Boolean +- Byte +- Char +- Double +- Float +- Int +- Long +- Short + +### Boolean + +In Kotlin, the `Boolean` is a primitive data type and can hold only one of the two possible values: `true` or `false`. + +Here is an example of how to declare a `Boolean` variable in Kotlin: + +```kotlin +val isTrue: Boolean = true +val isFalse: Boolean = false +// here we can see that if a variable is wrong we can written false and true if its right +``` + +The `Boolean` data type can be used to determine the outcome of `if ... then` statements: + +```kotlin +fun main(){ + val condition: Boolean = true + + if (condition) { + // write a code that you want to execute if the variable is given as true + } else { + // write a code for the false variable + } +} +``` + +### Byte + +A `Byte` is a data type that is similar to an integer data type, but it is only 8 bits in size, whereas an integer can be a different size depending on the system. A byte can hold values from -128 to 127. + +A `Byte` is a way to represent a small whole number. It is commonly used to store small pieces of data, such as characters in a text file or colors in an image. + +Here is an example of how to declare a `Byte` variable in Kotlin: + +```kotlin +fun main(){ + val a: Byte = 50 + val b: Byte = 25 + val c: Byte = a + b as Byte + print(c) + +} +``` + +The output will be: + +``` +75 +``` + +The range of the `Byte` data type is -128 to 127. Calculations that exceed this range will produce unexpected results: + +```kotlin +fun main(){ + val a: Byte = 120 + val b: Byte = 120 + val c: Byte = a + b as Byte + print(c) +} +``` + +Here the output will be: + +``` +-88 +``` + +In the above example, the value of c will be -88, because the result of 120 + 120 is 240, which exceeds the valid range for a Byte type. + +### Char + +In Kotlin, the `Char` data type represents a primitive data type that can hold any character from the Unicode character set, which includes letters, digits, symbols, and special characters. + +Here is an example of how to declare a `Char` variable in Kotlin: + +```kotlin +fun main(){ + val ch: Char = 'A' + print(ch) +} +``` + +The output will be: + +``` +A +``` + +### Double + +In Kotlin, the `Double` data type represents a 64-bit floating-point number. It is a primitive data type that can hold values with a fractional component, such as 3.14 or 0.5. + +Here is an example of how to declare a `Double` variable in Kotlin: + +```kotlin +fun main(){ + val a: Double = 3.14 + val b: Double = 2.71 + val c: Double = a * b as Double + print(c) +} +``` + +The output of the above code will be: + +``` +8.5094 +``` + +In this example, the c variable will be assigned the value 8.5014, which is the result of 3.14 \* 2.71. + +The `Double` data type can be used to perform more advanced operations, such as calculating the square root of a number or raising a number to a power in Kotlin. For example: + +```kotlin + +import kotlin.math.sqrt +import kotlin.math.pow +fun main(){ + + +val a: Double = 16.0 +val b: Double = sqrt(a) +val c: Double = a.pow(2) + +println("The square root of $a is $b.") +println("$a raised to the power of 2 is $c.") + +} +``` + +The output will be: + +``` +The square root of 16.0 is 4.0. +16.0 raised to the power of 2 is 256.0. +``` + +### Float + +In Kotlin, the `Float` data type represents a 32-bit floating-point number. It is a primitive data type that can hold values with a fractional component, such as 3.14 or 0.5. + +Here is an example of how to declare a Float variable in Kotlin: + +```kotlin +fun main(){ + val a: Float = 3.14f + val b: Float = 2.71f + val c: Float = a * b as Float + print(c) + +} +``` + +The output will be : + +``` +8.5094 +``` + +In this example, the c variable will be assigned the value 8.5014, which is the result of 3.14 \* 2.71. + +### Int + +In Kotlin, the `Int` data type represents a 32-bit signed integer. It is a primitive data type that can hold values ranging from -2147483648 to 2147483647. + +Here is an example of how to declare an `Int` variable in Kotlin: + +```kotlin +fun main(){ + val a: Int = 50 + val b: Int = 25 + val c: Int = a + b + print(c) +} +``` + +The output of the above code will be: + +``` +75 +``` + +### Long + +In Kotlin, the `Long` data type represents a 64-bit signed integer. It is a primitive data type that can hold values ranging from -9223372036854775808 to 9223372036854775807. + +Here is an example of how to declare a `Long` variable in Kotlin: + +```kotlin +fun main(){ + val a: Long = 500000000 + val b: Long = 250000000 + val c: Long = a + b + print(c) +} +``` + +The output for the above code will be : + +``` +750000000 +``` + +In this example, the c variable will be assigned the value 750000000, which is the result of 500000000 + 250000000. + +### Short + +In Kotlin, the `Short` data type represents a 16-bit signed integer. It is a primitive data type that can hold values ranging from -32768 to 32767. + +Here is an example of how to declare a `Short` variable in Kotlin: + +```kotlin +fun main(){ + val a: Short = 50 + val b: Short = 25 + val c: Short = (a + b).toShort() + print(c) +} +``` + +The output for the above code will be: + +``` +75 +``` + +In this example, the c variable will be assigned the value 75, which is the result of 50 + 25. The `toShort()` function is used to explicitly convert the result to a `Short` type. + +## Non-Primitive Data Types + +The following are non-primitive data types in Kotlin : + +- Arrays +- String +- Any +- Nothing +- Unit + +### Arrays + +In Kotlin, an `Array` is a data structure that stores a fixed-size collection of elements of the same data type. Arrays are non-primitive data types, which means they are derived from primitive data types or other non-primitive data types. + +To declare an `Array` in Kotlin, you can use the `arrayOf()` function and specify the elements of the array within the parentheses. For example: + +```kotlin +fun main(){ + val numbers: Array = arrayOf(1, 2, 3, 4, 5) +} +``` + +In this example, the `numbers` variable is an array of integers that contains the values 1, 2, 3, 4, and 5. The type of the array is specified using the `Array` syntax, which indicates that the array contains elements of the `Int` type. + +If you want to know how to print the array then you can check [Arrays]. + +### String + +In Kotlin, a `String` is a data type that represents a sequence of characters. You can use a string to store and manipulate text in your program. + +Here is an example of how to declare and initialize a string in Kotlin: + +```kotlin +fun main(){ + val message: String = "Hello, World!" + print(message) +} +``` + +The output for the above code will be : + +``` +Hello, World! +``` + +Strings are also [immutable](https://kotlinlang.org/docs/native-immutability.html) in Kotlin. + +You can use the '+' operator to concatenate strings, or the trim() function to remove leading and trailing whitespace from a string. For example: + +```kotlin +fun main(){ + val greeting: String = "Hello, " + val name: String = "John" + val fullMessage: String = greeting + name + println(fullMessage.trim()) +} +``` + +The output for the above code will be : + +``` +Hello, John +``` + +### Any + +In Kotlin, the `Any` type is a supertype of all types in the language. It represents a general type that can hold any value. + +Here is an example of how to declare a variable of type `Any` in Kotlin: + +```kotlin +fun main(){ + val value: Any = 100 + val value2: Any = "Hello, World!" + println(value) + println(value2) +} +``` + +The output for the above code will be : + +``` +100 +Hello, World! +``` + +### Nothing + +In Kotlin, the `Nothing` type is a special type that represents the absence of a value. It is a subtype of all types in the language and cannot be instantiated. + +The `Nothing` type is used to indicate that a function never returns a value. For example, a function that throws an exception or terminates the program will have a return type of `Nothing`. + +Here is an example of a function that has a return type of `Nothing`: + +```kotlin +fun error(): Nothing { + throw IllegalStateException("An error occurred.") +} +``` + +In the above, the `error()` function throws an exception and does not return a value. Therefore, its return type is `Nothing`. + +### Unit + +In Kotlin, the `Unit` type represents the absence of a value, similar to the `void` type in other programming languages. It is a special type that is used to indicate that a function does not return a value. + +The `Unit` type can be used to specify that a function has a return type of `Unit` when it is not possible to infer the return type from the function's body. For example: + +```kotlin +fun main(){ + +} +fun doSomething(): Unit { + // perform some action that you want to do +} +``` + +In the above example, the doSomething() function performs an action and does not return a value. Therefore, its return type is `Unit`. From 5bf7922ca9d38a8790d48960ec3e8edc1de6b758 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 2 Jan 2023 21:45:12 +0530 Subject: [PATCH 004/111] Update content/kotlin/concepts/loops/loops.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/loops/loops.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/loops/loops.md b/content/kotlin/concepts/loops/loops.md index 18962f5956c..6f2e28c62a4 100644 --- a/content/kotlin/concepts/loops/loops.md +++ b/content/kotlin/concepts/loops/loops.md @@ -1,6 +1,6 @@ --- Title: 'Loops' -Description: 'In kotlin, a loop is a control structure that allows you to repeat a block of code a specified number of times or until a certain condition is met. Loops are useful for automating repetitive tasks and for processing large amounts of data efficiently.' +Description: 'A loop is a control structure that allows you to repeat a block of code a specified number of times or until a certain condition is met.' Subjects: - 'Computer Science' Tags: From 162c7a9726d85eaf96bc529efd297e6dc39a9fb3 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 2 Jan 2023 21:45:24 +0530 Subject: [PATCH 005/111] Update content/kotlin/concepts/loops/loops.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/loops/loops.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/loops/loops.md b/content/kotlin/concepts/loops/loops.md index 6f2e28c62a4..b7c7f03f56e 100644 --- a/content/kotlin/concepts/loops/loops.md +++ b/content/kotlin/concepts/loops/loops.md @@ -4,7 +4,7 @@ Description: 'A loop is a control structure that allows you to repeat a block of Subjects: - 'Computer Science' Tags: - - 'loop' + - 'Loops' - 'while' - 'for' - 'do-while' From 43b62dfa5d7244c63c9ef0e64ad20f7a1a6c440b Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 2 Jan 2023 21:45:30 +0530 Subject: [PATCH 006/111] Update content/kotlin/concepts/loops/loops.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/loops/loops.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/loops/loops.md b/content/kotlin/concepts/loops/loops.md index b7c7f03f56e..09c677f1e2a 100644 --- a/content/kotlin/concepts/loops/loops.md +++ b/content/kotlin/concepts/loops/loops.md @@ -5,7 +5,7 @@ Subjects: - 'Computer Science' Tags: - 'Loops' - - 'while' + - 'While' - 'for' - 'do-while' CatalogContent: From 40eacf3f284e26ce40f9f3b09b50c94b0f73caad Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 2 Jan 2023 21:45:36 +0530 Subject: [PATCH 007/111] Update content/kotlin/concepts/loops/loops.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/loops/loops.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/loops/loops.md b/content/kotlin/concepts/loops/loops.md index 09c677f1e2a..07edc4a3ac6 100644 --- a/content/kotlin/concepts/loops/loops.md +++ b/content/kotlin/concepts/loops/loops.md @@ -6,7 +6,7 @@ Subjects: Tags: - 'Loops' - 'While' - - 'for' + - 'For' - 'do-while' CatalogContent: - 'learn-kotlin' From 48166f2c34a8ba08c6f6fc10ae157446ce739c33 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 2 Jan 2023 21:45:42 +0530 Subject: [PATCH 008/111] Update content/kotlin/concepts/loops/loops.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/loops/loops.md | 1 - 1 file changed, 1 deletion(-) diff --git a/content/kotlin/concepts/loops/loops.md b/content/kotlin/concepts/loops/loops.md index 07edc4a3ac6..21756c5f410 100644 --- a/content/kotlin/concepts/loops/loops.md +++ b/content/kotlin/concepts/loops/loops.md @@ -7,7 +7,6 @@ Tags: - 'Loops' - 'While' - 'For' - - 'do-while' CatalogContent: - 'learn-kotlin' - 'paths/computer-science' From 22c708d4035f5cae18924398b6b89a43f7eaddc6 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 2 Jan 2023 21:45:51 +0530 Subject: [PATCH 009/111] Update content/kotlin/concepts/loops/loops.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/loops/loops.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/loops/loops.md b/content/kotlin/concepts/loops/loops.md index 21756c5f410..8f32c82cef2 100644 --- a/content/kotlin/concepts/loops/loops.md +++ b/content/kotlin/concepts/loops/loops.md @@ -20,7 +20,7 @@ In Kotlin, a loop is a control flow statement that allows you to repeat a block ## While loop -The `while` loops used to execute a block of code repeatedly as long as a certain condition is true. The syntax for a `while` loop in Kotlin is: +The `while` loop is used to execute a block of code repeatedly as long as a certain condition is true. The syntax for a `while` loop in Kotlin is: ```kotlin fun main(){ From 5f03889c84480e91e5c7efe8469ac15c5578d034 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 2 Jan 2023 21:47:42 +0530 Subject: [PATCH 010/111] Update loops.md --- content/kotlin/concepts/loops/loops.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/kotlin/concepts/loops/loops.md b/content/kotlin/concepts/loops/loops.md index 8f32c82cef2..877802b7360 100644 --- a/content/kotlin/concepts/loops/loops.md +++ b/content/kotlin/concepts/loops/loops.md @@ -22,7 +22,7 @@ In Kotlin, a loop is a control flow statement that allows you to repeat a block The `while` loop is used to execute a block of code repeatedly as long as a certain condition is true. The syntax for a `while` loop in Kotlin is: -```kotlin +```pseudo fun main(){ while(condition){ //code to be execute while the condition is true @@ -52,7 +52,7 @@ The output for the above code will be: The `for` loop is used to iterate over a range of values or an iterable collection. The syntax for a `for` loop in Kotlin is: -```kotlin +```pseudo fun main(){ for (item in collection) { // code to be executed for each item @@ -80,7 +80,7 @@ The output for the above code will be: A `do-while` loop in Kotlin is similar to a `while` loop, but the block of code is always executed at least once before the condition is checked. The syntax for a `do-while` loop is: -```kotlin +```pseudo fun main(){ do { // code to be executed at least once From 885ea2a50c505f07eae7225da923d8d67b74cbea Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 2 Jan 2023 21:50:07 +0530 Subject: [PATCH 011/111] Update loops.md --- content/kotlin/concepts/loops/loops.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/loops/loops.md b/content/kotlin/concepts/loops/loops.md index 877802b7360..b46402ac22d 100644 --- a/content/kotlin/concepts/loops/loops.md +++ b/content/kotlin/concepts/loops/loops.md @@ -14,8 +14,8 @@ CatalogContent: In Kotlin, a loop is a control flow statement that allows you to repeat a block of code a certain number of times or until a certain condition is met. Kotlin provides several types of loops, including: -- `for` loop: This loop iterates over a range of values or elements in a collection. - `while` loop: This loop repeatedly executes a block of code as long as a certain condition is true. +- `for` loop: This loop iterates over a range of values or elements in a collection. - `do-while` loop: This loop is similar to a while loop, but it guarantees that the block of code will be executed at least once before the condition is checked. ## While loop From 0736dd9b5cb05479c628dea784a601767a54a0a7 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 2 Jan 2023 22:14:25 +0530 Subject: [PATCH 012/111] Update loops.md --- content/kotlin/concepts/loops/loops.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/loops/loops.md b/content/kotlin/concepts/loops/loops.md index b46402ac22d..3fcaf8b4bc4 100644 --- a/content/kotlin/concepts/loops/loops.md +++ b/content/kotlin/concepts/loops/loops.md @@ -88,7 +88,7 @@ fun main(){ } ``` -Here's an example of a `do-while` loop in Kotlin that prompts the user to enter a number and prints the decreasing order sequence till 0: +Here's an example of a `do-while` loop in Kotlin here the code declares a variable i with the initial value of 10. It then enters a loop, which will continue to run as long as i is greater than 0. Inside the loop, the code prints the value of i, and then decrements i by 1. This means that on each iteration of the loop, the value of i will be printed, and then reduced by 1. The loop will continue to run until i is no longer greater than 0, at which point the loop will exit and the program will end: ```kotlin fun main(){ From b42143fda3a5a0037422fed1c380038ca4f88b06 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 2 Jan 2023 22:31:50 +0530 Subject: [PATCH 013/111] changed the sequence of for while and do while loops --- content/kotlin/concepts/loops/loops.md | 46 +++++++++++++------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/content/kotlin/concepts/loops/loops.md b/content/kotlin/concepts/loops/loops.md index 3fcaf8b4bc4..23c8aa464c4 100644 --- a/content/kotlin/concepts/loops/loops.md +++ b/content/kotlin/concepts/loops/loops.md @@ -14,30 +14,28 @@ CatalogContent: In Kotlin, a loop is a control flow statement that allows you to repeat a block of code a certain number of times or until a certain condition is met. Kotlin provides several types of loops, including: -- `while` loop: This loop repeatedly executes a block of code as long as a certain condition is true. - `for` loop: This loop iterates over a range of values or elements in a collection. +- `while` loop: This loop repeatedly executes a block of code as long as a certain condition is true. - `do-while` loop: This loop is similar to a while loop, but it guarantees that the block of code will be executed at least once before the condition is checked. -## While loop +## For loop -The `while` loop is used to execute a block of code repeatedly as long as a certain condition is true. The syntax for a `while` loop in Kotlin is: +The `for` loop is used to iterate over a range of values or an iterable collection. The syntax for a `for` loop in Kotlin is: ```pseudo - fun main(){ - while(condition){ - //code to be execute while the condition is true - } +fun main(){ + for (item in collection) { + // code to be executed for each item } +} ``` -Here's an example of a `while` loop in Kotlin that prints the numbers from 1 to 10: +Here's an example of a `for` loop in Kotlin that prints the numbers from 1 to 10: ```kotlin - fun main(){ - var i = 1 - while (i <= 10) { - print(" " + i) - i++ +fun main(){ + for(i in 1..10){ + print(i) } } ``` @@ -48,24 +46,26 @@ The output for the above code will be: 1 2 3 4 5 6 7 8 9 10 ``` -## For loop +## While loop -The `for` loop is used to iterate over a range of values or an iterable collection. The syntax for a `for` loop in Kotlin is: +The `while` loop is used to execute a block of code repeatedly as long as a certain condition is true. The syntax for a `while` loop in Kotlin is: ```pseudo -fun main(){ - for (item in collection) { - // code to be executed for each item + fun main(){ + while(condition){ + //code to be execute while the condition is true + } } -} ``` -Here's an example of a `for` loop in Kotlin that prints the numbers from 1 to 10: +Here's an example of a `while` loop in Kotlin that prints the numbers from 1 to 10: ```kotlin -fun main(){ - for(i in 1..10){ - print(i) + fun main(){ + var i = 1 + while (i <= 10) { + print(" " + i) + i++ } } ``` From f6fd28e0c8036dce677bab01502dddac39eded78 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 2 Jan 2023 22:37:17 +0530 Subject: [PATCH 014/111] del --- content/kotlin/concepts/loops/loops.md | 107 ------------------------- 1 file changed, 107 deletions(-) delete mode 100644 content/kotlin/concepts/loops/loops.md diff --git a/content/kotlin/concepts/loops/loops.md b/content/kotlin/concepts/loops/loops.md deleted file mode 100644 index 23c8aa464c4..00000000000 --- a/content/kotlin/concepts/loops/loops.md +++ /dev/null @@ -1,107 +0,0 @@ ---- -Title: 'Loops' -Description: 'A loop is a control structure that allows you to repeat a block of code a specified number of times or until a certain condition is met.' -Subjects: - - 'Computer Science' -Tags: - - 'Loops' - - 'While' - - 'For' -CatalogContent: - - 'learn-kotlin' - - 'paths/computer-science' ---- - -In Kotlin, a loop is a control flow statement that allows you to repeat a block of code a certain number of times or until a certain condition is met. Kotlin provides several types of loops, including: - -- `for` loop: This loop iterates over a range of values or elements in a collection. -- `while` loop: This loop repeatedly executes a block of code as long as a certain condition is true. -- `do-while` loop: This loop is similar to a while loop, but it guarantees that the block of code will be executed at least once before the condition is checked. - -## For loop - -The `for` loop is used to iterate over a range of values or an iterable collection. The syntax for a `for` loop in Kotlin is: - -```pseudo -fun main(){ - for (item in collection) { - // code to be executed for each item - } -} -``` - -Here's an example of a `for` loop in Kotlin that prints the numbers from 1 to 10: - -```kotlin -fun main(){ - for(i in 1..10){ - print(i) - } -} -``` - -The output for the above code will be: - -``` -1 2 3 4 5 6 7 8 9 10 -``` - -## While loop - -The `while` loop is used to execute a block of code repeatedly as long as a certain condition is true. The syntax for a `while` loop in Kotlin is: - -```pseudo - fun main(){ - while(condition){ - //code to be execute while the condition is true - } - } -``` - -Here's an example of a `while` loop in Kotlin that prints the numbers from 1 to 10: - -```kotlin - fun main(){ - var i = 1 - while (i <= 10) { - print(" " + i) - i++ - } -} -``` - -The output for the above code will be: - -``` -1 2 3 4 5 6 7 8 9 10 -``` - -## Do-While loop - -A `do-while` loop in Kotlin is similar to a `while` loop, but the block of code is always executed at least once before the condition is checked. The syntax for a `do-while` loop is: - -```pseudo -fun main(){ - do { - // code to be executed at least once - } while (condition) -} -``` - -Here's an example of a `do-while` loop in Kotlin here the code declares a variable i with the initial value of 10. It then enters a loop, which will continue to run as long as i is greater than 0. Inside the loop, the code prints the value of i, and then decrements i by 1. This means that on each iteration of the loop, the value of i will be printed, and then reduced by 1. The loop will continue to run until i is no longer greater than 0, at which point the loop will exit and the program will end: - -```kotlin -fun main(){ - var i = 10 - do { - print(" " + i) - i-- - } while (i > 0) -} -``` - -The output for the above code will be: - -``` -10 9 8 7 6 5 4 3 2 1 -``` From c219bc5fbd2e7ac4020dd62232a47b607807104b Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 13:03:36 +0530 Subject: [PATCH 015/111] changed tags --- content/kotlin/concepts/data-types/data-types.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md index 4b75c551196..5dc51c20afd 100644 --- a/content/kotlin/concepts/data-types/data-types.md +++ b/content/kotlin/concepts/data-types/data-types.md @@ -5,9 +5,10 @@ Subjects: - 'Code Foundations' - 'Computer Science' Tags: - - 'Primitive Data-Type' + - 'Integers' - 'Data Types' - - 'Non Primitive Data-Type' + - 'Boolean' + - 'Characters' CatalogContent: - 'learn-Kotlin' - 'paths/computer-science' From a0cec1ede585280cb3a66d8bbb1dbeed7d6b064e Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 13:04:01 +0530 Subject: [PATCH 016/111] Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <72277593+yangc95@users.noreply.github.com> --- content/kotlin/concepts/data-types/data-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md index 5dc51c20afd..9459975871a 100644 --- a/content/kotlin/concepts/data-types/data-types.md +++ b/content/kotlin/concepts/data-types/data-types.md @@ -14,7 +14,7 @@ CatalogContent: - 'paths/computer-science' --- -`Data types` are a classification of types of data that determine the possible values and operations that can be performed on that data. +**Data types** are a classification of types of data that determine possible values and operations that can be performed on that data. Kotlin has both `primitive` and `non-primitive` data types. From 28c3525fffb4e6d3e54d98524dc41652e1100019 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 13:04:07 +0530 Subject: [PATCH 017/111] Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <72277593+yangc95@users.noreply.github.com> --- content/kotlin/concepts/data-types/data-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md index 9459975871a..57e7882abe8 100644 --- a/content/kotlin/concepts/data-types/data-types.md +++ b/content/kotlin/concepts/data-types/data-types.md @@ -18,7 +18,7 @@ CatalogContent: Kotlin has both `primitive` and `non-primitive` data types. -## Primitive Data-type +## Primitive Data Types There are the following different types of primitive data types in Kotlin: From 156cbaa3887490f8b536fa3ba383ecc113357029 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 13:04:15 +0530 Subject: [PATCH 018/111] Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <72277593+yangc95@users.noreply.github.com> --- content/kotlin/concepts/data-types/data-types.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md index 57e7882abe8..31031002c6f 100644 --- a/content/kotlin/concepts/data-types/data-types.md +++ b/content/kotlin/concepts/data-types/data-types.md @@ -33,9 +33,7 @@ There are the following different types of primitive data types in Kotlin: ### Boolean -In Kotlin, the `Boolean` is a primitive data type and can hold only one of the two possible values: `true` or `false`. - -Here is an example of how to declare a `Boolean` variable in Kotlin: +In Kotlin, `Boolean` can hold only one of the two possible values: `true` or `false`. Below is an example of how to declare a `Boolean` variable in Kotlin: ```kotlin val isTrue: Boolean = true From 9fbd5e609b8ee7aacd016a9b8b415ec483ffede3 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 13:04:22 +0530 Subject: [PATCH 019/111] Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <72277593+yangc95@users.noreply.github.com> --- content/kotlin/concepts/data-types/data-types.md | 1 - 1 file changed, 1 deletion(-) diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md index 31031002c6f..b0298bcebed 100644 --- a/content/kotlin/concepts/data-types/data-types.md +++ b/content/kotlin/concepts/data-types/data-types.md @@ -38,7 +38,6 @@ In Kotlin, `Boolean` can hold only one of the two possible values: `true` or `fa ```kotlin val isTrue: Boolean = true val isFalse: Boolean = false -// here we can see that if a variable is wrong we can written false and true if its right ``` The `Boolean` data type can be used to determine the outcome of `if ... then` statements: From 6a49a1c65258cb8252f065d83436075f776a6b0f Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 13:04:31 +0530 Subject: [PATCH 020/111] Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <72277593+yangc95@users.noreply.github.com> --- content/kotlin/concepts/data-types/data-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md index b0298bcebed..b756f116be7 100644 --- a/content/kotlin/concepts/data-types/data-types.md +++ b/content/kotlin/concepts/data-types/data-types.md @@ -40,7 +40,7 @@ val isTrue: Boolean = true val isFalse: Boolean = false ``` -The `Boolean` data type can be used to determine the outcome of `if ... then` statements: +The `Boolean` data type can be used to determine the outcome of `if...else` statements: ```kotlin fun main(){ From ab6ebff6858186641c4224c2ff9025280f53a5c9 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 13:05:32 +0530 Subject: [PATCH 021/111] Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <72277593+yangc95@users.noreply.github.com> --- content/kotlin/concepts/data-types/data-types.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md index b756f116be7..cc2afc2f324 100644 --- a/content/kotlin/concepts/data-types/data-types.md +++ b/content/kotlin/concepts/data-types/data-types.md @@ -260,10 +260,15 @@ In this example, the c variable will be assigned the value 75, which is the resu The following are non-primitive data types in Kotlin : -- Arrays -- String - Any +- Arrays +- Class +- Enum +- List +- Map - Nothing +- Set +- String - Unit ### Arrays From 2f1cc3c6d8a5a2be2c1f342d8f5696df852df55b Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 13:06:05 +0530 Subject: [PATCH 022/111] Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <72277593+yangc95@users.noreply.github.com> --- content/kotlin/concepts/data-types/data-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md index cc2afc2f324..34ede4d4647 100644 --- a/content/kotlin/concepts/data-types/data-types.md +++ b/content/kotlin/concepts/data-types/data-types.md @@ -278,7 +278,7 @@ In Kotlin, an `Array` is a data structure that stores a fixed-size collection of To declare an `Array` in Kotlin, you can use the `arrayOf()` function and specify the elements of the array within the parentheses. For example: ```kotlin -fun main(){ +fun main() { val numbers: Array = arrayOf(1, 2, 3, 4, 5) } ``` From 49b568f6de551b883783c467afb6f7255ef00b3d Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 13:06:14 +0530 Subject: [PATCH 023/111] Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <72277593+yangc95@users.noreply.github.com> --- content/kotlin/concepts/data-types/data-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md index 34ede4d4647..0b9a2e4e7aa 100644 --- a/content/kotlin/concepts/data-types/data-types.md +++ b/content/kotlin/concepts/data-types/data-types.md @@ -294,7 +294,7 @@ In Kotlin, a `String` is a data type that represents a sequence of characters. Y Here is an example of how to declare and initialize a string in Kotlin: ```kotlin -fun main(){ +fun main() { val message: String = "Hello, World!" print(message) } From 7e30fbaf6f807c558b5bd744f83850213c3b4135 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 13:06:26 +0530 Subject: [PATCH 024/111] Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <72277593+yangc95@users.noreply.github.com> --- content/kotlin/concepts/data-types/data-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md index 0b9a2e4e7aa..c977e53f308 100644 --- a/content/kotlin/concepts/data-types/data-types.md +++ b/content/kotlin/concepts/data-types/data-types.md @@ -300,7 +300,7 @@ fun main() { } ``` -The output for the above code will be : +The output for the above code will be: ``` Hello, World! From 970a372ffa0d5ac8336c60305fdcf44e657ec6a6 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 13:06:36 +0530 Subject: [PATCH 025/111] Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <72277593+yangc95@users.noreply.github.com> --- content/kotlin/concepts/data-types/data-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md index c977e53f308..94256bc946e 100644 --- a/content/kotlin/concepts/data-types/data-types.md +++ b/content/kotlin/concepts/data-types/data-types.md @@ -319,7 +319,7 @@ fun main(){ } ``` -The output for the above code will be : +The output for the above code will be: ``` Hello, John From 4974ee01d2ffbc7a848a7fa30ddccf2d06785e4a Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 13:06:46 +0530 Subject: [PATCH 026/111] Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <72277593+yangc95@users.noreply.github.com> --- content/kotlin/concepts/data-types/data-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md index 94256bc946e..ab67f0dc074 100644 --- a/content/kotlin/concepts/data-types/data-types.md +++ b/content/kotlin/concepts/data-types/data-types.md @@ -370,7 +370,7 @@ In Kotlin, the `Unit` type represents the absence of a value, similar to the `vo The `Unit` type can be used to specify that a function has a return type of `Unit` when it is not possible to infer the return type from the function's body. For example: ```kotlin -fun main(){ +fun main() { } fun doSomething(): Unit { From 37e863f444b25013f4823e3885ae39fff2dd70ab Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 13:06:58 +0530 Subject: [PATCH 027/111] Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <72277593+yangc95@users.noreply.github.com> --- content/kotlin/concepts/data-types/data-types.md | 1 + 1 file changed, 1 insertion(+) diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md index ab67f0dc074..ead6eff0f0b 100644 --- a/content/kotlin/concepts/data-types/data-types.md +++ b/content/kotlin/concepts/data-types/data-types.md @@ -373,6 +373,7 @@ The `Unit` type can be used to specify that a function has a return type of `Uni fun main() { } + fun doSomething(): Unit { // perform some action that you want to do } From 8f211b0c6dc52b9ce8696a51b5c61e0ad08fdd0d Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 13:07:08 +0530 Subject: [PATCH 028/111] Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <72277593+yangc95@users.noreply.github.com> --- content/kotlin/concepts/data-types/data-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md index ead6eff0f0b..b1bbcaad513 100644 --- a/content/kotlin/concepts/data-types/data-types.md +++ b/content/kotlin/concepts/data-types/data-types.md @@ -375,7 +375,7 @@ fun main() { } fun doSomething(): Unit { - // perform some action that you want to do + // Perform some action here } ``` From cd47aaa0d7b230e95df554d07c104b7ce15ed516 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 13:07:23 +0530 Subject: [PATCH 029/111] Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <72277593+yangc95@users.noreply.github.com> --- content/kotlin/concepts/data-types/data-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md index b1bbcaad513..dd06b164db9 100644 --- a/content/kotlin/concepts/data-types/data-types.md +++ b/content/kotlin/concepts/data-types/data-types.md @@ -379,4 +379,4 @@ fun doSomething(): Unit { } ``` -In the above example, the doSomething() function performs an action and does not return a value. Therefore, its return type is `Unit`. +In the above example, the `doSomething()` function performs an action and does not return a value. Therefore, its return type is `Unit`. From f24382f571c46c965aa35aa262ef82178b820a02 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 13:08:08 +0530 Subject: [PATCH 030/111] Update content/kotlin/concepts/data-types/data-types.md Co-authored-by: Christine Yang <72277593+yangc95@users.noreply.github.com> --- content/kotlin/concepts/data-types/data-types.md | 1 + 1 file changed, 1 insertion(+) diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md index dd06b164db9..0884abe1cff 100644 --- a/content/kotlin/concepts/data-types/data-types.md +++ b/content/kotlin/concepts/data-types/data-types.md @@ -374,6 +374,7 @@ fun main() { } + fun doSomething(): Unit { // Perform some action here } From ff95ae26d62338c09df4a7f123976fd3e1fe17ef Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 13:10:45 +0530 Subject: [PATCH 031/111] changed the sentence in line 278 --- content/kotlin/concepts/data-types/data-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md index 0884abe1cff..4f9a61c9e9d 100644 --- a/content/kotlin/concepts/data-types/data-types.md +++ b/content/kotlin/concepts/data-types/data-types.md @@ -275,7 +275,7 @@ The following are non-primitive data types in Kotlin : In Kotlin, an `Array` is a data structure that stores a fixed-size collection of elements of the same data type. Arrays are non-primitive data types, which means they are derived from primitive data types or other non-primitive data types. -To declare an `Array` in Kotlin, you can use the `arrayOf()` function and specify the elements of the array within the parentheses. For example: +To declare an Array in Kotlin, the arrayOf() function can be utilized, with the elements of the array specified within the parentheses. For instance: ```kotlin fun main() { From 9d8e67a710afa851fbfc9f158ca66097de711dae Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 13:11:33 +0530 Subject: [PATCH 032/111] Update data-types.md --- content/kotlin/concepts/data-types/data-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md index 4f9a61c9e9d..4e88bdae5c6 100644 --- a/content/kotlin/concepts/data-types/data-types.md +++ b/content/kotlin/concepts/data-types/data-types.md @@ -275,7 +275,7 @@ The following are non-primitive data types in Kotlin : In Kotlin, an `Array` is a data structure that stores a fixed-size collection of elements of the same data type. Arrays are non-primitive data types, which means they are derived from primitive data types or other non-primitive data types. -To declare an Array in Kotlin, the arrayOf() function can be utilized, with the elements of the array specified within the parentheses. For instance: +To declare an Array in Kotlin, the `arrayOf()` function can be utilized, with the elements of the array specified within the parentheses. For instance: ```kotlin fun main() { From 73e5ae96c155dff10a6d9f20960def5a5a6effd6 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 4 Jan 2023 23:14:19 +0530 Subject: [PATCH 033/111] added variables.md in kotlin folder --- .../kotlin/concepts/data-types/data-types.md | 383 ------------------ .../kotlin/concepts/variables/variables.md | 70 ++++ 2 files changed, 70 insertions(+), 383 deletions(-) delete mode 100644 content/kotlin/concepts/data-types/data-types.md create mode 100644 content/kotlin/concepts/variables/variables.md diff --git a/content/kotlin/concepts/data-types/data-types.md b/content/kotlin/concepts/data-types/data-types.md deleted file mode 100644 index 4e88bdae5c6..00000000000 --- a/content/kotlin/concepts/data-types/data-types.md +++ /dev/null @@ -1,383 +0,0 @@ ---- -Title: 'Data Types' -Description: 'Data types are a classification of types of values that can be stored and manipulated in a program.' -Subjects: - - 'Code Foundations' - - 'Computer Science' -Tags: - - 'Integers' - - 'Data Types' - - 'Boolean' - - 'Characters' -CatalogContent: - - 'learn-Kotlin' - - 'paths/computer-science' ---- - -**Data types** are a classification of types of data that determine possible values and operations that can be performed on that data. - -Kotlin has both `primitive` and `non-primitive` data types. - -## Primitive Data Types - -There are the following different types of primitive data types in Kotlin: - -- Boolean -- Byte -- Char -- Double -- Float -- Int -- Long -- Short - -### Boolean - -In Kotlin, `Boolean` can hold only one of the two possible values: `true` or `false`. Below is an example of how to declare a `Boolean` variable in Kotlin: - -```kotlin -val isTrue: Boolean = true -val isFalse: Boolean = false -``` - -The `Boolean` data type can be used to determine the outcome of `if...else` statements: - -```kotlin -fun main(){ - val condition: Boolean = true - - if (condition) { - // write a code that you want to execute if the variable is given as true - } else { - // write a code for the false variable - } -} -``` - -### Byte - -A `Byte` is a data type that is similar to an integer data type, but it is only 8 bits in size, whereas an integer can be a different size depending on the system. A byte can hold values from -128 to 127. - -A `Byte` is a way to represent a small whole number. It is commonly used to store small pieces of data, such as characters in a text file or colors in an image. - -Here is an example of how to declare a `Byte` variable in Kotlin: - -```kotlin -fun main(){ - val a: Byte = 50 - val b: Byte = 25 - val c: Byte = a + b as Byte - print(c) - -} -``` - -The output will be: - -``` -75 -``` - -The range of the `Byte` data type is -128 to 127. Calculations that exceed this range will produce unexpected results: - -```kotlin -fun main(){ - val a: Byte = 120 - val b: Byte = 120 - val c: Byte = a + b as Byte - print(c) -} -``` - -Here the output will be: - -``` --88 -``` - -In the above example, the value of c will be -88, because the result of 120 + 120 is 240, which exceeds the valid range for a Byte type. - -### Char - -In Kotlin, the `Char` data type represents a primitive data type that can hold any character from the Unicode character set, which includes letters, digits, symbols, and special characters. - -Here is an example of how to declare a `Char` variable in Kotlin: - -```kotlin -fun main(){ - val ch: Char = 'A' - print(ch) -} -``` - -The output will be: - -``` -A -``` - -### Double - -In Kotlin, the `Double` data type represents a 64-bit floating-point number. It is a primitive data type that can hold values with a fractional component, such as 3.14 or 0.5. - -Here is an example of how to declare a `Double` variable in Kotlin: - -```kotlin -fun main(){ - val a: Double = 3.14 - val b: Double = 2.71 - val c: Double = a * b as Double - print(c) -} -``` - -The output of the above code will be: - -``` -8.5094 -``` - -In this example, the c variable will be assigned the value 8.5014, which is the result of 3.14 \* 2.71. - -The `Double` data type can be used to perform more advanced operations, such as calculating the square root of a number or raising a number to a power in Kotlin. For example: - -```kotlin - -import kotlin.math.sqrt -import kotlin.math.pow -fun main(){ - - -val a: Double = 16.0 -val b: Double = sqrt(a) -val c: Double = a.pow(2) - -println("The square root of $a is $b.") -println("$a raised to the power of 2 is $c.") - -} -``` - -The output will be: - -``` -The square root of 16.0 is 4.0. -16.0 raised to the power of 2 is 256.0. -``` - -### Float - -In Kotlin, the `Float` data type represents a 32-bit floating-point number. It is a primitive data type that can hold values with a fractional component, such as 3.14 or 0.5. - -Here is an example of how to declare a Float variable in Kotlin: - -```kotlin -fun main(){ - val a: Float = 3.14f - val b: Float = 2.71f - val c: Float = a * b as Float - print(c) - -} -``` - -The output will be : - -``` -8.5094 -``` - -In this example, the c variable will be assigned the value 8.5014, which is the result of 3.14 \* 2.71. - -### Int - -In Kotlin, the `Int` data type represents a 32-bit signed integer. It is a primitive data type that can hold values ranging from -2147483648 to 2147483647. - -Here is an example of how to declare an `Int` variable in Kotlin: - -```kotlin -fun main(){ - val a: Int = 50 - val b: Int = 25 - val c: Int = a + b - print(c) -} -``` - -The output of the above code will be: - -``` -75 -``` - -### Long - -In Kotlin, the `Long` data type represents a 64-bit signed integer. It is a primitive data type that can hold values ranging from -9223372036854775808 to 9223372036854775807. - -Here is an example of how to declare a `Long` variable in Kotlin: - -```kotlin -fun main(){ - val a: Long = 500000000 - val b: Long = 250000000 - val c: Long = a + b - print(c) -} -``` - -The output for the above code will be : - -``` -750000000 -``` - -In this example, the c variable will be assigned the value 750000000, which is the result of 500000000 + 250000000. - -### Short - -In Kotlin, the `Short` data type represents a 16-bit signed integer. It is a primitive data type that can hold values ranging from -32768 to 32767. - -Here is an example of how to declare a `Short` variable in Kotlin: - -```kotlin -fun main(){ - val a: Short = 50 - val b: Short = 25 - val c: Short = (a + b).toShort() - print(c) -} -``` - -The output for the above code will be: - -``` -75 -``` - -In this example, the c variable will be assigned the value 75, which is the result of 50 + 25. The `toShort()` function is used to explicitly convert the result to a `Short` type. - -## Non-Primitive Data Types - -The following are non-primitive data types in Kotlin : - -- Any -- Arrays -- Class -- Enum -- List -- Map -- Nothing -- Set -- String -- Unit - -### Arrays - -In Kotlin, an `Array` is a data structure that stores a fixed-size collection of elements of the same data type. Arrays are non-primitive data types, which means they are derived from primitive data types or other non-primitive data types. - -To declare an Array in Kotlin, the `arrayOf()` function can be utilized, with the elements of the array specified within the parentheses. For instance: - -```kotlin -fun main() { - val numbers: Array = arrayOf(1, 2, 3, 4, 5) -} -``` - -In this example, the `numbers` variable is an array of integers that contains the values 1, 2, 3, 4, and 5. The type of the array is specified using the `Array` syntax, which indicates that the array contains elements of the `Int` type. - -If you want to know how to print the array then you can check [Arrays]. - -### String - -In Kotlin, a `String` is a data type that represents a sequence of characters. You can use a string to store and manipulate text in your program. - -Here is an example of how to declare and initialize a string in Kotlin: - -```kotlin -fun main() { - val message: String = "Hello, World!" - print(message) -} -``` - -The output for the above code will be: - -``` -Hello, World! -``` - -Strings are also [immutable](https://kotlinlang.org/docs/native-immutability.html) in Kotlin. - -You can use the '+' operator to concatenate strings, or the trim() function to remove leading and trailing whitespace from a string. For example: - -```kotlin -fun main(){ - val greeting: String = "Hello, " - val name: String = "John" - val fullMessage: String = greeting + name - println(fullMessage.trim()) -} -``` - -The output for the above code will be: - -``` -Hello, John -``` - -### Any - -In Kotlin, the `Any` type is a supertype of all types in the language. It represents a general type that can hold any value. - -Here is an example of how to declare a variable of type `Any` in Kotlin: - -```kotlin -fun main(){ - val value: Any = 100 - val value2: Any = "Hello, World!" - println(value) - println(value2) -} -``` - -The output for the above code will be : - -``` -100 -Hello, World! -``` - -### Nothing - -In Kotlin, the `Nothing` type is a special type that represents the absence of a value. It is a subtype of all types in the language and cannot be instantiated. - -The `Nothing` type is used to indicate that a function never returns a value. For example, a function that throws an exception or terminates the program will have a return type of `Nothing`. - -Here is an example of a function that has a return type of `Nothing`: - -```kotlin -fun error(): Nothing { - throw IllegalStateException("An error occurred.") -} -``` - -In the above, the `error()` function throws an exception and does not return a value. Therefore, its return type is `Nothing`. - -### Unit - -In Kotlin, the `Unit` type represents the absence of a value, similar to the `void` type in other programming languages. It is a special type that is used to indicate that a function does not return a value. - -The `Unit` type can be used to specify that a function has a return type of `Unit` when it is not possible to infer the return type from the function's body. For example: - -```kotlin -fun main() { - -} - - -fun doSomething(): Unit { - // Perform some action here -} -``` - -In the above example, the `doSomething()` function performs an action and does not return a value. Therefore, its return type is `Unit`. diff --git a/content/kotlin/concepts/variables/variables.md b/content/kotlin/concepts/variables/variables.md new file mode 100644 index 00000000000..31a8ea73a6b --- /dev/null +++ b/content/kotlin/concepts/variables/variables.md @@ -0,0 +1,70 @@ +--- +Title: 'Variables' +Description: 'Variables are used whenever there’s a need to store a piece of data and ensures code re-usability.' +Subjects: + - 'Computer Science' +Tags: + - 'Variables' + - 'Data Types' +CatalogContent: + - 'learn-java' + - 'paths/computer-science' +--- + +**Variable** is a storage location for a value that can be either mutable or read-only. The value stored in a variable can be of different types, such as numbers, characters, or strings. + +## Declaring a Variable + +A `variable` in Kotlin is declared using the `var` keyword for a mutable variable or the `val` keyword for a read-only variable. Here's an example of how to declare variables in Kotlin: + +```pseudo +fun main() { + // Declare a mutable variable + var x: Int = 5 + // Declare a read-only variable + val y: String = "Hello, World!" +} +``` + +The type of a `variable` is optional in Kotlin, as the type can be inferred from the value assigned to the variable. For example, the following declarations are equivalent to the ones above: + +```kotlin +fun main() { + // Declare a mutable variable with inferred type + var x = 5 + // Declare a read-only variable with inferred type + val y = "Hello, World!" +} +``` + +It's important to note that, unlike other programming languages, Kotlin doesn't have a `null` type. Instead, it has nullable and non-nullable types. A nullable type can hold a null value, while a non-nullable type cannot. To declare a nullable type, you can use the `?` operator after the type. For example: + +```kotlin +fun main() { + // Declare a nullable variable + var z: String? = null +} +``` + +One thing that not many people know about `variables` in Kotlin is that they can have `delegated` properties. A `delegated` property is a special type of property that is computed automatically by a delegate object, rather than being stored directly in the class. + +Here's an example of how to declare a delegated property in Kotlin: + +```kotlin +import kotlin.reflect.KProperty +class Example { + var prop: String by Delegate() +} +class Delegate { + operator fun getValue(thisRef: Any?, property: KProperty<*>): String { + return "$thisRef, thank you for delegating '${property.name}' to me!" + } + operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) { + println("$value has been assigned to '${property.name}' in $thisRef.") + } +} +``` + +In this example, the `prop` property of the `Example` class is delegated to an instance of the `Delegate` class. The `getValue` and `setValue` functions of the `Delegate` class define how the property is accessed and modified. + +`Delegated` properties are a powerful and flexible feature of Kotlin that can be used to implement various design patterns, such as the observer pattern or the proxy pattern. They can also be used to simplify code by abstracting away common property-related tasks, such as lazy initialization or thread-safe access. From 6af23b8a7cc132baeedbece712d6c5e50a363939 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 6 Jan 2023 22:51:16 +0530 Subject: [PATCH 034/111] Update content/kotlin/concepts/variables/variables.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/variables/variables.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/variables/variables.md b/content/kotlin/concepts/variables/variables.md index 31a8ea73a6b..e6204b2072b 100644 --- a/content/kotlin/concepts/variables/variables.md +++ b/content/kotlin/concepts/variables/variables.md @@ -7,7 +7,7 @@ Tags: - 'Variables' - 'Data Types' CatalogContent: - - 'learn-java' + - 'learn-kotlin' - 'paths/computer-science' --- From 6381b7b71a201e23e86255e2c3034407e7bcff3b Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 6 Jan 2023 22:51:22 +0530 Subject: [PATCH 035/111] Update content/kotlin/concepts/variables/variables.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/variables/variables.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/variables/variables.md b/content/kotlin/concepts/variables/variables.md index e6204b2072b..cd24a56002e 100644 --- a/content/kotlin/concepts/variables/variables.md +++ b/content/kotlin/concepts/variables/variables.md @@ -11,7 +11,7 @@ CatalogContent: - 'paths/computer-science' --- -**Variable** is a storage location for a value that can be either mutable or read-only. The value stored in a variable can be of different types, such as numbers, characters, or strings. +A **variable** is a storage location for a value that can be either mutable or read-only. The value stored in a variable can be of different types, such as numbers, characters, or strings. ## Declaring a Variable From 664148fd8d564f626173a170295d4ddd9acea3cd Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 6 Jan 2023 22:51:28 +0530 Subject: [PATCH 036/111] Update content/kotlin/concepts/variables/variables.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/variables/variables.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/variables/variables.md b/content/kotlin/concepts/variables/variables.md index cd24a56002e..220b6f62576 100644 --- a/content/kotlin/concepts/variables/variables.md +++ b/content/kotlin/concepts/variables/variables.md @@ -15,7 +15,7 @@ A **variable** is a storage location for a value that can be either mutable or r ## Declaring a Variable -A `variable` in Kotlin is declared using the `var` keyword for a mutable variable or the `val` keyword for a read-only variable. Here's an example of how to declare variables in Kotlin: +A variable in Kotlin is declared using the `var` keyword for a mutable variable or the `val` keyword for a read-only variable. Here's an example of how to declare variables in Kotlin: ```pseudo fun main() { From 36081dc589152a80da4e7f9452a94c5af73f31bd Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 6 Jan 2023 22:51:35 +0530 Subject: [PATCH 037/111] Update content/kotlin/concepts/variables/variables.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/variables/variables.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/variables/variables.md b/content/kotlin/concepts/variables/variables.md index 220b6f62576..11e82b89760 100644 --- a/content/kotlin/concepts/variables/variables.md +++ b/content/kotlin/concepts/variables/variables.md @@ -26,7 +26,7 @@ fun main() { } ``` -The type of a `variable` is optional in Kotlin, as the type can be inferred from the value assigned to the variable. For example, the following declarations are equivalent to the ones above: +Declaring the type of a variable is optional in Kotlin, as the type can be inferred from the value assigned to the variable. For example, the following declarations are equivalent to the ones above: ```kotlin fun main() { From b5a42f439ef31228738c8795eb771d66300f6864 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 6 Jan 2023 22:52:19 +0530 Subject: [PATCH 038/111] Update variables.md --- content/kotlin/concepts/variables/variables.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/variables/variables.md b/content/kotlin/concepts/variables/variables.md index 11e82b89760..7ada09d3b38 100644 --- a/content/kotlin/concepts/variables/variables.md +++ b/content/kotlin/concepts/variables/variables.md @@ -17,7 +17,7 @@ A **variable** is a storage location for a value that can be either mutable or r A variable in Kotlin is declared using the `var` keyword for a mutable variable or the `val` keyword for a read-only variable. Here's an example of how to declare variables in Kotlin: -```pseudo +```kotlin fun main() { // Declare a mutable variable var x: Int = 5 From 8152444aa828eecc6994f799c577b2b8370ea5ba Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Sat, 7 Jan 2023 23:39:31 +0530 Subject: [PATCH 039/111] del --- .../kotlin/concepts/variables/variables.md | 70 ------------------- 1 file changed, 70 deletions(-) delete mode 100644 content/kotlin/concepts/variables/variables.md diff --git a/content/kotlin/concepts/variables/variables.md b/content/kotlin/concepts/variables/variables.md deleted file mode 100644 index 7ada09d3b38..00000000000 --- a/content/kotlin/concepts/variables/variables.md +++ /dev/null @@ -1,70 +0,0 @@ ---- -Title: 'Variables' -Description: 'Variables are used whenever there’s a need to store a piece of data and ensures code re-usability.' -Subjects: - - 'Computer Science' -Tags: - - 'Variables' - - 'Data Types' -CatalogContent: - - 'learn-kotlin' - - 'paths/computer-science' ---- - -A **variable** is a storage location for a value that can be either mutable or read-only. The value stored in a variable can be of different types, such as numbers, characters, or strings. - -## Declaring a Variable - -A variable in Kotlin is declared using the `var` keyword for a mutable variable or the `val` keyword for a read-only variable. Here's an example of how to declare variables in Kotlin: - -```kotlin -fun main() { - // Declare a mutable variable - var x: Int = 5 - // Declare a read-only variable - val y: String = "Hello, World!" -} -``` - -Declaring the type of a variable is optional in Kotlin, as the type can be inferred from the value assigned to the variable. For example, the following declarations are equivalent to the ones above: - -```kotlin -fun main() { - // Declare a mutable variable with inferred type - var x = 5 - // Declare a read-only variable with inferred type - val y = "Hello, World!" -} -``` - -It's important to note that, unlike other programming languages, Kotlin doesn't have a `null` type. Instead, it has nullable and non-nullable types. A nullable type can hold a null value, while a non-nullable type cannot. To declare a nullable type, you can use the `?` operator after the type. For example: - -```kotlin -fun main() { - // Declare a nullable variable - var z: String? = null -} -``` - -One thing that not many people know about `variables` in Kotlin is that they can have `delegated` properties. A `delegated` property is a special type of property that is computed automatically by a delegate object, rather than being stored directly in the class. - -Here's an example of how to declare a delegated property in Kotlin: - -```kotlin -import kotlin.reflect.KProperty -class Example { - var prop: String by Delegate() -} -class Delegate { - operator fun getValue(thisRef: Any?, property: KProperty<*>): String { - return "$thisRef, thank you for delegating '${property.name}' to me!" - } - operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) { - println("$value has been assigned to '${property.name}' in $thisRef.") - } -} -``` - -In this example, the `prop` property of the `Example` class is delegated to an instance of the `Delegate` class. The `getValue` and `setValue` functions of the `Delegate` class define how the property is accessed and modified. - -`Delegated` properties are a powerful and flexible feature of Kotlin that can be used to implement various design patterns, such as the observer pattern or the proxy pattern. They can also be used to simplify code by abstracting away common property-related tasks, such as lazy initialization or thread-safe access. From 3d6c895136f5a16557360805465dc0c619769312 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 9 Jan 2023 23:02:05 +0530 Subject: [PATCH 040/111] added hashmap.md in kotlin folder --- content/kotlin/concepts/hashmap/hashmap.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 content/kotlin/concepts/hashmap/hashmap.md diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md new file mode 100644 index 00000000000..e69de29bb2d From 8c031b33fcbd6ff803f1c0e3ddc2f8471292e9b0 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 9 Jan 2023 23:07:32 +0530 Subject: [PATCH 041/111] did some changes --- content/kotlin/concepts/hashmap/hashmap.md | 118 +++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index e69de29bb2d..c7f9821091f 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -0,0 +1,118 @@ +--- +Title: 'HashMap' +Description: 'HashMap is used to store items as a key-value pairs. The keys and values can be of either same or different types.' +Subjects: + - 'Computer Science' +Tags: + - 'HashMap' + - 'Data Types' + - 'Collections' + - 'Data Structures' +CatalogContent: + - 'learn-kotlin' + - 'paths/computer-science' +--- + +A `HashMap` is a data structure that maps keys to values and is implemented using a hash table. It is an efficient way to store and retrieve data because it uses a hash function to map keys to indices in an array, so that keys can be found quickly. In Kotlin, a `HashMap` is represented by the `HashMap` class. + +## Syntax + +```pseudo +val map = HashMap() +``` + +Here, `KeyType` is the type of the keys in the map, and `ValueType` is the type of the values. + +You can also specify the types when you create an instance of the `HashMap` class like this: + +```pseudo +val map: HashMap = HashMap() +``` + +You can also use the `mutableMapOf()` function to create a mutable HashMap: + +```pseudo +val map = mutableMapOf() +``` + +And you can use the `mapOf()` function to create an immutable `HashMap`: + +```pseudo +val map = mapOf() +``` + +## Accessing Item + +To access the elements of a `HashMap`, you can use the `get()` function to retrieve the value for a given key. For example: + +```kotlin +val map = HashMap().apply { + put("apple", 1) + put("banana", 2) + put("cherry", 3) +} + +val value = map.get("apple") +``` + +The output for the above code will be: + +``` +1 +``` + +If the `HashMap` contains the specified key, the index operator returns the value for that key. If the key is not found, it returns `null`. + +You can also use the `containsKey()` function to check if a HashMap contains a given key: + +```kotlin +if (map.containsKey("apple")) { + // do something +} +``` + +Finally, you can use the `forEach()` function to iterate over the key-value pairs in the HashMap: + +```kotlin +map.forEach { key, value -> + println("$key -> $value") +} +``` + +## Adding a item + +To add a key-value pair to a `HashMap` in Kotlin, you can use the `put()` function. For example: + +```kotlin +val map = HashMap() + +map.put("apple", 1) +map.put("banana", 2) +map.put("cherry", 3) +``` + +You can also use the index operator `[]` to add a key-value pair to the `HashMap`. For example: + +```kotlin +val map = HashMap() + +map["apple"] = 1 +map["banana"] = 2 +map["cherry"] = 3 +``` + +Finally, you can use the `putAll()` function to add all the key-value pairs from another Map to the `HashMap`: + +```kotlin +val map1 = HashMap().apply { + put("apple", 1) + put("banana", 2) +} + +val map2 = HashMap().apply { + put("cherry", 3) + put("date", 4) +} + +map1.putAll(map2) // adds "cherry" -> 3 and "date" -> 4 to map1 +``` From 5d5485db845df0e54c4807d90aa900463582ee7e Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 9 Jan 2023 23:35:54 +0530 Subject: [PATCH 042/111] added remove function of hashmap --- content/kotlin/concepts/hashmap/hashmap.md | 29 +++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index c7f9821091f..bd933bc2da5 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -79,7 +79,7 @@ map.forEach { key, value -> } ``` -## Adding a item +## Adding an item To add a key-value pair to a `HashMap` in Kotlin, you can use the `put()` function. For example: @@ -116,3 +116,30 @@ val map2 = HashMap().apply { map1.putAll(map2) // adds "cherry" -> 3 and "date" -> 4 to map1 ``` + +## Removing an item + +To remove a key-value pair from a `HashMap` in Kotlin, you can use the `remove()` function. For example: + +```kotlin +val map = HashMap().apply { + put("apple", 1) + put("banana", 2) + put("cherry", 3) +} + +map.remove("banana") // removes "banana" -> 2 from the map +``` + +You can also use the `remove()` function to remove a key-value pair from the `HashMap` only if the value matches a certain condition. For example: + +```kotlin +val map = HashMap().apply { + put("apple", 1) + put("banana", 2) + put("cherry", 3) +} + +map.remove("cherry") { it == 3 } // removes "cherry" -> 3 from the map +map.remove("apple") { it == 3 } // does not remove anything, the value for "apple" is not 3 +``` From d4e59e3febae9d22df71d64b72fb88070356980e Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Tue, 10 Jan 2023 23:03:17 +0530 Subject: [PATCH 043/111] Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/hashmap/hashmap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index bd933bc2da5..8cb312199a5 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -1,6 +1,6 @@ --- Title: 'HashMap' -Description: 'HashMap is used to store items as a key-value pairs. The keys and values can be of either same or different types.' +Description: 'A HashMap is used to store items as a key-value pairs. The keys and values can be of either same or different types.' Subjects: - 'Computer Science' Tags: From 4a46ec79dff542f38a54b3b8dd3c9f525e04df45 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Tue, 10 Jan 2023 23:03:24 +0530 Subject: [PATCH 044/111] Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/hashmap/hashmap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index 8cb312199a5..8790296e488 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -23,7 +23,7 @@ val map = HashMap() Here, `KeyType` is the type of the keys in the map, and `ValueType` is the type of the values. -You can also specify the types when you create an instance of the `HashMap` class like this: +The types can also be specified when creating an instance of the `HashMap` class like this: ```pseudo val map: HashMap = HashMap() From 683bc4e5ca305b8b992f1662115aeace1a06d0c8 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Tue, 10 Jan 2023 23:22:52 +0530 Subject: [PATCH 045/111] Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/hashmap/hashmap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index 8790296e488..afd5641ca1e 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -29,7 +29,7 @@ The types can also be specified when creating an instance of the `HashMap` class val map: HashMap = HashMap() ``` -You can also use the `mutableMapOf()` function to create a mutable HashMap: +The `mutableMapOf()` function is used to create a mutable HashMap: ```pseudo val map = mutableMapOf() From 0490ebd068f6d9cb2d0e25ab224c1a3c487ad32a Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Tue, 10 Jan 2023 23:23:01 +0530 Subject: [PATCH 046/111] Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/hashmap/hashmap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index afd5641ca1e..409bc84d864 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -35,7 +35,7 @@ The `mutableMapOf()` function is used to create a mutable HashMap: val map = mutableMapOf() ``` -And you can use the `mapOf()` function to create an immutable `HashMap`: +The `mapOf()` function is used to create an immutable `HashMap`: ```pseudo val map = mapOf() From fa775c00fda7677237842b57b761e014ab94c7fb Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Tue, 10 Jan 2023 23:23:09 +0530 Subject: [PATCH 047/111] Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/hashmap/hashmap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index 409bc84d864..4717a4415f8 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -43,7 +43,7 @@ val map = mapOf() ## Accessing Item -To access the elements of a `HashMap`, you can use the `get()` function to retrieve the value for a given key. For example: +To access the elements of a `HashMap`, the `get()` function is used to retrieve the value for a given key. For example: ```kotlin val map = HashMap().apply { From c1254e63f0c34356e6354525c0f36ec44e040f02 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Tue, 10 Jan 2023 23:23:16 +0530 Subject: [PATCH 048/111] Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/hashmap/hashmap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index 4717a4415f8..18eb73f6e08 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -63,7 +63,7 @@ The output for the above code will be: If the `HashMap` contains the specified key, the index operator returns the value for that key. If the key is not found, it returns `null`. -You can also use the `containsKey()` function to check if a HashMap contains a given key: +The `containsKey()` function is used to check if a HashMap contains a given key: ```kotlin if (map.containsKey("apple")) { From 2e7f9d1cc4bac9f3bce0364b538e5dcb0545cb62 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Tue, 10 Jan 2023 23:23:22 +0530 Subject: [PATCH 049/111] Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/hashmap/hashmap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index 18eb73f6e08..bd2b8817c6a 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -71,7 +71,7 @@ if (map.containsKey("apple")) { } ``` -Finally, you can use the `forEach()` function to iterate over the key-value pairs in the HashMap: +The `forEach()` function is used to iterate over the key-value pairs in the HashMap: ```kotlin map.forEach { key, value -> From 8a5c17ae006d146f2619cfbcc9f5ca913eb567ab Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Tue, 10 Jan 2023 23:23:29 +0530 Subject: [PATCH 050/111] Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/hashmap/hashmap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index bd2b8817c6a..3d46858b59d 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -81,7 +81,7 @@ map.forEach { key, value -> ## Adding an item -To add a key-value pair to a `HashMap` in Kotlin, you can use the `put()` function. For example: +To add a key-value pair to a `HashMap` in Kotlin, the `put()` function is used. For example: ```kotlin val map = HashMap() From e3cf71824b36936b9cfb5bb6b3a44d4ddb5d33ce Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Tue, 10 Jan 2023 23:23:35 +0530 Subject: [PATCH 051/111] Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/hashmap/hashmap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index 3d46858b59d..dfd3ff0a895 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -91,7 +91,7 @@ map.put("banana", 2) map.put("cherry", 3) ``` -You can also use the index operator `[]` to add a key-value pair to the `HashMap`. For example: +The index operator `[]` can also be used to add a key-value pair to the `HashMap`. For example: ```kotlin val map = HashMap() From 9753de679f96f5e74416e1d728586f34e08e9ac8 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Tue, 10 Jan 2023 23:23:42 +0530 Subject: [PATCH 052/111] Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/hashmap/hashmap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index dfd3ff0a895..f3a8147d02b 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -101,7 +101,7 @@ map["banana"] = 2 map["cherry"] = 3 ``` -Finally, you can use the `putAll()` function to add all the key-value pairs from another Map to the `HashMap`: +The `putAll()` function can be used to add all the key-value pairs from another map to the `HashMap`: ```kotlin val map1 = HashMap().apply { From 4d7be2df3f3fe7e1adb289ef5be289614113ca5b Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Tue, 10 Jan 2023 23:23:49 +0530 Subject: [PATCH 053/111] Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/hashmap/hashmap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index f3a8147d02b..3d24cac1296 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -119,7 +119,7 @@ map1.putAll(map2) // adds "cherry" -> 3 and "date" -> 4 to map1 ## Removing an item -To remove a key-value pair from a `HashMap` in Kotlin, you can use the `remove()` function. For example: +To remove a key-value pair from a `HashMap` in Kotlin, the `remove()` function is used. For example: ```kotlin val map = HashMap().apply { From af7aa34406810891345a7efcdc848a5a372488c9 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Tue, 10 Jan 2023 23:23:56 +0530 Subject: [PATCH 054/111] Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/kotlin/concepts/hashmap/hashmap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index 3d24cac1296..6a21754ae15 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -131,7 +131,7 @@ val map = HashMap().apply { map.remove("banana") // removes "banana" -> 2 from the map ``` -You can also use the `remove()` function to remove a key-value pair from the `HashMap` only if the value matches a certain condition. For example: +The `remove()` function can be used to remove a key-value pair from the `HashMap` only if the value matches a certain condition. For example: ```kotlin val map = HashMap().apply { From b4acdbd4d1af8853d86fec394543fd80fc87e448 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 18 Jan 2023 22:19:50 +0530 Subject: [PATCH 055/111] Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: Brandon Dusch --- content/kotlin/concepts/hashmap/hashmap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index 6a21754ae15..080d09731cf 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -1,5 +1,5 @@ --- -Title: 'HashMap' +Title: 'HashMaps' Description: 'A HashMap is used to store items as a key-value pairs. The keys and values can be of either same or different types.' Subjects: - 'Computer Science' From 52b68be6f7acad0b04c194de69f7c76a7095a346 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 18 Jan 2023 22:20:01 +0530 Subject: [PATCH 056/111] Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: Brandon Dusch --- content/kotlin/concepts/hashmap/hashmap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index 080d09731cf..eb7bb43073f 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -1,6 +1,6 @@ --- Title: 'HashMaps' -Description: 'A HashMap is used to store items as a key-value pairs. The keys and values can be of either same or different types.' +Description: 'HashMaps are unordered collections of key-value pairs that are implemented using hash tables.' Subjects: - 'Computer Science' Tags: From 48c648faf14f24a4b8285c6bf847361bf18f3549 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 18 Jan 2023 22:20:09 +0530 Subject: [PATCH 057/111] Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: Brandon Dusch --- content/kotlin/concepts/hashmap/hashmap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index eb7bb43073f..ed4c2a5742b 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -13,7 +13,7 @@ CatalogContent: - 'paths/computer-science' --- -A `HashMap` is a data structure that maps keys to values and is implemented using a hash table. It is an efficient way to store and retrieve data because it uses a hash function to map keys to indices in an array, so that keys can be found quickly. In Kotlin, a `HashMap` is represented by the `HashMap` class. +**HashMaps** are unordered collections key-value pairs that are implemented using a [hash table](https://www.codecademy.com/resources/docs/general/hash-table). They offer efficient storage and data retrieval because of how keys are mapped to indices in an array. In Kotlin, a HashMap is represented by the `HashMap` class. ## Syntax From eeebfdcb5d036faf89b57138f66e3b8740fdda95 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 18 Jan 2023 22:20:18 +0530 Subject: [PATCH 058/111] Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: Brandon Dusch --- content/kotlin/concepts/hashmap/hashmap.md | 1 + 1 file changed, 1 insertion(+) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index ed4c2a5742b..f865be9a9f6 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -19,6 +19,7 @@ CatalogContent: ```pseudo val map = HashMap() +val map: HashMap = HashMap() ``` Here, `KeyType` is the type of the keys in the map, and `ValueType` is the type of the values. From 384fbfba25d58c7e27383aadbfd9e04a53c1eb48 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 18 Jan 2023 22:20:40 +0530 Subject: [PATCH 059/111] Update content/kotlin/concepts/hashmap/hashmap.md Co-authored-by: Brandon Dusch --- content/kotlin/concepts/hashmap/hashmap.md | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md index f865be9a9f6..b278d258d84 100644 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ b/content/kotlin/concepts/hashmap/hashmap.md @@ -22,13 +22,7 @@ val map = HashMap() val map: HashMap = HashMap() ``` -Here, `KeyType` is the type of the keys in the map, and `ValueType` is the type of the values. - -The types can also be specified when creating an instance of the `HashMap` class like this: - -```pseudo -val map: HashMap = HashMap() -``` +The `KeyType` and `ValueType` are the data types respectively assigned to the keys and values of the `map`. The `mutableMapOf()` function is used to create a mutable HashMap: From bdc79331e4c9586ba0b913a0db94768e12c267d2 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Sat, 28 Jan 2023 00:02:44 +0530 Subject: [PATCH 060/111] del --- content/kotlin/concepts/hashmap/hashmap.md | 140 --------------------- 1 file changed, 140 deletions(-) delete mode 100644 content/kotlin/concepts/hashmap/hashmap.md diff --git a/content/kotlin/concepts/hashmap/hashmap.md b/content/kotlin/concepts/hashmap/hashmap.md deleted file mode 100644 index b278d258d84..00000000000 --- a/content/kotlin/concepts/hashmap/hashmap.md +++ /dev/null @@ -1,140 +0,0 @@ ---- -Title: 'HashMaps' -Description: 'HashMaps are unordered collections of key-value pairs that are implemented using hash tables.' -Subjects: - - 'Computer Science' -Tags: - - 'HashMap' - - 'Data Types' - - 'Collections' - - 'Data Structures' -CatalogContent: - - 'learn-kotlin' - - 'paths/computer-science' ---- - -**HashMaps** are unordered collections key-value pairs that are implemented using a [hash table](https://www.codecademy.com/resources/docs/general/hash-table). They offer efficient storage and data retrieval because of how keys are mapped to indices in an array. In Kotlin, a HashMap is represented by the `HashMap` class. - -## Syntax - -```pseudo -val map = HashMap() -val map: HashMap = HashMap() -``` - -The `KeyType` and `ValueType` are the data types respectively assigned to the keys and values of the `map`. - -The `mutableMapOf()` function is used to create a mutable HashMap: - -```pseudo -val map = mutableMapOf() -``` - -The `mapOf()` function is used to create an immutable `HashMap`: - -```pseudo -val map = mapOf() -``` - -## Accessing Item - -To access the elements of a `HashMap`, the `get()` function is used to retrieve the value for a given key. For example: - -```kotlin -val map = HashMap().apply { - put("apple", 1) - put("banana", 2) - put("cherry", 3) -} - -val value = map.get("apple") -``` - -The output for the above code will be: - -``` -1 -``` - -If the `HashMap` contains the specified key, the index operator returns the value for that key. If the key is not found, it returns `null`. - -The `containsKey()` function is used to check if a HashMap contains a given key: - -```kotlin -if (map.containsKey("apple")) { - // do something -} -``` - -The `forEach()` function is used to iterate over the key-value pairs in the HashMap: - -```kotlin -map.forEach { key, value -> - println("$key -> $value") -} -``` - -## Adding an item - -To add a key-value pair to a `HashMap` in Kotlin, the `put()` function is used. For example: - -```kotlin -val map = HashMap() - -map.put("apple", 1) -map.put("banana", 2) -map.put("cherry", 3) -``` - -The index operator `[]` can also be used to add a key-value pair to the `HashMap`. For example: - -```kotlin -val map = HashMap() - -map["apple"] = 1 -map["banana"] = 2 -map["cherry"] = 3 -``` - -The `putAll()` function can be used to add all the key-value pairs from another map to the `HashMap`: - -```kotlin -val map1 = HashMap().apply { - put("apple", 1) - put("banana", 2) -} - -val map2 = HashMap().apply { - put("cherry", 3) - put("date", 4) -} - -map1.putAll(map2) // adds "cherry" -> 3 and "date" -> 4 to map1 -``` - -## Removing an item - -To remove a key-value pair from a `HashMap` in Kotlin, the `remove()` function is used. For example: - -```kotlin -val map = HashMap().apply { - put("apple", 1) - put("banana", 2) - put("cherry", 3) -} - -map.remove("banana") // removes "banana" -> 2 from the map -``` - -The `remove()` function can be used to remove a key-value pair from the `HashMap` only if the value matches a certain condition. For example: - -```kotlin -val map = HashMap().apply { - put("apple", 1) - put("banana", 2) - put("cherry", 3) -} - -map.remove("cherry") { it == 3 } // removes "cherry" -> 3 from the map -map.remove("apple") { it == 3 } // does not remove anything, the value for "apple" is not 3 -``` From afaddda1b0cd75df534b12efc9e3e54c25b26370 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Sat, 10 Jun 2023 00:12:26 +0530 Subject: [PATCH 061/111] adding eluclidean algotrithm --- .../euclidean-algorithm.md | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md new file mode 100644 index 00000000000..8fb614cbe69 --- /dev/null +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -0,0 +1,46 @@ +--- +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.' +Subjects: + - 'Computer Science' +Tags: + - 'Algorithms' + - 'Hcf' +CatalogContent: + - 'paths/computer-science' +--- + +The `Euclidean algorithm` is a recursive algorithm that allows us to find the highest common factor (HCF) of two numbers. It is based on the observation that if we have two numbers, say A and B, and if 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. + +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`. + +## Basic Approach + +In the basic approach, we can follow a simple rule to find the GCD. We start by finding the minimum value between the two given numbers. Then, we divide this minimum value by both numbers. If the number does not divide both numbers evenly, we decrease it by `1` and continue dividing it by both numbers. We repeat this process until the minimum value can divide both numbers evenly. At that point, the minimum value is our HCF (highest common factor). + +```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 +``` + +## Optimized approach with Euclidean algorithm + +Here two methods are shown using Euclidean algorithm to find the HCF of two numbers. From e133fcf43a12d673cb0a22a6e8473f16ee99e5a7 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Sat, 10 Jun 2023 08:32:55 +0530 Subject: [PATCH 062/111] added euclidean algorith method 1 --- .../euclidean-algorithm.md | 53 +++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 8fb614cbe69..65b0c69313c 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -16,7 +16,7 @@ There can be multiple methods to solve and find the highest common factor (HCF) ## Basic Approach -In the basic approach, we can follow a simple rule to find the GCD. We start by finding the minimum value between the two given numbers. Then, we divide this minimum value by both numbers. If the number does not divide both numbers evenly, we decrease it by `1` and continue dividing it by both numbers. We repeat this process until the minimum value can divide both numbers evenly. At that point, the minimum value is our HCF (highest common factor). +In the basic approach, Let's follow a simple rule to find the GCD. Let's start by finding the minimum value between the two given numbers. Then, let's divide this minimum value by both numbers. If the number does not divide both numbers evenly, let's decrease it by `1` and continue dividing it by both numbers. Let's repeat this process until the minimum value can divide both numbers evenly. At that point, the minimum value is our HCF (highest common factor). ```java public static void main(String[] args) { @@ -41,6 +41,53 @@ The output for the above code will be: 5 ``` -## Optimized approach with Euclidean algorithm +## Approach with Euclidean algorithm -Here two methods are shown using Euclidean algorithm to find the HCF of two numbers. +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. + +## Method 1 + +In this method, we aim to compare both 'a' and 'b'. Whichever number is greater, we subtract the smaller number from the larger number and update the value of the larger number accordingly. If 'a' is greater than 'b', we replace 'a' with 'a - b', and if 'b' is greater than 'a', we replace 'b' with 'b - a'. We repeat this step until 'a' becomes equal to 'b'. Once 'a' and 'b' are equal, we 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 +class Euclidean { +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 out for the above code will be: + +```shell +5 +``` + +If we debug the code above step by steps then: + +Sure! Here's the updated explanation: + +- Since 'b' is greater than 'a' (20 > 15), we replace 'b' with 'b - a', which gives us 'b = 20 - 15 = 5'. +- Now 'a' is still 15, and 'b' is 5. +- We proceed to replace 'a' with 'a - b', resulting in 'a = 15 - 5 = 10'. +- The values are now 'a = 10' and 'b = 5'. +- We continue the process and replace 'a' with 'a - b', giving us 'a = 10 - 5 = 5'. +- 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. +- Therefore, the highest common factor (HCF) of (15, 20) is 5. From fad1a2b6075c5bca334f0bef535518e2c5b96a50 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Sat, 10 Jun 2023 10:13:20 +0530 Subject: [PATCH 063/111] completed all 3 methods and time complexity --- .../euclidean-algorithm.md | 65 ++++++++++++++++++- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 65b0c69313c..d7665c8d2fa 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -50,7 +50,7 @@ Here two methods are shown using Euclidean algorithm to find the HCF of two numb In this method, we aim to compare both 'a' and 'b'. Whichever number is greater, we subtract the smaller number from the larger number and update the value of the larger number accordingly. If 'a' is greater than 'b', we replace 'a' with 'a - b', and if 'b' is greater than 'a', we replace 'b' with 'b - a'. We repeat this step until 'a' becomes equal to 'b'. Once 'a' and 'b' are equal, we 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 -class Euclidean { +class Euclidean1 { static int gcd(int a, int b) { while (a != b) { @@ -81,8 +81,6 @@ The out for the above code will be: If we debug the code above step by steps then: -Sure! Here's the updated explanation: - - Since 'b' is greater than 'a' (20 > 15), we replace 'b' with 'b - a', which gives us 'b = 20 - 15 = 5'. - Now 'a' is still 15, and 'b' is 5. - We proceed to replace 'a' with 'a - b', resulting in 'a = 15 - 5 = 10'. @@ -91,3 +89,64 @@ Sure! Here's the updated explanation: - 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. - Therefore, the highest common factor (HCF) of (15, 20) is 5. + +## Method 2 + +In this method we will use [recursion](https://www.codecademy.com/learn/discrete-math-recurrence-relations). The method takes two integer parameters 'a' and 'b' . Then returns an integer as the result. The gcd method uses a `recursive` approach to implement the `Euclidean algorithm`. It first checks if `b` is equal to 0. If it is, then it means that `a` is the GCD, and it returns `a` as the result. If `b` is not 0, it means 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` becomes 0, at which point the base case is triggered, and the GCD is found. + +```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 +``` + +Let's debug the code above step by steps: + +- We have two integers as input: `a = 150` and `b = 500`. We enter the `EuclideanOptimized` function, and the first `if` statement we encounter is `(b == 0)`. In this case, `b` is not equal to 0, so we will exit the `if` statement. + +- We then proceed to return `EuclideanOptimized`, but with the arguments changed to `(b, a % b)`. + +- In the first recursive cycle, `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)`. + +- We now start the function again with arguments `(500, 150)`. We enter the `if` statement, but since `b` is not equal to 0, we will exit the `if` statement. The returned arguments for the next recursive cycle will be `(150, 50)` after calculating the modulus of `500 % 150`. + +- Once again, we start the function with arguments `(150, 50)`. We enter the `if` statement, but as before, `b` is not equal to 0, so we exit the `if` statement. The returned arguments after the modulus of `150 % 50` are `(50, 0)`. + +- In this recursive cycle, when we enter 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. + +## Time complexities + +- 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)). + +- 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)). + +- 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. + +To summarize, the time complexities of the three codes are as follows: + +- Code 1: O(min(a, b)) +- Code 2: O(max(a, b)) +- Code 3: O(log(max(a, b))) + +Note that these time complexities 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. From 54d0b094b27badd07b80a60027a2f328da35ada3 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Sat, 10 Jun 2023 10:21:34 +0530 Subject: [PATCH 064/111] changing the title numbers --- .../terms/euclidean-algorithm/euclidean-algorithm.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index d7665c8d2fa..5beca2a3d44 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -14,7 +14,7 @@ The `Euclidean algorithm` is a recursive algorithm that allows us to find the hi 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`. -## Basic Approach +## Method 1 In the basic approach, Let's follow a simple rule to find the GCD. Let's start by finding the minimum value between the two given numbers. Then, let's divide this minimum value by both numbers. If the number does not divide both numbers evenly, let's decrease it by `1` and continue dividing it by both numbers. Let's repeat this process until the minimum value can divide both numbers evenly. At that point, the minimum value is our HCF (highest common factor). @@ -45,7 +45,7 @@ The output for the above code will be: 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. -## Method 1 +## Method 2 In this method, we aim to compare both 'a' and 'b'. Whichever number is greater, we subtract the smaller number from the larger number and update the value of the larger number accordingly. If 'a' is greater than 'b', we replace 'a' with 'a - b', and if 'b' is greater than 'a', we replace 'b' with 'b - a'. We repeat this step until 'a' becomes equal to 'b'. Once 'a' and 'b' are equal, we 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'. @@ -90,7 +90,7 @@ If we debug the code above step by steps then: - At this point, the while loop exits, and we return 'a' as our answer, which is 5. - Therefore, the highest common factor (HCF) of (15, 20) is 5. -## Method 2 +## Method 3 In this method we will use [recursion](https://www.codecademy.com/learn/discrete-math-recurrence-relations). The method takes two integer parameters 'a' and 'b' . Then returns an integer as the result. The gcd method uses a `recursive` approach to implement the `Euclidean algorithm`. It first checks if `b` is equal to 0. If it is, then it means that `a` is the GCD, and it returns `a` as the result. If `b` is not 0, it means 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` becomes 0, at which point the base case is triggered, and the GCD is found. From b0947125adc0050d3b0f5ad898f8cf9a294194f2 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 12 Jun 2023 10:26:08 +0530 Subject: [PATCH 065/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: Christine Belzie <105683440+CBID2@users.noreply.github.com> --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 5beca2a3d44..3bd8793d1db 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -47,7 +47,7 @@ Here two methods are shown using Euclidean algorithm to find the HCF of two numb ## Method 2 -In this method, we aim to compare both 'a' and 'b'. Whichever number is greater, we subtract the smaller number from the larger number and update the value of the larger number accordingly. If 'a' is greater than 'b', we replace 'a' with 'a - b', and if 'b' is greater than 'a', we replace 'b' with 'b - a'. We repeat this step until 'a' becomes equal to 'b'. Once 'a' and 'b' are equal, we 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'. +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 class Euclidean1 { From d40441a66b92e624763575e23d5d2d8d1458ee55 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 12 Jun 2023 11:00:37 +0530 Subject: [PATCH 066/111] Suggested changes --- .../euclidean-algorithm.md | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 3bd8793d1db..2610e555590 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -10,7 +10,7 @@ CatalogContent: - 'paths/computer-science' --- -The `Euclidean algorithm` is a recursive algorithm that allows us to find the highest common factor (HCF) of two numbers. It is based on the observation that if we have two numbers, say A and B, and if 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. +The `Euclidean algorithm` is a recursive algorithm that allows us 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. 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`. @@ -73,26 +73,28 @@ public static void main(String[] args) { } ``` -The out for the above code will be: +The output for the above code will be: ```shell 5 ``` -If we debug the code above step by steps then: +When we debug the code above step by step, we can analyze it as follows: -- Since 'b' is greater than 'a' (20 > 15), we replace 'b' with 'b - a', which gives us 'b = 20 - 15 = 5'. -- Now 'a' is still 15, and 'b' is 5. -- We proceed to replace 'a' with 'a - b', resulting in 'a = 15 - 5 = 10'. +- Since 'b' is greater than 'a' (20 > 15), here 'b' is replaced with 'b - a', which gives us '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'. -- We continue the process and replace 'a' with 'a - b', giving us 'a = 10 - 5 = 5'. +- The process is continued as '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 we return 'a' as our answer, which is 5. - Therefore, the highest common factor (HCF) of (15, 20) is 5. ## Method 3 -In this method we will use [recursion](https://www.codecademy.com/learn/discrete-math-recurrence-relations). The method takes two integer parameters 'a' and 'b' . Then returns an integer as the result. The gcd method uses a `recursive` approach to implement the `Euclidean algorithm`. It first checks if `b` is equal to 0. If it is, then it means that `a` is the GCD, and it returns `a` as the result. If `b` is not 0, it means 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` becomes 0, at which point the base case is triggered, and the GCD is found. +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. + +For more information on recursion, you can refer to this [resource](https://www.codecademy.com/learn/java-algorithms/modules/recursion-apcs/cheatsheet). ```java public class Euclidean2 { @@ -117,16 +119,11 @@ The output for the above code will be: Let's debug the code above step by steps: -- We have two integers as input: `a = 150` and `b = 500`. We enter the `EuclideanOptimized` function, and the first `if` statement we encounter is `(b == 0)`. In this case, `b` is not equal to 0, so we will exit the `if` statement. - -- We then proceed to return `EuclideanOptimized`, but with the arguments changed to `(b, a % b)`. - +- Given the input of two integers: `a = 150` and `b = 500`, we proceed 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. +- Then proceed to return `EuclideanOptimized`, but with the arguments changed to `(b, a % b)`. - In the first recursive cycle, `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)`. - -- We now start the function again with arguments `(500, 150)`. We enter the `if` statement, but since `b` is not equal to 0, we will exit the `if` statement. The returned arguments for the next recursive cycle will be `(150, 50)` after calculating the modulus of `500 % 150`. - -- Once again, we start the function with arguments `(150, 50)`. We enter the `if` statement, but as before, `b` is not equal to 0, so we exit the `if` statement. The returned arguments after the modulus of `150 % 50` are `(50, 0)`. - +- 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`. +- 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)`. - In this recursive cycle, when we enter 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. ## Time complexities From f031bcaa2791d8d61985145c8791e26eadb16919 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 12 Jun 2023 11:14:45 +0530 Subject: [PATCH 067/111] Update euclidean-algorithm.md --- .../terms/euclidean-algorithm/euclidean-algorithm.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 2610e555590..e1dab7727be 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -79,7 +79,7 @@ The output for the above code will be: 5 ``` -When we debug the code above step by step, we can analyze it as follows: +When debugging the code above step by step, one can analyze the code as follows: - Since 'b' is greater than 'a' (20 > 15), here 'b' is replaced with 'b - a', which gives us 'b = 20 - 15 = 5'. - Now 'a' is 15 and 'b' is 5. @@ -119,12 +119,12 @@ The output for the above code will be: Let's debug the code above step by steps: -- Given the input of two integers: `a = 150` and `b = 500`, we proceed 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. -- Then proceed to return `EuclideanOptimized`, but with the arguments changed to `(b, a % b)`. -- In the first recursive cycle, `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)`. +- 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. +- Then the code proceeds to return `EuclideanOptimized`, but with the arguments changed to `(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 exits the `if` statement. The next recursive cycle will use the arguments `(150, 50)`, which are obtained by calculating the modulus of `500 % 150`. - 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)`. -- In this recursive cycle, when we enter 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. +- 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. ## Time complexities From 97bb0abf5bb0255e360c52391337bcd6dd58da0f Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 12 Jun 2023 20:39:18 +0530 Subject: [PATCH 068/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: Christine Belzie <105683440+CBID2@users.noreply.github.com> --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index e1dab7727be..3dcc0424f9a 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -146,4 +146,4 @@ To summarize, the time complexities of the three codes are as follows: - Code 2: O(max(a, b)) - Code 3: O(log(max(a, b))) -Note that these time complexities 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. +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. From 1ffe09d047de6c7d826f6e87cab0bb8c70c4f3a3 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 12 Jun 2023 21:34:42 +0530 Subject: [PATCH 069/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: Christine Belzie <105683440+CBID2@users.noreply.github.com> --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 3dcc0424f9a..647ada91884 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -10,7 +10,7 @@ CatalogContent: - 'paths/computer-science' --- -The `Euclidean algorithm` is a recursive algorithm that allows us 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. +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. 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`. From 3003ead945447686e7342ff1f4a59aade88744f6 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 12 Jun 2023 21:34:51 +0530 Subject: [PATCH 070/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: Christine Belzie <105683440+CBID2@users.noreply.github.com> --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 647ada91884..b92beb39af1 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -16,7 +16,7 @@ There can be multiple methods to solve and find the highest common factor (HCF) ## Method 1 -In the basic approach, Let's follow a simple rule to find the GCD. Let's start by finding the minimum value between the two given numbers. Then, let's divide this minimum value by both numbers. If the number does not divide both numbers evenly, let's decrease it by `1` and continue dividing it by both numbers. Let's repeat this process until the minimum value can divide both numbers evenly. At that point, the minimum value is our HCF (highest common factor). +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 public static void main(String[] args) { From c697de116b9cc11dc5f5ef7300cbe59a231610bf Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 12 Jun 2023 21:34:59 +0530 Subject: [PATCH 071/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: Christine Belzie <105683440+CBID2@users.noreply.github.com> --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index b92beb39af1..538e1e7d968 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -94,7 +94,7 @@ When debugging the code above step by step, one can analyze the code as follows: 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. -For more information on recursion, you can refer to this [resource](https://www.codecademy.com/learn/java-algorithms/modules/recursion-apcs/cheatsheet). +For more information on recursion, refer to this [resource](https://www.codecademy.com/learn/java-algorithms/modules/recursion-apcs/cheatsheet). ```java public class Euclidean2 { From eaffa1c16e8e4c3cc0f6ec27bd15df731c8a0646 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 19 Jun 2023 11:15:03 +0530 Subject: [PATCH 072/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 538e1e7d968..a41b41d77a3 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -1,6 +1,6 @@ --- 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.' +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: From dfa36e1a450ac4f5a9a77314e3e9896178059805 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 19 Jun 2023 11:16:12 +0530 Subject: [PATCH 073/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index a41b41d77a3..aebe7cc78d4 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -5,7 +5,7 @@ Subjects: - 'Computer Science' Tags: - 'Algorithms' - - 'Hcf' + - 'Arithmetic' CatalogContent: - 'paths/computer-science' --- From 5b72a2bd6a0b30daedbbf1d3f1789c6f88fa3a2a Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 19 Jun 2023 11:16:22 +0530 Subject: [PATCH 074/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index aebe7cc78d4..a66cd46b129 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -130,7 +130,7 @@ Let's debug the code above step by steps: - 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)). + - 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))**. - Method 2: From 41a88f66952bd19902f7989370f9f4f43fc16ebc Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 19 Jun 2023 11:16:47 +0530 Subject: [PATCH 075/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index a66cd46b129..7416c736041 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -134,7 +134,7 @@ Let's debug the code above step by steps: - 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)). + - 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))**. - Method 3: From 939e8b959e19d7178da67b07736b9dd7810a3335 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 19 Jun 2023 11:16:54 +0530 Subject: [PATCH 076/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 7416c736041..d13fe6fc2a8 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -138,7 +138,7 @@ Let's debug the code above step by steps: - 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. + - 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 **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: From 79613a86ab1dd949e96ea96c71dd084bd66ffa0a Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 19 Jun 2023 11:17:02 +0530 Subject: [PATCH 077/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index d13fe6fc2a8..e308da2859c 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -144,6 +144,6 @@ To summarize, the time complexities of the three codes are as follows: - Code 1: O(min(a, b)) - Code 2: O(max(a, b)) -- Code 3: O(log(max(a, b))) +- Code 3: **O(log(max(a, b)))** 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. From 8f27ae01e9cac02b42946d55386be01bf0f90579 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 19 Jun 2023 11:17:09 +0530 Subject: [PATCH 078/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index e308da2859c..4ad190ffd3a 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -146,4 +146,4 @@ To summarize, the time complexities of the three codes are as follows: - Code 2: O(max(a, b)) - Code 3: **O(log(max(a, b)))** -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. +> **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. From 5497a4069cb59a4f7b44390dcb8e93333f4a97b6 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 19 Jun 2023 11:19:28 +0530 Subject: [PATCH 079/111] Pseudo changes --- .../terms/euclidean-algorithm/euclidean-algorithm.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 4ad190ffd3a..5418ff7c5e1 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -18,7 +18,7 @@ There can be multiple methods to solve and find the highest common factor (HCF) 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 +```pseudo public static void main(String[] args) { System.out.println(gcd(10,15)); } @@ -49,7 +49,7 @@ Here two methods are shown using Euclidean algorithm to find the HCF of two numb 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 +```pseudo class Euclidean1 { static int gcd(int a, int b) { @@ -96,7 +96,7 @@ In this method, a recursive approach is used to implement the Euclidean algorith For more information on recursion, refer to this [resource](https://www.codecademy.com/learn/java-algorithms/modules/recursion-apcs/cheatsheet). -```java +```pseudo public class Euclidean2 { public static void main(String[] args) { System.out.println(EuclideanOptimized(150, 500)); From 7cbfaf9ede35a885f0a9bff93c5b74ca25f0cee1 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 19 Jun 2023 11:20:04 +0530 Subject: [PATCH 080/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 5418ff7c5e1..ec3ffffd084 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -142,7 +142,7 @@ Let's debug the code above step by steps: To summarize, the time complexities of the three codes are as follows: -- Code 1: O(min(a, b)) +- Code 1: **O(min(a, b))** - Code 2: O(max(a, b)) - Code 3: **O(log(max(a, b)))** From d296763c7b512fcd8ca35e5e32c0371960ac1f09 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Mon, 19 Jun 2023 11:20:17 +0530 Subject: [PATCH 081/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index ec3ffffd084..15554ec9b46 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -143,7 +143,7 @@ Let's debug the code above step by steps: To summarize, the time complexities of the three codes are as follows: - Code 1: **O(min(a, b))** -- Code 2: O(max(a, b)) +- Code 2: **O(max(a, b))** - Code 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. From 9c72eb8ec34649e1537b9bd77f704f6947c09b2a Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 21 Jun 2023 22:54:32 +0530 Subject: [PATCH 082/111] Changing tags --- .../terms/euclidean-algorithm/euclidean-algorithm.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 15554ec9b46..b0ab4665179 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -18,7 +18,7 @@ There can be multiple methods to solve and find the highest common factor (HCF) 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). -```pseudo +```java public static void main(String[] args) { System.out.println(gcd(10,15)); } @@ -49,7 +49,7 @@ Here two methods are shown using Euclidean algorithm to find the HCF of two numb 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'. -```pseudo +```java class Euclidean1 { static int gcd(int a, int b) { @@ -96,7 +96,7 @@ In this method, a recursive approach is used to implement the Euclidean algorith For more information on recursion, refer to this [resource](https://www.codecademy.com/learn/java-algorithms/modules/recursion-apcs/cheatsheet). -```pseudo +```java public class Euclidean2 { public static void main(String[] args) { System.out.println(EuclideanOptimized(150, 500)); From 0c3837907bb373307cd151dabdbe5d6b186d04cb Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Thu, 22 Jun 2023 20:19:43 +0530 Subject: [PATCH 083/111] Update euclidean-algorithm.md --- .../terms/euclidean-algorithm/euclidean-algorithm.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index b0ab4665179..ab0bcd7f95c 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -16,7 +16,7 @@ There can be multiple methods to solve and find the highest common factor (HCF) ## 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). +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). The following code illustrates this method in Java: ```java public static void main(String[] args) { @@ -47,7 +47,7 @@ Here two methods are shown using Euclidean algorithm to find the HCF of two numb ## Method 2 -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'. +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'. The following code illustrates this method in Java: ```java class Euclidean1 { @@ -94,7 +94,7 @@ When debugging the code above step by step, one can analyze the code as follows: 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. -For more information on recursion, refer to this [resource](https://www.codecademy.com/learn/java-algorithms/modules/recursion-apcs/cheatsheet). +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 { From 7f4a664dc8bc302c7c94b7c58223c5559613f5e3 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:01:27 +0530 Subject: [PATCH 084/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index ab0bcd7f95c..fcbae2940a0 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -1,5 +1,5 @@ --- -Title: 'Euclidean algorithm' +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' From 4bf925f2f34d8d6b0c7f4c6e5f4e524014138c55 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:01:40 +0530 Subject: [PATCH 085/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 1 + 1 file changed, 1 insertion(+) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index fcbae2940a0..b172303e995 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -7,6 +7,7 @@ Tags: - 'Algorithms' - 'Arithmetic' CatalogContent: + - 'learn-java' - 'paths/computer-science' --- From 60f1535783a0a7838cf3c36a6fb74f4558d700d9 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:02:05 +0530 Subject: [PATCH 086/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index b172303e995..05e62bf0844 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -11,7 +11,7 @@ CatalogContent: - 'paths/computer-science' --- -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. +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 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`. From 935401260353921a06a9dad839fe235e8b5dec55 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:02:14 +0530 Subject: [PATCH 087/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 05e62bf0844..b5fe65cf11d 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -13,7 +13,7 @@ CatalogContent: 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 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`. +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 From 25a57ffe128ec5d9e0fc85d3f74b71df6945da8c Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:02:25 +0530 Subject: [PATCH 088/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../terms/euclidean-algorithm/euclidean-algorithm.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index b5fe65cf11d..a367ee43b04 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -17,7 +17,9 @@ There are multiple methods to solve and find the highest common factor (HCF) of ## 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). The following code illustrates this method in Java: +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 `1` 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) { From 032d2ee60696cb1649c7af825d69ee40a4ff40d9 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:02:45 +0530 Subject: [PATCH 089/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index a367ee43b04..2c077c2712b 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -44,9 +44,6 @@ The output for the above code will be: 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. ## Method 2 From de53909dc7bf5dd60fafb6ad86174e57da13c36e Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:02:57 +0530 Subject: [PATCH 090/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 2c077c2712b..dec8c0413db 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -45,7 +45,7 @@ The output for the above code will be: ``` -## Method 2 +## Method 2: A Basic Euclidean Algorithm Approach 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'. The following code illustrates this method in Java: From 90f696b1f947fdfbdd577dc003a777e776dadc8c Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:03:11 +0530 Subject: [PATCH 091/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../terms/euclidean-algorithm/euclidean-algorithm.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index dec8c0413db..bc2c0aaed0d 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -47,7 +47,9 @@ The output for the above code will be: ## Method 2: A Basic Euclidean Algorithm Approach -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'. The following code illustrates this method in Java: +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 { From c43b9973f14fb898d46bad0e3209504c94960093 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:03:21 +0530 Subject: [PATCH 092/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 1 - 1 file changed, 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index bc2c0aaed0d..eb7ff0d4cde 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -70,7 +70,6 @@ public static void main(String[] args) { int a = 15, b = 20; System.out.println(gcd(a, b)); - } } ``` From 9ac788619f31f42949cb9da4f6f9d91e4ab02823 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:03:30 +0530 Subject: [PATCH 093/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index eb7ff0d4cde..6a681ed6c80 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -80,7 +80,7 @@ The output for the above code will be: 5 ``` -When debugging the code above step by step, one can analyze the code as follows: +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 us 'b = 20 - 15 = 5'. - Now 'a' is 15 and 'b' is 5. From eae1b0956ba8fda521fc34ecce3cbb581e52c589 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:03:43 +0530 Subject: [PATCH 094/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 6a681ed6c80..f50a27ea17b 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -82,7 +82,7 @@ The output for the above code will be: 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 us 'b = 20 - 15 = 5'. +- 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'. From cba498547abfbebf83470656454a06a9fc0fb9e1 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:03:59 +0530 Subject: [PATCH 095/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index f50a27ea17b..90d09cd8dce 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -86,7 +86,7 @@ The step by step execution of the above code is as follows: - 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'. +- 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 we return 'a' as our answer, which is 5. - Therefore, the highest common factor (HCF) of (15, 20) is 5. From 9224cd67e90285702b834a6985a5a28096c3e8e1 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:04:09 +0530 Subject: [PATCH 096/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 90d09cd8dce..ed7f5ffe5a1 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -88,7 +88,7 @@ The step by step execution of the above code is as follows: - 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 we return 'a' as our answer, which is 5. +- At this point, the while loop exits, and `a` is returned. Therefore, the HCF of `15` and `20` is `5`. - Therefore, the highest common factor (HCF) of (15, 20) is 5. ## Method 3 From 1e1b1cbc037a06385fbe30849e17212d463aa9ab Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:04:49 +0530 Subject: [PATCH 097/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 1 - 1 file changed, 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index ed7f5ffe5a1..ce0066f7504 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -89,7 +89,6 @@ The step by step execution of the above code is as follows: - 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`. -- Therefore, the highest common factor (HCF) of (15, 20) is 5. ## Method 3 From 5f5d159caa7cf43111f4ee66d011b79559c405a4 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:04:59 +0530 Subject: [PATCH 098/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index ce0066f7504..279fb1c487f 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -90,7 +90,7 @@ The step by step execution of the above code is as follows: - 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 +## 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 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. From d5d4cde91821a946c34303169c14d33fc5e32d48 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:05:10 +0530 Subject: [PATCH 099/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 279fb1c487f..a23357b5b6b 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -92,7 +92,7 @@ The step by step execution of the above code is as follows: ## 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 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. +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: From c17d7b2665e773b0d1018e2034f26b489e492944 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:05:38 +0530 Subject: [PATCH 100/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index a23357b5b6b..f56eb12a639 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -117,7 +117,7 @@ The output for the above code will be: 50 ``` -Let's debug the code above step by steps: +The step by step execution of this example is as follows: - 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. - Then the code proceeds to return `EuclideanOptimized`, but with the arguments changed to `(b, a % b)`. From 0f4b6798ece3f6c4236d5efe0d8160edc0c802e7 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:06:09 +0530 Subject: [PATCH 101/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index f56eb12a639..2d4b4b3a9a3 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -119,7 +119,7 @@ The output for the above code will be: The step by step execution of this example is as follows: -- 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. +- 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 changed to `(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 exits the `if` statement. The next recursive cycle will use the arguments `(150, 50)`, which are obtained by calculating the modulus of `500 % 150`. From 8e8e1368fc1797ec995068160fbe9b1c2ce3d4d8 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:06:22 +0530 Subject: [PATCH 102/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 2d4b4b3a9a3..6fa3b17658a 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -120,7 +120,7 @@ The output for the above code will be: 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 changed to `(b, a % b)`. +- 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 exits the `if` statement. The next recursive cycle will use the arguments `(150, 50)`, which are obtained by calculating the modulus of `500 % 150`. - 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)`. From 132dda945fe24355c3a90902686d33bb50b4f239 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:06:32 +0530 Subject: [PATCH 103/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 6fa3b17658a..1a87bc10244 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -122,7 +122,7 @@ 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 exits the `if` statement. The next recursive cycle will use the arguments `(150, 50)`, which are obtained by calculating the modulus of `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)`. 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)`. - 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. From 3bb3560c3260fd0fb0320cb14a363b2a7a42a98a Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:06:43 +0530 Subject: [PATCH 104/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 1a87bc10244..97e23fca95c 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -123,7 +123,7 @@ The step by step execution of this example is as follows: - 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)`. 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)`. +- 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`, and the highest common factor (HCF) for the input will be 50. ## Time complexities From f00142b97dc6cd07e7fdd44867c745407064a3af Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:06:55 +0530 Subject: [PATCH 105/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 97e23fca95c..84bebe5f879 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -124,7 +124,7 @@ The step by step execution of this example is as follows: - 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`, and the highest common factor (HCF) for the input will be 50. +- 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 From 3b95e8ebec058045cdb0959a2e968ac5529c9c73 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:07:06 +0530 Subject: [PATCH 106/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 84bebe5f879..ebe9c529c99 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -126,7 +126,7 @@ The step by step execution of this example is as follows: - 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 +## Time Complexities - Method 1: From ffc01048ebc28e3fe8b85af457ee20575585b553 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:07:15 +0530 Subject: [PATCH 107/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index ebe9c529c99..49245edd4d8 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -130,7 +130,7 @@ The step by step execution of this example is as follows: - 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))**. + - 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 1, checking if it divides both `a` and `b`. Therefore, the time complexity of this code is *O(min(a, b))*. - Method 2: From 1000185c2d09b48c0ccaa87b7c72cf019968bbf1 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:07:24 +0530 Subject: [PATCH 108/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 49245edd4d8..631610cba1d 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -134,7 +134,7 @@ The step by step execution of this example is as follows: - 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))**. + - 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: From 70d347b901987015ee988a98844240b63dd15332 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:07:35 +0530 Subject: [PATCH 109/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../algorithm/terms/euclidean-algorithm/euclidean-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index 631610cba1d..b4ceb9e9ffb 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -138,7 +138,7 @@ The step by step execution of this example is as follows: - 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 **O(log(max(a, b)))** since the algorithm reduces the values quickly by taking the modulus. + - 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: From aed9a4b980b450ba38a31a7292215cc6e279e1c3 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 23 Jun 2023 09:07:46 +0530 Subject: [PATCH 110/111] Update content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md Co-authored-by: caupolicandiaz --- .../terms/euclidean-algorithm/euclidean-algorithm.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index b4ceb9e9ffb..bfc7f38d3c7 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -142,8 +142,8 @@ The step by step execution of this example is as follows: To summarize, the time complexities of the three codes are as follows: -- Code 1: **O(min(a, b))** -- Code 2: **O(max(a, b))** -- Code 3: **O(log(max(a, b)))** +- 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. From f8ca014267a78c5c8b1ecbcd5f4de4c117495b2f Mon Sep 17 00:00:00 2001 From: Caupolican Diaz Date: Fri, 23 Jun 2023 09:33:54 -0700 Subject: [PATCH 111/111] review edits --- .../euclidean-algorithm.md | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md index bfc7f38d3c7..b752c10b34e 100644 --- a/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md +++ b/content/general/concepts/algorithm/terms/euclidean-algorithm/euclidean-algorithm.md @@ -17,7 +17,7 @@ There are multiple methods to solve and find the highest common factor (HCF) of ## 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 `1` and continue dividing. This process repeats until the minimum value can divide both numbers evenly. At this point, the minimum value is the HCF. +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: @@ -44,10 +44,9 @@ The output for the above code will be: 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. +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: @@ -82,17 +81,17 @@ The output for the above code will be: 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`. +- 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. +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: @@ -119,31 +118,31 @@ The output for the above code will be: 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. +- 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. +- 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 1, checking if it divides both `a` and `b`. Therefore, the time complexity of this code is *O(min(a, b))*. + - 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))*. + - 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. + - 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)))* +- 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.