Check whether two numbers are in golden ratio

Given two numbers A and B, the task is to check that A and B are in golden ratio.

Golden Ratio: Two numbers are said to be in golden ratio if their ratio is same as the ratio of the sum of the two numbers to the larger number. Here a > b > 0, Below is the geometric representation of the Golden ratio:
\frac{A+B}{A} = \frac{A}{B} = \varphi = \frac{1+ \sqrt{5}}{2} = 1.618


Input: A = 1, B = 0.618
Output: Yes
These two numbers together forms Golden ratio
\frac{A}{B} = \frac{A + B}{A} = \frac{1.618}{1} = 1.618

Input: A = 61.77, B = 38.22
Output Yes
These two numbers together forms Golden ratio
\frac{A}{B} = \frac{A + B}{A} = \frac{99.99}{61.77} = 1.618

Approach: The idea is to find two ratios and check that these ratio is equal to the Golden ratio. That is 1.618.

// Here A denotes the larger number
\frac{A}{B} = \frac{A + B}{A} = 1.618

Below is the implementation of the above approach:






# Python implementation to check 
# whether two numbers are in 
# golden ratio with each other
# Function to check that two 
# numbers are in golden ratio
def checkGoldenRatio(a, b):
    # Swapping the numbers such 
    # that A contains the maximum
    # number between these numbers
    a, b = max(a, b), min(a, b)
    # First Ratio
    ratio1 = round(a/b, 3)
    # Second Ratio
    ratio2 = round((a+b)/a, 3)
    # Condition to check that two
    # numbers are in golden ratio
    if ratio1 == ratio2 and\
       ratio1 == 1.618:
        return True
        return False
# Driver Code
if __name__ == "__main__":
    a = 0.618
    b = 1
    # Function Call
    checkGoldenRatio(a, b)





Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Recommended Posts:

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :


Please write to us at to report any issue with the above content.