
- 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 number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1<=a<=b<=c<= n in C++
We are given an integer n. The goal is to find triplets ( set of 3 numbers ) that satisfy the conditions −
a2+b2=c2
1<=a<=b<=c<=n
We will do this by running two loops for values of 1<=a<=n and 1<=b<=n. Calculate c accordingly (c=sqrt(a2+b2 )) and increment count if both conditions 1 and 2 are met.
Let’s understand with examples.
Input − N=5
Output − Number of triplets − 1
Explanation −
for a=3, b=4 and c=5 both conditions are met.
Input − N=3
Output − Number of triplets − 0
Explanation −
No such triplets that meet conditions 1 and 2.
Approach used in the below program is as follows
Integer N stores the last limit of the range [1,N].
Function countTriplets(int n) takes n and returns the count of triplets which satisfy the conditions a2+b2=c2 and 1<=a<=b<=c<=n
Variable count stores the number of such triplets, initially 0.
Variable sum stores the sum of squares of a and b.
Starting from a=1 to n and b=a to n, calculate sum=a*a+b*b and c as square root of sum (sqrt(sum)).
If calculated c has value such that c*c==sum and b<=c && c<=n ( both condition 1 and 2 are met ).
Increment count as current a,b,c satisfy both conditions.
Perform this until a=n and b=n. In the end, count will have a number of such triplets that satisfy the conditions.
Return the count as desired result.
Example
#include <bits/stdc++.h> using namespace std; int countTriplets(int n){ int count = 0; int a,b,c; a=b=c=1; int sum=0; for (a = 1; a <= n; a++) //1<=a<=n{ for (b = a; b <= n; b++) //1<=a<=b<=n{ sum = a*a + b*b; //a^2 + b^2 =c^2 c = sqrt(sum); if (c * c == sum && b<=c && c<=n) //check 1<=a<=b<=c<=n{ count++; cout<<endl<<"a :"<<a<<" b :"<<b<<" c :"<<c; //to print triplets } } } return count; } int main(){ int N = 15; cout <<endl<< "Number of triplets : "<<countTriplets(N); return 0; }
Output
If we run the above code it will generate the following output −
Number of triplets : a :3 b :4 c :5 a :5 b :12 c :13 a :6 b :8 c :10 a :9 b :12 c :154 Number of triplets : 4
- Related Articles
- Factorize:$(a – b + c)^2 + (b – c + a)^2 + 2(a – b + c) (b – c + a)$
- Simplify:$(a + b + c)^2 + (a - b + c)^2 + (a + b - c)^2$
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N) in C++
- Prove that:\( \left(\frac{x^{a}}{x^{b}}\right)^{a^{2}+a b+b^{2}} \times\left(\frac{x^{b}}{x^{c}}\right)^{b^{2}+b c+c^{2}} \times\left(\frac{x^{c}}{x^{a}}\right)^{c^{2}+c a+a^{2}}=1 \)
- Prove that:\( \left(\frac{x^{a}}{x^{-b}}\right)^{a^{2}-a b+b^{2}} \times\left(\frac{x^{b}}{x^{-c}}\right)^{b^{2}-b c+c^{2}} \times\left(\frac{x^{c}}{x^{-a}}\right)^{c^{2}-c a+a^{2}}=1 \)
- Count the triplets such that A[i] < B[j] < C[k] in C++
- Simplify:$(a + b + c)^2 + (a - b + c)^2$
- Simplify:$(a + b + c)^2 - (a - b + c)^2$
- If \( a+b=5 \) and \( a b=2 \), find the value of(a) \( (a+b)^{2} \)(b) \( a^{2}+b^{2} \)(c) \( (a-b)^{2} \)
- Show that:\( \left(\frac{x^{a^{2}+b^{2}}}{x^{a b}}\right)^{a+b}\left(\frac{x^{b^{2}+c^{2}}}{x^{b c}}\right)^{b+c}\left(\frac{x^{c^{2}+a^{2}}}{x^{a c}}\right)^{a+c}= x^{2\left(a^{3}+b^{3}+c^{3}\right)} \)
- If $a ≠ b ≠ c$, prove that the points $(a, a^2), (b, b^2), (c, c^2)$ can never be collinear.
- In a quadrilateral \( A B C D, \angle B=90^{\circ}, A D^{2}=A B^{2}+B C^{2}+C D^{2}, \) prove that $\angle A C D=90^o$.
- Factorize:\( 6 a b-b^{2}+12 a c-2 b c \)
- Verify that $a ÷ (b+c) ≠ (a ÷ b) + (a ÷ c)$ for each of the following values of $a,\ b$ and $c$.(a) $a=12,\ b=- 4,\ c=2$(b) $a=(-10),\ b = 1,\ c = 1$
- Prove that \( \frac{a^{-1}}{a^{-1}+b^{-1}}+\frac{a^{-1}}{a^{-1}-b^{-1}}=\frac{2 b^{2}}{b^{2}-a^{2}} \)
