TIP: Click on subject to list as thread! ANSI
echo: science
to: MILES MAXTED
from: DAVID WILLIAMS
date: 2004-08-08 14:23:18
subject: Re: Pythagorean triples

-> Unfortunately, my quick timer addition uses time$ which does not  
-> report parts of seconds;  this version still clocks in at 4  
-> seconds here... 
  
Based on my proof (which I think is solid) that D and E are integers 
for all triples in lowest terms, I realized that if the triple is in 
lowest terms, D and E can have no common factors (except 1) and vice 
versa. So, since D and E are generally smaller than A and B (in fact, D 
is often 1), it should be quicker to use the common-factor test on D 
and E, rather than A and B. 
  
So I made that change, and did a few other little fiddles. I think 
you'll find that this version runs to completion in 3 seconds, or 
possibly even 2! 
  
                             dow 
  
---------------------------------------------------------- 
  
' pythagorean triples 
DEFLNG A-Z 
Mn = 3 ' minimum length of shortest side 
Mx = 10000 ' maximum length of shortest side 
N = 0 
Z# = SQR(2) - 1 
CLS 
FOR A = Mn TO Mx 
  T = A MOD 4 
  SELECT CASE T 
    CASE 0 
      S = A \ 2 
      FOR F = INT(SQR(Z# * S)) TO 1 STEP -1 
        G = S \ F 
        IF G * F = S THEN 
          IF (F XOR G) AND 1 THEN 
            E = F + G 
            D = G - F 
            GOSUB XX 
          END IF 
        END IF 
      NEXT 
    CASE IS  2 
      S = INT(SQR(Z# * A)) 
      FOR D = S - 1 OR 1 TO 1 STEP -2 
        E = A \ D 
        IF E * D = A THEN GOSUB XX 
      NEXT 
  END SELECT 
NEXT 
END 
XX: 
  U = E 
  V = D 
  DO WHILE V > 1 
    W = U MOD V 
    U = V 
    V = W 
  LOOP 
  IF V THEN 
    IF T THEN B = ((E + D) * (E - D)) \ 2 ELSE B = D * E 
    C = (E * E + D * D) \ 2 
    N = N + 1 
    PRINT N, , A, B, C 
  END IF 
RETURN 
  
------------------------------------------------ 
--- Platinum Xpress/Win/WINServer v3.0pr5
* Origin: The Bayman BBS,Toronto, (416)698-6573 - 1:250/514 (1:250/514)
SEEN-BY: 633/267 270
@PATH: 250/514 123/500 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.