How to resolve the algorithm Pythagorean triples step by step in the Ruby programming language
How to resolve the algorithm Pythagorean triples step by step in the Ruby 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 Ruby programming language
Class Definition:
The code defines a class named PythagoranTriplesCounter
that generates Pythagorean triples up to a specified limit and counts them.
Instance Variables:
@limit
: The upper bound for the perimeter of the generated Pythagorean triples.@total
: The total number of Pythagorean triples within the limit.@primitives
: The number of primitive Pythagorean triples (i.e., triples with no common factors).
Constructor:
initialize(limit)
: Initializes thePythagoranTriplesCounter
with the desired perimeter limit.
Public Attributes:
total
: Returns the total number of Pythagorean triples generated within the limit.primitives
: Returns the number of primitive Pythagorean triples generated.
Private Methods:
generate_triples(a, b, c)
: Generates Pythagorean triples by iteratively applying the following formulas:(a-2*b+2*c, 2*a-b+2*c, 2*a-2*b+3*c)
(a+2*b+2*c, 2*a+b+2*c, 2*a+2*b+3*c)
(-a+2*b+2*c, -2*a+b+2*c, -2*a+2*b+3*c)
Main Program:
- Initializes a perimeter variable
perim
with a value of 10. - Creates a loop that continues until
perim
exceeds 100,000,000. - Inside the loop:
- Creates a
PythagoranTriplesCounter
instance with the current perimeter limit. - Prints the perimeter, total number of triples, and number of primitive triples generated.
- Multiplies
perim
by 10 to increase the perimeter limit for the next iteration.
- Creates a
Source code in the ruby programming language
class PythagoranTriplesCounter
def initialize(limit)
@limit = limit
@total = 0
@primitives = 0
generate_triples(3, 4, 5)
end
attr_reader :total, :primitives
private
def generate_triples(a, b, c)
perim = a + b + c
return if perim > @limit
@primitives += 1
@total += @limit / perim
generate_triples( a-2*b+2*c, 2*a-b+2*c, 2*a-2*b+3*c)
generate_triples( a+2*b+2*c, 2*a+b+2*c, 2*a+2*b+3*c)
generate_triples(-a+2*b+2*c,-2*a+b+2*c,-2*a+2*b+3*c)
end
end
perim = 10
while perim <= 100_000_000
c = PythagoranTriplesCounter.new perim
p [perim, c.total, c.primitives]
perim *= 10
end
You may also check:How to resolve the algorithm Copy a string step by step in the RLaB programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the Frink programming language
You may also check:How to resolve the algorithm String length step by step in the Ring programming language
You may also check:How to resolve the algorithm Pinstripe/Display step by step in the Delphi programming language
You may also check:How to resolve the algorithm Abbreviations, automatic step by step in the Yabasic programming language