Check whether two numbers are in silver ratio

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

Silver Ratio: Two numbers are said to be in silver ratio if the ratio of the sum of the smaller and twice the larger number to the larger number is the same as the ratio of the larger one to the smaller one. Below is the representation of the silver ratio:

\frac{2*A+B}{A} = \frac{A}{B} = \delta_{\varsigma} = {1+ \sqrt{2}} = 2.414

for A > 0, B > 0

Examples:



Input: A = 2.414, B = 1 
Output: Yes 
Explanation:

\frac{A}{B} = 2.414 \;\;\text{as well as}\;\; \frac{2*A + B}{A} = \frac{2.414}{1} = 2.414

Input: A = 1, B = 0.414 
Output No 
Explanation: Ratio of A to B do not form a golden ratio

Approach: The idea is to find two ratios and check whether they are equal to the silver ratio(2.414).

// Here A denotes the larger number
\frac{A}{B} = \frac{2*A + B}{A} = 2.414 

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to check 
// whether two numbers are in 
// silver ratio with each other 
#include<bits/stdc++.h>
using namespace std;
  
// Function to check that two 
// numbers are in silver ratio 
bool checksilverRatio(float a, float b)
{
      
    // Swapping the numbers such 
    // that A contains the maximum 
    // number between these numbers
    if(a < b)
        swap(a, b);
      
    // First Ratio 
    float ratio1 = ((a / b) * 1000.0) / 1000.0;
      
    // Second Ratio 
    float ratio2 = (int)(((2 * a + b) / 
                          a) * 1000);
    ratio2 = ratio2 / 1000;
      
    // Condition to check that two 
    // numbers are in silver ratio 
    if (ratio1 == ratio2 && 
       (int)(ratio1 - 2.414) == 0)
    {
        cout << "Yes\n";
        return true;
    }
    else
    {
        cout << "No\n";
        return false;
    }
}
  
// Driver Code 
int main()
{
    float a = 2.414;
    float b = 1;
      
    // Function call 
    checksilverRatio(a, b);
}
  
// This code is contributed by ishayadav181

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to check 
// whether two numbers are in 
// silver ratio with each other 
import java.util.*;
import java.lang.*;
  
class GFG{
  
// Function to check that two 
// numbers are in silver ratio 
static boolean checksilverRatio(double a,
                                double b)
{
  
    // Swapping the numbers such 
    // that A contains the maximum 
    // number between these numbers
    if (a < b)
    {
        a = a + b;
        b = a - b;
        a = a - b;
    }
  
    // First Ratio 
    double ratio1 = ((a / b) * 1000) / 1000;
  
    // Second Ratio 
    double ratio2 = (int)(((2 * a + b) / 
                           a) * 1000);
    ratio2 = ratio2 / 1000;
  
    // Condition to check that two 
    // numbers are in silver ratio 
    if (ratio1 == ratio2 && 
       (int)(ratio1 - 2.414) == 0)
    {
        System.out.println("Yes");
        return true;
    }
    else
    {
        System.out.println("No");
        return false;
    }
}
  
// Driver Code
public static void main(String[] args)
{
    double a = 2.414;
    double b = 1;
  
    // Function call 
    checksilverRatio(a, b);
}
}
  
// This code is contributed by jana_sayantan

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to check 
# whether two numbers are in 
# silver ratio with each other
  
# Function to check that two 
# numbers are in silver ratio
def checksilverRatio(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((2 * a + b)/a, 3)
    # Condition to check that two
    # numbers are in silver ratio
    if ratio1 == ratio2 and\
       ratio1 == 2.414:
        print("Yes")
        return True
    else:
        print("No")
        return False
          
# Driver Code
if __name__ == "__main__":
    a = 2.414
    b = 1
      
    # Function Call
    checksilverRatio(a, b)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to check 
// whether two numbers are in 
// silver ratio with each other 
using System;
  
class GFG{ 
  
// Function to check that two 
// numbers are in silver ratio 
static bool checksilverRatio(double a, 
                             double b) 
  
    // Swapping the numbers such 
    // that A contains the maximum 
    // number between these numbers 
    if (a < b) 
    
        a = a + b; 
        b = a - b; 
        a = a - b; 
    
  
    // First Ratio 
    double ratio1 = ((a / b) * 1000) / 1000; 
  
    // Second Ratio 
    double ratio2 = (int)(((2 * a + b) / 
                        a) * 1000); 
    ratio2 = ratio2 / 1000; 
  
    // Condition to check that two 
    // numbers are in silver ratio 
    if (ratio1 == ratio2 && 
       (int)(ratio1 - 2.414) == 0) 
    
        Console.WriteLine("Yes"); 
        return true
    
    else
    
        Console.WriteLine("No"); 
        return false
    
  
// Driver Code 
public static void Main() 
    double a = 2.414; 
    double b = 1; 
  
    // Function call 
    checksilverRatio(a, b); 
  
// This code is contributed by sanjoy_62

chevron_right


Output: 

Yes

References: https://en.wikipedia.org/wiki/Silver_ratio
 

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:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.