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

-> G'morning David,  
 
->  DW> Right. With the PRINT disabled, it gets to that solution, on my 
->  DW> machine, in about six seconds! 
 
-> Mmmm - with PRINTing, qbasic `92 running under WK2 Prof on this  
-> 550CDT, it times itself at 4 seconds. 
 
->  DW> Can you, or Jasen, or anyone 
->  DW> else, come up with a proof? A couple of formulae that would
let D and E 
->  DW> be calculated from A, B and C in "integer"
arithmetic (i.e. no 
->  DW> square-roots, etc.) would constitute a good proof. 
 
-> I'll sit down with paper & pen later today, fiddle with the  
-> formula and see what stumbles out ... but don't hold your breath -  
-> Jason's `empirical' effort has me a little baffled! 
 
-> :-) 
  
Good evening, Miles: 
  
Yes. Jasen's thing is unintelligible, even to him! But I haven't looked 
at the theory behind it really hard... yet. 
 
->  DW> Right. With the PRINT disabled, it gets to that solution, on my 
->  DW> machine, in about six seconds! 
 
-> Mmmm - with PRINTing, qbasic `92 running under WK2 Prof on this  
-> 550CDT, it times itself at 4 seconds. 
  
Your machine is obviously faster than mine! There are probably plenty 
that can do it in less than ONE second! 
  
Incidentally, I've made a little discovery. There was a line in the 
program that read: 
  
FOR D = INT(SQR(Z# * A)) - 1 OR 1 TO 1 STEP -2 
  
In the latest version, I've replaced it with two lines: 
  
S = INT(SQR(Z# * A)) 
FOR D = S - 1 OR 1 TO 1 STEP -2 
  
This second version turns out to be considerably faster! Why? Because 
the "-1 OR 1" operations are done in integer arithmetic. S is an 
integer variable, so BASIC "sees" that no floating-point variables are 
involved, and just uses integer operations. But the first version has 
floating-point numbers in the calculation, so *everything* is done in 
floating-point, which is much slower. 
  
Incidentally, the "S - 1 OR 1" expression finds the greatest ODD number 
that is less than or equal to S. I think that's about the easiest way 
to do it. 
  
Here's the latest. Not many changes. 
  
                                  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 
  SELECT CASE A MOD 4 
    CASE 0 
      S = A \ 2 
      FOR F = INT(SQR(Z# * S)) TO 1 STEP -1 
        G = S \ F 
        IF G * F = S THEN 
          E = F + G 
          IF E MOD 2 THEN 
            D = G - F 
            B = D * E 
            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 
          B = ((E + D) * (E - D)) \ 2 
          GOSUB XX 
        END IF 
      NEXT 
  END SELECT 
NEXT 
END 
XX: 
  U = B 
  V = A 
  DO WHILE V > 1 
    W = U MOD V 
    U = V 
    V = W 
  LOOP 
  IF V THEN 
    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™.