2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,28 +1,29 @@
/*REXX program counts number of Pythagorean triples that exist given a max */
/*────────── perimeter of N, and also counts how many of them are primitives.*/
trips=0; prims=0 /*set the number of triples, primitives*/
parse arg N .; if N=='' then n=100 /*N specified? No, then use default. */
/*REXX program counts the number of Pythagorean triples that exist given a maximum */
/*──────────────────── perimeter of N, and also counts how many of them are primitives.*/
trips=0; prims=0 /*set the number of triples, primitives*/
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then n=100 /*Not specified? Then use the default.*/
do a=3 to N%3; aa=a*a /*limit side to 1/3 of the perimeter.*/
do a=3 to N%3; aa=a*a /*limit side to 1/3 of the perimeter.*/
do b=a+1 /*the triangle can't be isosceles. */
ab=a+b /*compute a partial perimeter (2 sides)*/
if ab>=N then iterate a /*is a+b ≥ perimeter? Try different A*/
aabb=aa+b*b /*compute the sum of a²+b² (shortcut)*/
do b=a+1 /*the triangle can't be isosceles. */
ab=a + b /*compute a partial perimeter (2 sides)*/
if ab>=N then iterate a /*is a+b ≥ perimeter? Try different A*/
aabb=aa + b*b /*compute the sum of a²+b² (shortcut)*/
do c=b+1 /*compute the value of the third side. */
if ab+c>N then iterate a /*is a+b+c > perimeter? Try diff. A.*/
cc=c*c /*compute the value of C². */
if cc > aabb then iterate b /*is c² > a²+b² ? Try a different B.*/
if cc\==aabb then iterate /*is c² ¬= a²+b² ? Try a different C.*/
trips=trips+1 /*eureka. We found a Pythagorean triple*/
prims=prims+(gcd(a,b)==1) /*is this triple a primitive triple? */
do c=b+1 /*compute the value of the third side. */
if ab + c > N then iterate a /*is a+b+c > perimeter? Try diff. A.*/
cc=c*c /*compute the value of C². */
if cc > aabb then iterate b /*is c² > a²+b² ? Try a different B.*/
if cc\==aabb then iterate /*is c² ¬= a²+b² ? Try a different C.*/
trips=trips + 1 /*eureka. We found a Pythagorean triple*/
prims=prims + (gcd(a,b)==1) /*is this triple a primitive triple? */
end /*c*/
end /*b*/
end /*a*/
_=left('',7) /*for padding the output with 7 blanks.*/
say 'max perimeter =' N _ "Pythagorean triples =" trips _ 'primitives =' prims
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────────────*/
gcd: procedure; parse arg x,y; do until y==0; parse value x//y y with y x; end; return x
_=left('', 7) /*for padding the output with 7 blanks.*/
say 'max perimeter =' N _ "Pythagorean triples =" trips _ 'primitives =' prims
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
gcd: procedure; parse arg x,y; do until y==0; parse value x//y y with y x; end; return x

View file

@ -1,36 +1,36 @@
/*REXX program counts number of Pythagorean triples that exist given a max */
/*REXX program counts number of Pythagorean triples that exist given a max */
/*────────── perimeter of N, and also counts how many of them are primitives.*/
@.=0; trips=0; prims=0 /*define some REXX variables to zero. */
parse arg N .; if N=='' then n=100 /*N specified? No, then use default. */
/*REXX program counts the number of Pythagorean triples that exist given a maximum */
/*──────────────────── perimeter of N, and also counts how many of them are primitives.*/
@.=0; trips=0; prims=0 /*define some REXX variables to zero. */
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then n=100 /*Not specified? Then use the default.*/
do a=3 to N%3; aa=a*a /*limit side to 1/3 of the perimeter.*/
aEven= a//2==0 /*set variable to 1 if A is even. */
do a=3 to N%3; aa=a*a /*limit side to 1/3 of the perimeter.*/
aEven= a//2==0 /*set variable to 1 if A is even. */
do b=a+1 by 1+aEven /*the triangle can't be isosceles. */
ab=a+b /*compute a partial perimeter (2 sides)*/
if ab>=N then iterate a /*is a+b ≥ perimeter? Try different A*/
aabb=aa+b*b /*compute the sum of a²+b² (shortcut)*/
do b=a+1 by 1+aEven /*the triangle can't be isosceles. */
ab=a + b /*compute a partial perimeter (2 sides)*/
if ab>=N then iterate a /*is a+b ≥ perimeter? Try different A*/
aabb=aa + b*b /*compute the sum of a²+b² (shortcut)*/
do c=b+1 /*compute the value of the third side. */
do c=b + 1 /*compute the value of the third side. */
if aEven then if c//2==0 then iterate
if ab+c>n then iterate a /*a+b+c > perimeter? Try different A.*/
cc=c*c /*compute the value of C². */
if cc > aabb then iterate b /*is c² > a²+b² ? Try a different B.*/
if cc\==aabb then iterate /*is c² ¬= a²+b² ? Try a different C.*/
if @.a.b.c then iterate /*Is this a duplicate? Then try again.*/
trips=trips+1 /*Eureka! We found a Pythagorean triple*/
prims=prims+1 /*count this also as a primitive triple*/
if ab+c>n then iterate a /*a+b+c > perimeter? Try different A.*/
cc=c*c /*compute the value of C². */
if cc > aabb then iterate b /*is c² > a²+b² ? Try a different B.*/
if cc\==aabb then iterate /*is c² ¬= a²+b² ? Try a different C.*/
if @.a.b.c then iterate /*Is this a duplicate? Then try again.*/
trips=trips + 1 /*Eureka! We found a Pythagorean triple*/
prims=prims + 1 /*count this also as a primitive triple*/
do m=2; am=a*m; bm=b*m; cm=c*m /*generate non-primitives.*/
if am+bm+cm>N then leave /*is this multiple Pythagorean triple? */
trips=trips+1 /*Eureka! We found a Pythagorean triple*/
@.am.bm.cm=1 /*mark Pythagorean triangle as a triple*/
do m=2; am=a*m; bm=b*m; cm=c*m /*generate non-primitives Pythagoreans.*/
if am+bm+cm>N then leave /*is this multiple Pythagorean triple? */
trips=trips+1 /*Eureka! We found a Pythagorean triple*/
@.am.bm.cm=1 /*mark Pythagorean triangle as a triple*/
end /*m*/
end /*c*/
end /*b*/
end /*a*/
_=left('',7) /*for padding the output with 7 blanks.*/
say 'max perimeter =' N _ "Pythagorean triples =" trips _ 'primitives =' prims
/*stick a fork in it, we're all done. */
_=left('', 7) /*for padding the output with 7 blanks.*/
say 'max perimeter =' N _ "Pythagorean triples =" trips _ 'primitives =' prims
/*stick a fork in it, we're all done. */