RosettaCodeData/Task/Pythagorean-triples/BASIC256/pythagorean-triples.basic
2026-04-30 12:34:36 -04:00

49 lines
909 B
Text

global limite, terna, prim
t1 = msec
print "below ternas primitivas tiempo"
for x = 1 to 7
limite = 10 ^ x
terna = 0
prim = 0
call terna_pit_fast(limite, terna, prim)
print " 10^"; x; " "; ljust(string(terna), 14); ljust(string(prim), 14)
next x
print
print "Tiempo total necesario: "; (msec - t1)/1000; " seg."
end
function gcd(x, y)
while y <> 0
t = y
y = x mod y
x = t
end while
return x
end function
subroutine terna_pit_fast(limite, terna, prim)
for m = 2 to int(sqrt(limite / 2))
for n = 1 to m - 1
# m y n deben ser coprimos y no ambos impares
if (gcd(m, n) = 1) and not ((m mod 2 = 1) and (n mod 2 = 1)) then
a = m * m - n * n
b = 2 * m * n
c = m * m + n * n
p = a + b + c
if p > limite then exit for
prim += 1
x = p
while x <= limite
terna += 1
x = x + p
end while
end if
next n
next m
end subroutine