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,12 +1,22 @@
A [[wp:Pythagorean_triple|Pythagorean triple]] is defined as three positive integers <math>(a, b, c)</math> where <math>a < b < c</math>, and <math>a^2+b^2=c^2.</math> They are called primitive triples if <math>a, b, c</math> are coprime, that is, if their pairwise greatest common divisors <math>{\rm gcd}(a, b) = {\rm gcd}(a, c) = {\rm gcd}(b, c) = 1</math>. Because of their relationship through the Pythagorean theorem, a, b, and c are coprime if a and b are coprime (<math>{\rm gcd}(a, b) = 1</math>). Each triple forms the length of the sides of a right triangle, whose perimeter is <math>P=a+b+c</math>.
A [[wp:Pythagorean_triple|Pythagorean triple]] is defined as three positive integers <math>(a, b, c)</math> where <math>a < b < c</math>, and <math>a^2+b^2=c^2.</math>
'''Task'''
They are called primitive triples if <math>a, b, c</math> are co-prime, that is, if their pairwise greatest common divisors <math>{\rm gcd}(a, b) = {\rm gcd}(a, c) = {\rm gcd}(b, c) = 1</math>.
Because of their relationship through the Pythagorean theorem, a, b, and c are co-prime if a and b are co-prime (<math>{\rm gcd}(a, b) = 1</math>). &nbsp;
Each triple forms the length of the sides of a right triangle, whose perimeter is <math>P=a+b+c</math>.
;Task:
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.
'''Extra credit:''' Deal with large values. Can your program handle a max 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.
;Extra credit:
Deal with large values. &nbsp; Can your program handle a maximum perimeter of 1,000,000? &nbsp; What about 10,000,000? &nbsp; 100,000,000?
Note: the extra credit is not for you to demonstrate how fast your language is compared to others; &nbsp; you need a proper algorithm to solve them in a timely manner.
;Cf:
* [[List comprehensions]]
<br><br>

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. */

View file

@ -0,0 +1,11 @@
1 LET Y=0: LET X=0: LET Z=0: LET V=0: LET U=0: LET L=10: LET T=0: LET P=0: LET N=4: LET M=0: PRINT "limit trip. prim."
2 FOR U=2 TO INT (SQR (L/2)): LET Y=U-INT (U/2)*2: LET N=N+4: LET M=U*U*2: IF Y=0 THEN LET M=M-U-U
3 FOR V=1+Y TO U-1 STEP 2: LET M=M+N: LET X=U: LET Y=V
4 LET Z=Y: LET Y=X-INT (X/Y)*Y: LET X=Z: IF Y<>0 THEN GO TO 4
5 IF X>1 THEN GO TO 8
6 IF M>L THEN GO TO 9
7 LET P=P+1: LET T=T+INT (L/M)
8 NEXT V
9 NEXT U
10 PRINT L;TAB 8;T;TAB 16;P
11 LET N=4: LET T=0: LET P=0: LET L=L*10: IF L<=100000 THEN GO TO 2