
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N) in C++
We are given a number N. The goal is to find ordered pairs of positive numbers such that the sum of their squares is N.
We will do this by finding solutions to the equation a2+ b2 = N. Where a is not more than square root of N and b can be calculated as square root of (N-a2).
Let’s understand with examples.
Input
N=100
Output
Count of pairs of (a,b) where a^3+b^3=N: 2
Explanation
Pairs will be (6,8) and (8,6). 62+82=36+64=100
Input
N=11
Output
Count of pairs of (a,b) where a^3+b^3=N: 0
Explanation
No such pairs possible.
Approach used in the below program is as follows
We take integer N.
Function squareSum(int n) takes n and returns the count of ordered pairs with sum of squares as n.
Take the initial variable count as 0 for pairs.
Traverse using for loop to find a.
Start from a=1 to a<=sqrt(n) which is square root of n.
Calculate square of b as n-pow(a,2).
Calculate b as sqrt(bsquare)
If pow(b,2)==bsquare. Increment count by 1.
At the end of all loops count will have a total number of such pairs.
Return the count as result.
Example
#include <bits/stdc++.h> #include <math.h> using namespace std; int squareSum(int n){ int count = 0; for (int a = 1; a <= sqrt(n); a++){ int bsquare=n - (pow(a,2)); int b = sqrt(bsquare); if(pow(b,2)==bsquare){ count++; cout<<a; } } return count; } int main(){ int N =5; cout <<"Count of pairs of (a,b) where a^2+b^2=N: "<<squareSum(N); return 0; }
Output
If we run the above code it will generate the following output −
Count of pairs of (a,b) where a^2+b^2=N: 122
- Related Articles
- Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N) in C++
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N in C++
- Number of pairs whose sum is a power of 2 in C++
- Minimize the sum of squares of sum of N/2 paired formed by N numbers in C++
- Minimum number of squares whose sum equals to given number n\n
- If \( a \cos \theta+b \sin \theta=m \) and \( a \sin \theta-b \cos \theta=n \), prove that \( a^{2}+b^{2}=m^{2}+n^{2} \)
- The IUPAC name of is(a) 2-ethyl-2-methyl propane(b) 2\n
- Find the $5^{th}$ term of an A.P. of $n$ terms whose sum is $n^2−2n$.
- Evaluate: $\frac{a^{2 n+1} \times a^{(2 n+1)(2 n-1)}}{a^{n(4 n-1)}\times(a^{2})^{2 n+3}}$.
- If $n(A)=30, n(B) = 45, n(A \cup B) = 65$ then find $n(A \cap B)$, $n(A-B)$ and $n(B-A)$.
- Find Cube Pairs - (A n^(2/3) Solution) in C++
- Solve the following pairs of equations:\( \frac{x}{a}+\frac{y}{b}=a+b \)\( \frac{x}{a^{2}}+\frac{y}{b^{2}}=2, a, b ≠ 0 \)
- Count of pairs in an array whose sum is a perfect square in C++
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1
- Program to count number of consecutive lists whose sum is n in C++
