{"id":27736775,"url":"https://github.com/moosesue/gcd","last_synced_at":"2026-04-30T09:34:02.472Z","repository":{"id":290097716,"uuid":"973365105","full_name":"moosesue/GCD","owner":"moosesue","description":"Standard GCD algorithm in Python and Rust","archived":false,"fork":false,"pushed_at":"2025-04-26T21:10:12.000Z","size":0,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-26T21:28:28.552Z","etag":null,"topics":["cryptography","cryptography-algorithms","gcd","number-theory","python","rust","rust-lang"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/moosesue.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-04-26T20:37:31.000Z","updated_at":"2025-04-26T21:13:20.000Z","dependencies_parsed_at":"2025-04-26T21:38:32.887Z","dependency_job_id":null,"html_url":"https://github.com/moosesue/GCD","commit_stats":null,"previous_names":["moosesue/gcd"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moosesue%2FGCD","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moosesue%2FGCD/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moosesue%2FGCD/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moosesue%2FGCD/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/moosesue","download_url":"https://codeload.github.com/moosesue/GCD/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251330602,"owners_count":21572319,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cryptography","cryptography-algorithms","gcd","number-theory","python","rust","rust-lang"],"created_at":"2025-04-28T14:30:56.250Z","updated_at":"2026-04-30T09:34:02.424Z","avatar_url":"https://github.com/moosesue.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"Greatest Common Divisor: From Factor Lists To Elegant Python\n\n![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)\n\nFinding the greatest common divisor (GCD) of two integers is a simple but useful problem in Mathematics. It’s usually taught in primary school and describes finding the largest whole number that divides into both numbers. Sometimes it is called finding the highest common factor (HCF). It’s also sometimes referred to as Euclid’s algorithm, named after the ancient Greek mathematician, Euclid, who lived c300BC. \n\nA simple example would be finding the highest common factor of 28 and 35.\nIn primary school, you might list the factors of both of these numbers.\n\nFactors of 28: 1, 2, 4, 7, 14, 28\n\nFactors of 35: 1, 5, 7, 35\n\nSo it’s easy to see with a simple example that the largest factor common to both lists is 7.\n\nEuclid formed an algorithm that can be used to find this largest factor with bigger numbers, when writing out lists might be a bit long and tedious. The basic idea is that the largest factor doesn’t change if you replace the bigger number with the difference between it and the smaller number. This is then repeated this until the numbers are the same. This final number is the largest factor of both numbers. So, in our simple example Euclid's original method is:\n\n35 - 28 = 7\n\n28 - 7 = 21\n\n21 - 7 = 14\n\n14 - 7 = 7\n\nSo the GCD is 7.\n\nIn programming, finding the difference repeatedly is equivalent to using the modulus operator % , which gives the remainder after division. This is much more efficient and gives the same result.\n\n35 % 28 = 7\n\n28 % 7 = 0\n\nThe remainder is 0 and so the GCD is 7.\n\nWe could write this in Python as follows:\n\n```python\ndef gcd(a: int, b: int)-\u003eint:\n  #check if either of the numbers passed in is zero\n  if (a !=0) and (b!=0) :\n    #check if a is bigger than b\n    if a \u003e b:\n       #if it is then set new_number_a to be the remainder of a #divided by b and set new_number_b to be b.\n       new_number_a = a%b\n       new_number_b = b\n    else:\n        #if it isn’t then set new_number_a to be the remainder of b #divided by a and set new_number_b to be a.\n        if a \u003c b:\n           new_number_a = b%a\n           new_number_b = a\n  #check if new_number_a is the same as new_number_b and that\n  #new_number_b is not zero. If so, call the function again \n  #with the new numbers.\n  if (new_number_a != new_number_b) and (new_number_b!=0):\n     return gcd(new_number_a, new_number_b)\n  else:\n     #if not then we must be finished and so the result is \n     #new_number_a\n     return new_number_a\n```\n\nHowever, this is not a very elegant solution as it is multiple lines and calculations and the same result can be found with a lot fewer operations. If the numbers are not the same and are not zero then the same function can be called by itself. This is called a recursive function. You could actually just write the following to do exactly the same thing.\n\n```python\ndef gcd(a,b):return gcd(b%a,a) if a else b\n```\n\nIn Rust you could write:\n\n```rust\nfn gcd(a: u32, b: u32) -\u003e u32 {\n  if a == 0 {\n     b\n }\n  else {\n     gcd(b % a, a)\n     }\n}\n``` \n\nOr non-recursively:\n\n```rust\nfn gcd(mut a: u32, mut b: u32) -\u003e u32 {\n while a != 0 {\n    let temp = a;\n    a = b % a;\n    b = temp;\n  }\n b\n}\n``` \n\nFinding the GCD of two numbers is powerful, but what if we wanted to express the GCD as a combination of those numbers?\n\nFor example, what if we wanted to find two numbers $x$ and $y$ such that\n\n$a \\cdot x + b \\cdot y = \\gcd(a, b)$\n\nThis is called Bézout’s identity and is the basis for finding modular inverses. A modular inverse (or modular multiplicative inverse) is a number c such that\n\n$a \\cdot c \\bmod b \\equiv 1 $\n\nUnderstanding this is foundational for algorithms in modern cryptography and forms the backbone of algorithms such as RSA, where modular arithmetic and prime factors are key. Look out for my next article on these topics. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoosesue%2Fgcd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoosesue%2Fgcd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoosesue%2Fgcd/lists"}