{"id":34378,"date":"2024-11-01T09:27:28","date_gmt":"2024-11-01T09:27:28","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34378"},"modified":"2024-11-01T11:41:32","modified_gmt":"2024-11-01T11:41:32","slug":"javascript-coding-test-course-finding-the-greatest-common-divisor","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34378\/","title":{"rendered":"Javascript Coding Test Course, Finding the Greatest Common Divisor"},"content":{"rendered":"<p><body><\/p>\n<h2>Topic: Finding the Greatest Common Divisor<\/h2>\n<h3>Problem Description<\/h3>\n<p>Write a function that calculates the greatest common divisor (GCD) of two given integers <code>a<\/code> and <code>b<\/code>.<br \/>\n    The greatest common divisor is the largest number that divides both numbers.<\/p>\n<h3>Input and Output Format<\/h3>\n<ul>\n<li>Input: Two positive integers <code>a<\/code>, <code>b<\/code> (1 \u2264 <code>a<\/code>, <code>b<\/code> \u2264 10<sup>9<\/sup>)<\/li>\n<li>Output: The greatest common divisor of the two numbers<\/li>\n<\/ul>\n<h3>Example<\/h3>\n<pre>\n        Input: 48, 18\n        Output: 6\n    <\/pre>\n<h3>Approach to the Problem<\/h3>\n<p>There are various methods to find the greatest common divisor, but utilizing the famous <strong>Euclidean Algorithm<\/strong> can solve it efficiently.<br \/>\n    This algorithm is based on the following principle:<\/p>\n<ul>\n<li>The GCD of two integers <code>a<\/code> and <code>b<\/code> can be found by repeatedly calculating <code>a % b<\/code> until <code>b<\/code> becomes 0.<\/li>\n<li>That is, GCD(<code>a<\/code>, <code>b<\/code>) = GCD(<code>b<\/code>, <code>a % b<\/code>), and when <code>b<\/code> becomes 0, <code>a<\/code> is the greatest common divisor.<\/li>\n<\/ul>\n<h3>Explanation of the Euclidean Algorithm<\/h3>\n<p>The Euclidean algorithm operates in the following steps:<\/p>\n<ol>\n<li>Prepare <code>a<\/code> and <code>b<\/code>. If <code>b<\/code> is not 0, proceed to the next step.<\/li>\n<li>Calculate <code>r = a % b<\/code> to obtain the new remainder.<\/li>\n<li>Update the value of <code>a<\/code> to <code>b<\/code>, and the value of <code>b<\/code> to <code>r<\/code>.<\/li>\n<li>Repeat this process until <code>b<\/code> becomes 0.<\/li>\n<li>As a result, <code>a<\/code> will be the greatest common divisor.<\/li>\n<\/ol>\n<h3>JavaScript Implementation<\/h3>\n<p>The code implementing the Euclidean algorithm in JavaScript is as follows:<\/p>\n<pre class=\"algorithm\">\n        function gcd(a, b) {\n            while (b !== 0) {\n                const r = a % b;\n                a = b;\n                b = r;\n            }\n            return a;\n        }\n\n        \/\/ Test\n        const result = gcd(48, 18);\n        console.log(result); \/\/ 6\n    <\/pre>\n<h3>Time Complexity Analysis<\/h3>\n<p>The time complexity of the Euclidean algorithm is O(log(min(a, b))).<br \/>\n    The performance is generally good depending on the ratio of the two numbers, and it is particularly efficient when dealing with large numbers.<\/p>\n<h3>Additional Problems and Exercises<\/h3>\n<p>If you are comfortable with finding the greatest common divisor, try solving the following problems:<\/p>\n<ul>\n<li>Write a function to calculate the least common multiple when given two integers. (Use the fact that LCM(a, b) = a * b \/ GCD(a, b).)<\/li>\n<li>Write a function to find the greatest common divisor of all elements in a given array.<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>In this article, we explored how to solve the problem of finding the greatest common divisor using JavaScript.<br \/>\n    We learned an efficient approach through the Euclidean algorithm.<br \/>\n    Such fundamental algorithms are frequently used in coding tests and practical applications, so thorough practice is essential.<\/p>\n<h3>References<\/h3>\n<ul>\n<li><a href=\"https:\/\/ko.wikipedia.org\/wiki\/%EC%A4%91%EA%B0%90%EC%88%98\">Greatest Common Divisor &#8211; Wikipedia<\/a><\/li>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/euclidean-algorithms-basic-and-extended\/\">Geeks for Geeks &#8211; Euclidean Algorithm<\/a><\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/ko\/docs\/Web\/JavaScript\">MDN Web Docs &#8211; JavaScript<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Topic: Finding the Greatest Common Divisor Problem Description Write a function that calculates the greatest common divisor (GCD) of two given integers a and b. The greatest common divisor is the largest number that divides both numbers. Input and Output Format Input: Two positive integers a, b (1 \u2264 a, b \u2264 109) Output: The &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34378\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Javascript Coding Test Course, Finding the Greatest Common Divisor&#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":[141],"tags":[],"class_list":["post-34378","post","type-post","status-publish","format-standard","hentry","category-javascript-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Javascript Coding Test Course, Finding the Greatest Common Divisor - \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\/34378\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Javascript Coding Test Course, Finding the Greatest Common Divisor - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Topic: Finding the Greatest Common Divisor Problem Description Write a function that calculates the greatest common divisor (GCD) of two given integers a and b. The greatest common divisor is the largest number that divides both numbers. Input and Output Format Input: Two positive integers a, b (1 \u2264 a, b \u2264 109) Output: The &hellip; \ub354 \ubcf4\uae30 &quot;Javascript Coding Test Course, Finding the Greatest Common Divisor&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34378\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:27:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:41:32+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=\"2\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34378\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34378\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Javascript Coding Test Course, Finding the Greatest Common Divisor\",\"datePublished\":\"2024-11-01T09:27:28+00:00\",\"dateModified\":\"2024-11-01T11:41:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34378\/\"},\"wordCount\":347,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Javascript Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34378\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34378\/\",\"name\":\"Javascript Coding Test Course, Finding the Greatest Common Divisor - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:27:28+00:00\",\"dateModified\":\"2024-11-01T11:41:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34378\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34378\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34378\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Javascript Coding Test Course, Finding the Greatest Common Divisor\"}]},{\"@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":"Javascript Coding Test Course, Finding the Greatest Common Divisor - \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\/34378\/","og_locale":"ko_KR","og_type":"article","og_title":"Javascript Coding Test Course, Finding the Greatest Common Divisor - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Topic: Finding the Greatest Common Divisor Problem Description Write a function that calculates the greatest common divisor (GCD) of two given integers a and b. The greatest common divisor is the largest number that divides both numbers. Input and Output Format Input: Two positive integers a, b (1 \u2264 a, b \u2264 109) Output: The &hellip; \ub354 \ubcf4\uae30 \"Javascript Coding Test Course, Finding the Greatest Common Divisor\"","og_url":"https:\/\/atmokpo.com\/w\/34378\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:27:28+00:00","article_modified_time":"2024-11-01T11:41:32+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":"2\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34378\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34378\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Javascript Coding Test Course, Finding the Greatest Common Divisor","datePublished":"2024-11-01T09:27:28+00:00","dateModified":"2024-11-01T11:41:32+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34378\/"},"wordCount":347,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Javascript Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34378\/","url":"https:\/\/atmokpo.com\/w\/34378\/","name":"Javascript Coding Test Course, Finding the Greatest Common Divisor - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:27:28+00:00","dateModified":"2024-11-01T11:41:32+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34378\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34378\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34378\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Javascript Coding Test Course, Finding the Greatest Common Divisor"}]},{"@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\/34378","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=34378"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34378\/revisions"}],"predecessor-version":[{"id":34379,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34378\/revisions\/34379"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}