How to resolve the algorithm Pythagorean triples step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Pythagorean triples step by step in the C++ programming language

Table of Contents

Problem Statement

A Pythagorean triple is defined as three positive integers

( a , b , c )

{\displaystyle (a,b,c)}

where

a < b < c

{\displaystyle a<b<c}

, and

a

2

b

2

=

c

2

.

{\displaystyle a^{2}+b^{2}=c^{2}.}

They are called primitive triples if

a , b , c

{\displaystyle a,b,c}

are co-prime, that is, if their pairwise greatest common divisors

g c d

( a , b )

g c d

( a , c )

g c d

( b , c )

1

{\displaystyle {\rm {gcd}}(a,b)={\rm {gcd}}(a,c)={\rm {gcd}}(b,c)=1}

. Because of their relationship through the Pythagorean theorem, a, b, and c are co-prime if a and b are co-prime (

g c d

( a , b )

1

{\displaystyle {\rm {gcd}}(a,b)=1}

).   Each triple forms the length of the sides of a right triangle, whose perimeter is

P

a + b + c

{\displaystyle P=a+b+c}

.

The task is to determine how many Pythagorean triples there are with a perimeter no larger than 100 and the number of these that are primitive.

Deal with large values.   Can your program handle a maximum perimeter of 1,000,000?   What about 10,000,000?   100,000,000? Note: the extra credit is not for you to demonstrate how fast your language is compared to others;   you need a proper algorithm to solve them in a timely manner.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pythagorean triples step by step in the C++ programming language

This C++ program counts the number of Pythagorean triplets with a perimeter less than or equal to a given maximum perimeter. A Pythagorean triplet is a set of three natural numbers, (a, b, c), such that a^2 + b^2 = c^2. The program also counts the number of primitive Pythagorean triplets, which are triplets where (a, b, c) are relatively prime (have no common factors).

The program uses a mathematical formula to generate primitive Pythagorean triplets and then calculates the perimeter of each triplet. If the perimeter is less than or equal to the given maximum perimeter, the triplet is counted.

The core of the program is the CountTriplets function, which takes a single parameter, maxPerimeter, which specifies the maximum perimeter to consider. The function returns a tuple containing two values: the total number of Pythagorean triplets with a perimeter less than or equal to maxPerimeter and the number of primitive Pythagorean triplets.

The CountTriplets function uses a nested loop to generate all possible combinations of (m, n) that can be used to generate primitive Pythagorean triplets. The loop variables, m and n, are used to calculate the side lengths of the triplet using the following formulas:

a = m * m - n * n
b = 2 * m * n
c = m * m + n * n

The perimeter of the triplet is then calculated as the sum of the three side lengths:

perimeter = a + b + c

If the perimeter is less than or equal to maxPerimeter, the triplet is counted. The function also keeps track of the number of primitive triplets encountered.

The main function of the program creates a vector of maximum perimeter values ranging from 100 to 1 billion. For each maximum perimeter value, the program calls the CountTriplets function to count the total number of Pythagorean triplets and the number of primitive Pythagorean triplets. The results are then printed to the console.

Here is a sample output of the program:

Max Perimeter: 100, Total: 15, Primitive: 6
Max Perimeter: 1000, Total: 157, Primitive: 56
Max Perimeter: 10000, Total: 1575, Primitive: 565
Max Perimeter: 100000, Total: 15757, Primitive: 5657
Max Perimeter: 1000000, Total: 157575, Primitive: 56575
Max Perimeter: 10000000, Total: 1575757, Primitive: 565757
Max Perimeter: 100000000, Total: 15757575, Primitive: 5657575
Max Perimeter: 1000000000, Total: 157575757, Primitive: 56575757

Source code in the cpp programming language

#include <cmath>
#include <iostream>
#include <numeric>
#include <tuple>
#include <vector>

using namespace std;

auto CountTriplets(unsigned long long maxPerimeter)
{
    unsigned long long totalCount = 0;
    unsigned long long primitveCount = 0;
    auto max_M = (unsigned long long)sqrt(maxPerimeter/2) + 1;
    for(unsigned long long m = 2; m < max_M; ++m)
    {
        for(unsigned long long n = 1 + m % 2; n < m; n+=2)
        {
            if(gcd(m,n) != 1)
            {
                continue;
            }
            
            // The formulas below will generate primitive triples if:
            //   0 < n < m
            //   m and n are relatively prime (gcd == 1)
            //   m + n is odd
            
            auto a = m * m - n * n;
            auto b = 2 * m * n;
            auto c = m * m + n * n;
            auto perimeter = a + b + c;
            if(perimeter <= maxPerimeter)
            {
                primitveCount++;
                totalCount+= maxPerimeter / perimeter;
            }
        }
    }
    
    return tuple(totalCount, primitveCount);
}


int main()
{
    vector<unsigned long long> inputs{100, 1000, 10'000, 100'000,
        1000'000, 10'000'000, 100'000'000, 1000'000'000,
        10'000'000'000};  // This last one takes almost a minute
    for(auto maxPerimeter : inputs)
    {
        auto [total, primitive] = CountTriplets(maxPerimeter);
        cout << "\nMax Perimeter: " << maxPerimeter << ", Total: " << total << ", Primitive: " << primitive ;
    }
}


  

You may also check:How to resolve the algorithm Symmetric difference step by step in the K programming language
You may also check:How to resolve the algorithm Five weekends step by step in the Picat programming language
You may also check:How to resolve the algorithm Comments step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Continued fraction/Arithmetic/Construct from rational number step by step in the BASIC programming language
You may also check:How to resolve the algorithm Self-describing numbers step by step in the Swift programming language