DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Extended GCD (Extended Euclid Algorithm)

Snippets Manager user avatar by
Snippets Manager
·
Sep. 14, 08 · · Code Snippet
Like (0)
Save
Tweet
926 Views

Join the DZone community and get the full member experience.

Join For Free
This is the extended Euclid algorithm.  This is useful for finding the multiplicative inverse (i.e., find d such that for a given e, d*e mod m = 1) of a number in a modulus space (integers between 0 and the modulus m).  This is commonly used in the RSA algorithm to determine the decryption exponent from the encryption exponent.


def extended_gcd(b,m,_recursion_depth=0)
  if b % m == 0
    temp = [0,1]
    return temp
  else
    temp = extended_gcd(m, b % m, _recursion_depth+1)
    temp2 = [temp[1], temp[0]-temp[1] * ((b/m).to_i)]
    if _recursion_depth == 0
      return temp2[0] % m
    else
      return temp2
    end
  end
end
Algorithm

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Use Geofences for Precise Audience Messaging
  • A Guide to Events in Vue
  • Building a Kotlin Mobile App with the Salesforce SDK, Part 3: Synchronizing Data
  • Create a Self-Service Customer Support Chatbot Without Code

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo