给定整数N ,任务是计算三元组(a,b,c)的数量,以使a 2 + b 2 = c 2且1≤a≤b≤c≤N 。
例子:
Input: N = 5
Output: 1
The only possible triplet pair is (3, 4, 5)
3^2 + 4^2 = 5^2 i.e. 9 + 16 = 25
Input: N = 10
Output: 2
(3, 4, 5) and (6, 8, 10) are the required triplet pairs.
方法1:
运行两个循环,第一个循环从i = 1到N,第二个循环从j = i + 1到N。考虑每一对,找到i * i + j * j并检查这是否是一个完美的平方且其平方根小于N.如果是,则增加计数。
下面是上述方法的实现:
C++
// C++ program to Find number of
// Triplets 1 <= a <= b<= c <= n,
// Such that a^2 + b^2 = c^2
#include
using namespace std;
// function to ind number of
// Triplets 1 <= a <= b<= c <= n,
// Such that a^2 + b^2 = c^2
int Triplets(int n)
{
// to store required answer
int ans = 0;
// run nested loops for first two numbers.
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
int x = i * i + j * j;
// third number
int y = sqrt(x);
// check if third number is perfect
// square and less than n
if (y * y == x && y <= n)
++ans;
}
}
return ans;
}
// Driver code
int main()
{
int n = 10;
// function call
cout << Triplets(n);
return 0;
}
Java
// Java program to Find number of
// Triplets 1 <= a <= b<= c <= n,
// Such that a^2 + b^2 = c^2
class Solution
{
// function to ind number of
// Triplets 1 <= a <= b<= c <= n,
// Such that a^2 + b^2 = c^2
static int Triplets(int n)
{
// to store required answer
int ans = 0;
// run nested loops for first two numbers.
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
int x = i * i + j * j;
// third number
int y =(int) Math.sqrt(x);
// check if third number is perfect
// square and less than n
if (y * y == x && y <= n)
++ans;
}
}
return ans;
}
// Driver code
public static void main(String args[])
{
int n = 10;
// function call
System.out.println(Triplets(n));
}
}
//contributed by Arnab Kundu
Python3
# Python3 program to Find number of
# Triplets 1 <= a <= b<= c <= n,
# Such that a^2 + b^2 = c^2
import math
# function to ind number of
# Triplets 1 <= a <= b<= c <= n,
# Such that a^2 + b^2 = c^2
def Triplets(n):
# to store required answer
ans = 0
# run nested loops for first two numbers.
for i in range(1, n + 1):
for j in range(i, n + 1):
x = i * i + j * j
# third number
y = int(math.sqrt(x))
# check if third number is perfect
# square and less than n
if (y * y == x and y <= n):
ans += 1
return ans
# Driver code
if __name__ == "__main__":
n = 10
# function call
print(Triplets(n))
# This code is contributed
# by ChitraNayal
C#
// C# program to Find number of
// Triplets 1 <= a <= b<= c <= n,
// Such that a^2 + b^2 = c^2
using System;
class GFG
{
// function to ind number of
// Triplets 1 <= a <= b<= c <= n,
// Such that a^2 + b^2 = c^2
static int Triplets(int n)
{
// to store required answer
int ans = 0;
// run nested loops for first two numbers.
for (int i = 1; i <= n; ++i)
{
for (int j = i; j <= n; ++j)
{
int x = i * i + j * j;
// third number
int y = (int)Math.Sqrt(x);
// check if third number is perfect
// square and less than n
if (y * y == x && y <= n)
++ans;
}
}
return ans;
}
// Driver code
static void Main()
{
int n = 10;
Console.WriteLine(Triplets(n));
}
}
// This code is contributed by ANKITRAI1
PHP
Javascript
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return an Array containing
// all the perfect squares upto n
vector getPerfectSquares(int n)
{
vector perfectSquares;
int current = 1, i = 1;
// While current perfect square
// is less than or equal to n
while (current <= n)
{
perfectSquares.push_back(current);
current = pow(++i, 2);
}
return perfectSquares;
}
// Function to return the count of
// triplet (a, b, c) pairs such that
// a^2 + b^2 = c^2 and
// 1 <= a <= b <= c <= n
int countTriplets(int n)
{
// Vector of perfect squares upto n^2
vector perfectSquares = getPerfectSquares(
pow(n, 2));
int count = 0;
for(int a = 1; a <= n; a++)
{
int aSquare = pow(a, 2);
for(int i = 0; i < perfectSquares.size(); i++)
{
int cSquare = perfectSquares[i];
// Since, a^2 + b^2 = c^2
int bSquare = abs(cSquare - aSquare);
int b = sqrt(bSquare);
int c = sqrt(cSquare);
// If c < a or bSquare is not a
// perfect square
if (c < a || (find(perfectSquares.begin(),
perfectSquares.end(),
bSquare) ==
perfectSquares.end()))
continue;
// If triplet pair (a, b, c) satisfy
// the given condition
if ((b >= a) && (b <= c) &&
(aSquare + bSquare == cSquare))
count++;
}
}
return count;
}
// Driver code
int main()
{
int n = 10;
cout << countTriplets(n);
return 0;
}
// This code is contributed by himanshu77
Java
// Java implementation of the approach
import java.util.*;
public class GFG {
// Function to return an ArrayList containing
// all the perfect squares upto n
public static ArrayList getPerfectSquares(int n)
{
ArrayList perfectSquares = new ArrayList<>();
int current = 1, i = 1;
// while current perfect square is less than or equal to n
while (current <= n) {
perfectSquares.add(current);
current = (int)Math.pow(++i, 2);
}
return perfectSquares;
}
// Function to return the count of triplet (a, b, c) pairs
// such that a^2 + b^2 = c^2 and 1 <= a <= b <= c <= n
public static int countTriplets(int n)
{
// List of perfect squares upto n^2
ArrayList perfectSquares
= getPerfectSquares((int)Math.pow(n, 2));
int count = 0;
for (int a = 1; a <= n; a++) {
int aSquare = (int)Math.pow(a, 2);
for (int i = 0; i < perfectSquares.size(); i++) {
int cSquare = perfectSquares.get(i);
// Since, a^2 + b^2 = c^2
int bSquare = cSquare - aSquare;
int b = (int)Math.sqrt(bSquare);
int c = (int)Math.sqrt(cSquare);
// If c < a or bSquare is not a perfect square
if (c < a || !perfectSquares.contains(bSquare))
continue;
// If triplet pair (a, b, c) satisfy the given condition
if ((b >= a) && (b <= c) && (aSquare + bSquare == cSquare))
count++;
}
}
return count;
}
// Driver code
public static void main(String[] args)
{
int n = 10;
System.out.println(countTriplets(n));
}
}
Python3
# Python3 implementation of the approach
import math
# Function to return an ArrayList containing
# all the perfect squares upto n
def getPerfectSquares(n):
perfectSquares = []
current = 1
i = 1
# while current perfect square is less than or equal to n
while (current <= n) :
perfectSquares.append(current)
i += 1
current = i** 2
return perfectSquares
# Function to return the count of triplet (a, b, c) pairs
# such that a^2 + b^2 = c^2 and 1 <= a <= b <= c <= n
def countTriplets(n):
# List of perfect squares upto n^2
perfectSquares= getPerfectSquares(n**2)
count = 0
for a in range(1, n +1 ):
aSquare = a**2
for i in range(len(perfectSquares)):
cSquare = perfectSquares[i]
# Since, a^2 + b^2 = c^2
bSquare = abs(cSquare - aSquare)
b = math.sqrt(bSquare)
b = int(b)
c = math.sqrt(cSquare)
c = int(c)
# If c < a or bSquare is not a perfect square
if (c < a or (bSquare not in perfectSquares)):
continue
# If triplet pair (a, b, c) satisfy the given condition
if ((b >= a) and (b <= c) and (aSquare + bSquare == cSquare)):
count += 1
return count
# Driver code
if __name__ == "__main__":
n = 10
print(countTriplets(n))
# This code is contributed by chitranayal
C#
// C# implementation of the approach
using System.Collections;
using System;
class GFG
{
// Function to return an ArrayList containing
// all the perfect squares upto n
public static ArrayList getPerfectSquares(int n)
{
ArrayList perfectSquares = new ArrayList();
int current = 1, i = 1;
// while current perfect square is less
// than or equal to n
while (current <= n)
{
perfectSquares.Add(current);
current = (int)Math.Pow(++i, 2);
}
return perfectSquares;
}
// Function to return the count of triplet
// (a, b, c) pairs such that a^2 + b^2 = c^2
// and 1 <= a <= b <= c <= n
public static int countTriplets(int n)
{
// List of perfect squares upto n^2
ArrayList perfectSquares = getPerfectSquares((int)Math.Pow(n, 2));
int count = 0;
for (int a = 1; a <= n; a++)
{
int aSquare = (int)Math.Pow(a, 2);
for (int i = 0; i < perfectSquares.Count; i++)
{
int cSquare = (int)perfectSquares[i];
// Since, a^2 + b^2 = c^2
int bSquare = cSquare - aSquare;
int b = (int)Math.Sqrt(bSquare);
int c = (int)Math.Sqrt(cSquare);
// If c < a or bSquare is not a perfect square
if (c < a || !perfectSquares.Contains(bSquare))
continue;
// If triplet pair (a, b, c) satisfy
// the given condition
if ((b >= a) && (b <= c) &&
(aSquare + bSquare == cSquare))
count++;
}
}
return count;
}
// Driver code
public static void Main()
{
int n = 10;
Console.WriteLine(countTriplets(n));
}
}
// This code is contributed by mits.
Javascript
输出:
2
方法2:
- 查找直到n 2的所有理想平方并将其保存到ArrayList中。
- 现在,对于从1到n的每个a ,执行以下操作:
- 从先前计算的理想平方的列表中选择c 2 。
- 然后,可以将b 2计算为b 2 = c 2 – a 2 。
- 现在检查上一步中计算的a <= b <= c和b 2是否必须是理想平方。
- 如果满足以上条件,则增加计数。
- 最后打印计数。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return an Array containing
// all the perfect squares upto n
vector getPerfectSquares(int n)
{
vector perfectSquares;
int current = 1, i = 1;
// While current perfect square
// is less than or equal to n
while (current <= n)
{
perfectSquares.push_back(current);
current = pow(++i, 2);
}
return perfectSquares;
}
// Function to return the count of
// triplet (a, b, c) pairs such that
// a^2 + b^2 = c^2 and
// 1 <= a <= b <= c <= n
int countTriplets(int n)
{
// Vector of perfect squares upto n^2
vector perfectSquares = getPerfectSquares(
pow(n, 2));
int count = 0;
for(int a = 1; a <= n; a++)
{
int aSquare = pow(a, 2);
for(int i = 0; i < perfectSquares.size(); i++)
{
int cSquare = perfectSquares[i];
// Since, a^2 + b^2 = c^2
int bSquare = abs(cSquare - aSquare);
int b = sqrt(bSquare);
int c = sqrt(cSquare);
// If c < a or bSquare is not a
// perfect square
if (c < a || (find(perfectSquares.begin(),
perfectSquares.end(),
bSquare) ==
perfectSquares.end()))
continue;
// If triplet pair (a, b, c) satisfy
// the given condition
if ((b >= a) && (b <= c) &&
(aSquare + bSquare == cSquare))
count++;
}
}
return count;
}
// Driver code
int main()
{
int n = 10;
cout << countTriplets(n);
return 0;
}
// This code is contributed by himanshu77
Java
// Java implementation of the approach
import java.util.*;
public class GFG {
// Function to return an ArrayList containing
// all the perfect squares upto n
public static ArrayList getPerfectSquares(int n)
{
ArrayList perfectSquares = new ArrayList<>();
int current = 1, i = 1;
// while current perfect square is less than or equal to n
while (current <= n) {
perfectSquares.add(current);
current = (int)Math.pow(++i, 2);
}
return perfectSquares;
}
// Function to return the count of triplet (a, b, c) pairs
// such that a^2 + b^2 = c^2 and 1 <= a <= b <= c <= n
public static int countTriplets(int n)
{
// List of perfect squares upto n^2
ArrayList perfectSquares
= getPerfectSquares((int)Math.pow(n, 2));
int count = 0;
for (int a = 1; a <= n; a++) {
int aSquare = (int)Math.pow(a, 2);
for (int i = 0; i < perfectSquares.size(); i++) {
int cSquare = perfectSquares.get(i);
// Since, a^2 + b^2 = c^2
int bSquare = cSquare - aSquare;
int b = (int)Math.sqrt(bSquare);
int c = (int)Math.sqrt(cSquare);
// If c < a or bSquare is not a perfect square
if (c < a || !perfectSquares.contains(bSquare))
continue;
// If triplet pair (a, b, c) satisfy the given condition
if ((b >= a) && (b <= c) && (aSquare + bSquare == cSquare))
count++;
}
}
return count;
}
// Driver code
public static void main(String[] args)
{
int n = 10;
System.out.println(countTriplets(n));
}
}
Python3
# Python3 implementation of the approach
import math
# Function to return an ArrayList containing
# all the perfect squares upto n
def getPerfectSquares(n):
perfectSquares = []
current = 1
i = 1
# while current perfect square is less than or equal to n
while (current <= n) :
perfectSquares.append(current)
i += 1
current = i** 2
return perfectSquares
# Function to return the count of triplet (a, b, c) pairs
# such that a^2 + b^2 = c^2 and 1 <= a <= b <= c <= n
def countTriplets(n):
# List of perfect squares upto n^2
perfectSquares= getPerfectSquares(n**2)
count = 0
for a in range(1, n +1 ):
aSquare = a**2
for i in range(len(perfectSquares)):
cSquare = perfectSquares[i]
# Since, a^2 + b^2 = c^2
bSquare = abs(cSquare - aSquare)
b = math.sqrt(bSquare)
b = int(b)
c = math.sqrt(cSquare)
c = int(c)
# If c < a or bSquare is not a perfect square
if (c < a or (bSquare not in perfectSquares)):
continue
# If triplet pair (a, b, c) satisfy the given condition
if ((b >= a) and (b <= c) and (aSquare + bSquare == cSquare)):
count += 1
return count
# Driver code
if __name__ == "__main__":
n = 10
print(countTriplets(n))
# This code is contributed by chitranayal
C#
// C# implementation of the approach
using System.Collections;
using System;
class GFG
{
// Function to return an ArrayList containing
// all the perfect squares upto n
public static ArrayList getPerfectSquares(int n)
{
ArrayList perfectSquares = new ArrayList();
int current = 1, i = 1;
// while current perfect square is less
// than or equal to n
while (current <= n)
{
perfectSquares.Add(current);
current = (int)Math.Pow(++i, 2);
}
return perfectSquares;
}
// Function to return the count of triplet
// (a, b, c) pairs such that a^2 + b^2 = c^2
// and 1 <= a <= b <= c <= n
public static int countTriplets(int n)
{
// List of perfect squares upto n^2
ArrayList perfectSquares = getPerfectSquares((int)Math.Pow(n, 2));
int count = 0;
for (int a = 1; a <= n; a++)
{
int aSquare = (int)Math.Pow(a, 2);
for (int i = 0; i < perfectSquares.Count; i++)
{
int cSquare = (int)perfectSquares[i];
// Since, a^2 + b^2 = c^2
int bSquare = cSquare - aSquare;
int b = (int)Math.Sqrt(bSquare);
int c = (int)Math.Sqrt(cSquare);
// If c < a or bSquare is not a perfect square
if (c < a || !perfectSquares.Contains(bSquare))
continue;
// If triplet pair (a, b, c) satisfy
// the given condition
if ((b >= a) && (b <= c) &&
(aSquare + bSquare == cSquare))
count++;
}
}
return count;
}
// Driver code
public static void Main()
{
int n = 10;
Console.WriteLine(countTriplets(n));
}
}
// This code is contributed by mits.
Java脚本
输出:
2
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。