

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find x, y, z that satisfy 2/n = 1/x + 1/y + 1/z in C++
In this problem, we are given integer values n. Our task is to find x, y, z that satisfy 2/nx + 1/y + 1/z.
Let's take an example to understand the problem,
Input : n = 4 Output : 4, 5, 20
Solution Approach
A simple solution to the problem is by finding the solution using the value of n.
If n = 1, no solution for the equation.
If n > 1, the solution to the equation is x = n, y = n+1, z = n(n+1).
The solution is $2/n\:=\:1/n\:+1\:(n+1)\:+\:1/(n^*(n\:+\:1))$
Example
Program to illustrate the working of our solution
#include <iostream> using namespace std; void findSolution(int a, int b, int n){ for (int i = 0; i * a <= n; i++) { if ((n - (i * a)) % b == 0) { cout<<i<<" and "<<(n - (i * a)) / b; return; } } cout<<"No solution"; } int main(){ int a = 2, b = 3, n = 7; cout<<"The value of x and y for the equation 'ax + by = n' is "; findSolution(a, b, n); return 0; }
Output
The value of x and y for the equation 'ax + by = n' is 2 and 1
- Related Questions & Answers
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x +\ny*y < n in C++
- MySQL where column = 'x, y, z'?
- Construct SLR (1) parsing table for the following grammar\nS → x A y |x B y |x A z\nA → q s | q\nB → q
- Maximize the value of x + y + z such that ax + by + cz = n in C++
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + … + upto n terms in C++
- Find the Number of solutions for the equation x + y + z <= n using C++
- How to plot y=1/x as a single graph in Python?
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n in C++
- C++ program to find the sum of the series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Find larger of x^y and y^x in C++
Advertisements