Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3597158
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:04:56+00:00 2026-05-18T20:04:56+00:00

Euclid’s Algorithm for GCF of two numbers is: GCF(a, b)=GCF(b, a mod b) .

  • 0

Euclid’s Algorithm for GCF of two numbers is: GCF(a, b)=GCF(b, a mod b). I have seen this implemented in Python as follows:

def gcf(a, b):
    return b and gcf(b, a%b) or a

I don’t understand how to parse this function or specifically how boolean logic is applied to integers. For example, gcf(42, 56) = 14. When I walk through it, I see that eventually the recursive part returns zero. I follow that 0 or n == n and 0 and n == 0. However, once I have a pair of non-zero integers being compared with and/or logic, I don’t understand what happens and why.

Can someone walk me through this function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:04:56+00:00Added an answer on May 18, 2026 at 8:04 pm

    Python boolean operators ‘or’ and ‘and’ don’t return boolean values. They return one of the values they are comparing.

    0 or n – returns n

    0 and n – returns 0

    a and b or c is just a trick to implement the (a ? b : c) syntax in C.

    Read section 4.6 of Dive into Python for a great description of python’s boolean operations and this trick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Euclid's Algorithm for GCF of two numbers is: GCF(a, b)=GCF(b, a mod b) .
I have used Euclid's method to find the L.C.M for two numbers. l.c.m=a*b/(gcd(a,b)) How
can somebody explain why is the following trivial code (implementation of Euclid's algorithm to
i have this code and i want to change it to BigInteger import java.util.*;
The following relation works only for two (3, 12) numbers, it fails to produce
Take this for example: http://www.haskell.org/haskellwiki/99_questions/Solutions/32 (**) Determine the greatest common divisor of two positive
This is probably a very simple question. Say I had a class that computed
euclid :: Int -> Int euclid n = length (filter (gcd n == 1)
I have a code that basically solves one of the problem in Project Euler
We have a gazillion spatial coordinates (x, y and z) representing atoms in 3d

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.