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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Design a Multi-Agent AI Framework in Python for Enterprise LLM Workflows
  • Building a Python API Client That Doesn’t Fall Apart When the API Misbehaves
  • 3D Air Quality Maps With Neo4j, Python, and R
  • Building an Identity-Aware MCP Server in Python

Trending

  • Select AI and Vector Search on a Legacy Oracle Schema: What It Actually Takes
  • Databricks Lakebase: Give Your Agent a Branch, Not Your Production Database
  • This $5,000 Berkeley Humanoid Can Be Built With a Desktop 3D Printer
  • Extracting Entities and Relationships From Engineering Documents With spaCy
  1. DZone
  2. Coding
  3. Languages
  4. Reduce Fractions Function Python

Reduce Fractions Function Python

By 
Snippets Manager user avatar
Snippets Manager
·
Jul. 19, 10 · Code Snippet
Likes (0)
Comment
Save
Tweet
Share
16.5K Views

Join the DZone community and get the full member experience.

Join For Free
A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.


def reducefract(n, d):
    '''Reduces fractions. n is the numerator and d the denominator.'''
    def gcd(n, d):
        while d != 0:
            t = d
            d = n%d
            n = t
        return n
    assert d!=0, "integer division by zero"
    assert isinstance(d, int), "must be int"
    assert isinstance(n, int), "must be int"
    greatest=gcd(n,d)
    n/=greatest
    d/=greatest
    return n, d
Python (language)

Opinions expressed by DZone contributors are their own.

Related

  • How to Design a Multi-Agent AI Framework in Python for Enterprise LLM Workflows
  • Building a Python API Client That Doesn’t Fall Apart When the API Misbehaves
  • 3D Air Quality Maps With Neo4j, Python, and R
  • Building an Identity-Aware MCP Server in Python

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook