How to resolve the algorithm Pythagorean triples step by step in the Quackery programming language
How to resolve the algorithm Pythagorean triples step by step in the Quackery 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 Quackery programming language
Source code in the quackery programming language
[ dup 0 peek
swap 2 peek
2dup + 2dup +
join join join ] is f1 ( [ --> [ )
[ dup 3 peek
swap 1 peek
2dup + 2dup +
join join join ] is f2 ( [ --> [ )
[ dup 3 peek
swap 2 peek
2dup + 2dup +
join join join ] is f3 ( [ --> [ )
[ do over + tuck + rot * unrot * + ] is perimeter ( [ --> n )
[ stack ] is limit ( --> s )
[ stack ] is primitives ( --> s )
[ stack ] is triples ( --> s )
[ limit put
0 primitives put
0 triples put
' [ 1 1 2 3 ]
[ dup perimeter
limit share over < iff 2drop done
1 primitives tally
limit share swap / triples tally
dup f1 recurse
dup f2 recurse
f3 again ]
say "Pythagorean triples, perimeter < "
limit take echo
say ": "
triples take echo
say ", of which "
primitives take echo
say " are primitive." cr ] is task ( n --> )
7 times [ 10 i^ 2 + ** task ]
You may also check:How to resolve the algorithm Lah numbers step by step in the Prolog programming language
You may also check:How to resolve the algorithm Empty string step by step in the Java programming language
You may also check:How to resolve the algorithm Halt and catch fire step by step in the Rust programming language
You may also check:How to resolve the algorithm Here document step by step in the Frink programming language
You may also check:How to resolve the algorithm Loops/Wrong ranges step by step in the ALGOL W programming language