{"id":35186,"date":"2024-11-01T09:36:31","date_gmt":"2024-11-01T09:36:31","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=35186"},"modified":"2024-11-01T11:44:43","modified_gmt":"2024-11-01T11:44:43","slug":"kotlin-coding-test-course-extended-euclidean-algorithm","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/35186\/","title":{"rendered":"kotlin coding test course, extended Euclidean algorithm"},"content":{"rendered":"<p><body><\/p>\n<p>One of the frequently encountered topics in coding tests is mathematical algorithms. Among them, the &#8220;Extended Euclidean Algorithm&#8221; is a powerful technique that not only finds the greatest common divisor (GCD) of two numbers but also provides solutions related to B\u00e9zout&#8217;s identity. In this article, I will explain the Extended Euclidean Algorithm and detail the process of solving problems using it.<\/p>\n<h2>What is the Extended Euclidean Algorithm?<\/h2>\n<p>The Extended Euclidean Algorithm is a method for finding integers <code>x<\/code> and <code>y<\/code> in the process of calculating the greatest common divisor <code>gcd(a, b)<\/code> of two integers <code>a<\/code> and <code>b<\/code>:<\/p>\n<p><code>gcd(a, b) = ax + by<\/code><\/p>\n<p>The above equation is known as B\u00e9zout&#8217;s identity, where <code>x<\/code> and <code>y<\/code> are integers. The Extended Euclidean Algorithm can be implemented using either a recursive method or an iterative method.<\/p>\n<h2>Problem Definition<\/h2>\n<p>The problem we will address involves given numbers, using the Extended Euclidean Algorithm to output both the greatest common divisor and B\u00e9zout&#8217;s coefficients.<\/p>\n<h3>Problem<\/h3>\n<blockquote>\n<p><strong>Problem Description:<\/strong><\/p>\n<p>Given two integers <code>A<\/code> and <code>B<\/code>, find the following:<\/p>\n<ul>\n<li>The greatest common divisor <code>GCD(A, B)<\/code>, and<\/li>\n<li>Find integers <code>X<\/code>, <code>Y<\/code> such that <code>GCD(A, B) = AX + BY<\/code>.<\/li>\n<\/ul>\n<\/blockquote>\n<p><strong>Input:<\/strong> Two integers <code>A<\/code>, <code>B<\/code> (-10<sup>9<\/sup> \u2264 <code>A<\/code>, <code>B<\/code> \u2264 10<sup>9<\/sup>)<\/p>\n<p><strong>Output:<\/strong> Print <code>GCD(A, B)<\/code>, <code>X<\/code>, <code>Y<\/code> separated by spaces.<\/p>\n<h2>Problem Solving Process<\/h2>\n<h3>1. Understanding the Basic Algorithm<\/h3>\n<p>First, to implement the Extended Euclidean Algorithm, one must understand the basic Euclidean algorithm. The Euclidean Algorithm is a famous method for finding the greatest common divisor of two numbers, following these steps:<\/p>\n<ul>\n<li>While both <code>A<\/code> and <code>B<\/code> are not zero, execute <code>A = A % B<\/code>.<\/li>\n<li>Then swap the values of <code>A<\/code> and <code>B<\/code>. Make <code>A<\/code> equal to <code>B<\/code> and <code>B<\/code> equal to the previous <code>B<\/code>.<\/li>\n<li>Repeat until one of the numbers becomes zero.<\/li>\n<\/ul>\n<p>The remaining number at the end is the greatest common divisor. By tracking the changes of <code>x<\/code> and <code>y<\/code> at each step, we can construct B\u00e9zout&#8217;s identity.<\/p>\n<h3>2. Implementing the Extended Euclidean Algorithm<\/h3>\n<p>Here is the code implementing the Extended Euclidean Algorithm in Kotlin:<\/p>\n<pre><code>fun extendedGCD(a: Int, b: Int): Triple<int, int=\"\" int,=\"\"> {\n    if (b == 0) {\n        return Triple(a, 1, 0) \/\/ GCD = a, X = 1, Y = 0\n    } else {\n        val (gcd, x1, y1) = extendedGCD(b, a % b) \/\/ Recursive call\n        val x = y1\n        val y = x1 - (a \/ b) * y1\n        return Triple(gcd, x, y) \/\/ Return GCD, X, Y\n    }\n}\n<\/int,><\/code><\/pre>\n<p>The above function takes two integers <code>a<\/code> and <code>b<\/code> as input and returns the greatest common divisor along with the B\u00e9zout coefficients <code>x<\/code> and <code>y<\/code>.<\/p>\n<h3>3. Writing the Main Function<\/h3>\n<p>Now, let\u2019s write code in the main function to read two numbers from the user, call the Extended Euclidean Algorithm, and print the results:<\/p>\n<pre><code>fun main() {\n    val reader = Scanner(System.`in`)\n    println(\"Please enter two integers:\")\n    val A = reader.nextInt()\n    val B = reader.nextInt()\n    \n    val (gcd, x, y) = extendedGCD(A, B)\n    println(\"GCD: $gcd, X: $x, Y: $y\")\n}\n<\/code><\/pre>\n<h2>4. Complexity Analysis<\/h2>\n<p>The time complexity of the Extended Euclidean Algorithm is the same as that of the basic Euclidean algorithm. The time complexity of the Euclidean algorithm is <code>O(log(min(A, B)))<\/code>. This makes the Extended Euclidean Algorithm very efficient as well.<\/p>\n<h2>5. Testing and Examples<\/h2>\n<p>Now let&#8217;s perform some tests using the implemented code. For example, let&#8217;s check the results when <code>A = 30<\/code> and <code>B = 21<\/code>:<\/p>\n<pre><code>Please enter two integers:\n30\n21\nGCD: 3, X: -1, Y: 2\n<\/code><\/pre>\n<p>The result above shows that <code>3 = 30 * (-1) + 21 * 2<\/code>, confirming that B\u00e9zout&#8217;s identity holds.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this posting, we explored how to find the greatest common divisor and B\u00e9zout&#8217;s identity of two numbers using the Extended Euclidean Algorithm. This algorithm can be applied to various problem-solving scenarios and is particularly useful in problems related to mathematical algorithms. Its low complexity makes it a great tool for achieving high scores in actual coding tests. I hope we can continue to explore various algorithms and problem-solving methods together in the future.<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the frequently encountered topics in coding tests is mathematical algorithms. Among them, the &#8220;Extended Euclidean Algorithm&#8221; is a powerful technique that not only finds the greatest common divisor (GCD) of two numbers but also provides solutions related to B\u00e9zout&#8217;s identity. In this article, I will explain the Extended Euclidean Algorithm and detail the &hellip; <a href=\"https:\/\/atmokpo.com\/w\/35186\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;kotlin coding test course, extended Euclidean algorithm&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[106],"tags":[],"class_list":["post-35186","post","type-post","status-publish","format-standard","hentry","category----en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>kotlin coding test course, extended Euclidean algorithm - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/35186\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"kotlin coding test course, extended Euclidean algorithm - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"One of the frequently encountered topics in coding tests is mathematical algorithms. Among them, the &#8220;Extended Euclidean Algorithm&#8221; is a powerful technique that not only finds the greatest common divisor (GCD) of two numbers but also provides solutions related to B\u00e9zout&#8217;s identity. In this article, I will explain the Extended Euclidean Algorithm and detail the &hellip; \ub354 \ubcf4\uae30 &quot;kotlin coding test course, extended Euclidean algorithm&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/35186\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:36:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:44:43+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/35186\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35186\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"kotlin coding test course, extended Euclidean algorithm\",\"datePublished\":\"2024-11-01T09:36:31+00:00\",\"dateModified\":\"2024-11-01T11:44:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35186\/\"},\"wordCount\":497,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/35186\/\",\"url\":\"https:\/\/atmokpo.com\/w\/35186\/\",\"name\":\"kotlin coding test course, extended Euclidean algorithm - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:36:31+00:00\",\"dateModified\":\"2024-11-01T11:44:43+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35186\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/35186\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/35186\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"kotlin coding test course, extended Euclidean algorithm\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"kotlin coding test course, extended Euclidean algorithm - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/35186\/","og_locale":"ko_KR","og_type":"article","og_title":"kotlin coding test course, extended Euclidean algorithm - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"One of the frequently encountered topics in coding tests is mathematical algorithms. Among them, the &#8220;Extended Euclidean Algorithm&#8221; is a powerful technique that not only finds the greatest common divisor (GCD) of two numbers but also provides solutions related to B\u00e9zout&#8217;s identity. In this article, I will explain the Extended Euclidean Algorithm and detail the &hellip; \ub354 \ubcf4\uae30 \"kotlin coding test course, extended Euclidean algorithm\"","og_url":"https:\/\/atmokpo.com\/w\/35186\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:36:31+00:00","article_modified_time":"2024-11-01T11:44:43+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/35186\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/35186\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"kotlin coding test course, extended Euclidean algorithm","datePublished":"2024-11-01T09:36:31+00:00","dateModified":"2024-11-01T11:44:43+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/35186\/"},"wordCount":497,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/35186\/","url":"https:\/\/atmokpo.com\/w\/35186\/","name":"kotlin coding test course, extended Euclidean algorithm - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:36:31+00:00","dateModified":"2024-11-01T11:44:43+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/35186\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/35186\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/35186\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"kotlin coding test course, extended Euclidean algorithm"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=35186"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35186\/revisions"}],"predecessor-version":[{"id":35187,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35186\/revisions\/35187"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=35186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=35186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=35186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}