Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
33
Task/Pythagorean-triples/Ada/pythagorean-triples.adb
Normal file
33
Task/Pythagorean-triples/Ada/pythagorean-triples.adb
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Pythagorean_Triples is
|
||||
|
||||
type Large_Natural is range 0 .. 2**63-1;
|
||||
-- this is the maximum for gnat
|
||||
|
||||
procedure New_Triangle(A, B, C: Large_Natural;
|
||||
Max_Perimeter: Large_Natural;
|
||||
Total_Cnt, Primitive_Cnt: in out Large_Natural) is
|
||||
Perimeter: constant Large_Natural := A + B + C;
|
||||
begin
|
||||
if Perimeter <= Max_Perimeter then
|
||||
Primitive_Cnt := Primitive_Cnt + 1;
|
||||
Total_Cnt := Total_Cnt + Max_Perimeter / Perimeter;
|
||||
New_Triangle(A-2*B+2*C, 2*A-B+2*C, 2*A-2*B+3*C, Max_Perimeter, Total_Cnt, Primitive_Cnt);
|
||||
New_Triangle(A+2*B+2*C, 2*A+B+2*C, 2*A+2*B+3*C, Max_Perimeter, Total_Cnt, Primitive_Cnt);
|
||||
New_Triangle(2*B+2*C-A, B+2*C-2*A, 2*B+3*C-2*A, Max_Perimeter, Total_Cnt, Primitive_Cnt);
|
||||
end if;
|
||||
end New_Triangle;
|
||||
|
||||
T_Cnt, P_Cnt: Large_Natural;
|
||||
|
||||
begin
|
||||
for I in 1 .. 9 loop
|
||||
T_Cnt := 0;
|
||||
P_Cnt := 0;
|
||||
New_Triangle(3,4,5, 10**I, Total_Cnt => T_Cnt, Primitive_Cnt => P_Cnt);
|
||||
Ada.Text_IO.Put_Line("Up to 10 **" & Integer'Image(I) & " :" &
|
||||
Large_Natural'Image(T_Cnt) & " Triples," &
|
||||
Large_Natural'Image(P_Cnt) & " Primitives");
|
||||
end loop;
|
||||
end Pythagorean_Triples;
|
||||
49
Task/Pythagorean-triples/BASIC256/pythagorean-triples.basic
Normal file
49
Task/Pythagorean-triples/BASIC256/pythagorean-triples.basic
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
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
|
||||
|
|
@ -1,12 +1,17 @@
|
|||
global total prim maxperi .
|
||||
proc newtri s0 s1 s2 .
|
||||
p = s0 + s1 + s2
|
||||
if p <= maxperi
|
||||
fastproc newtri s0 s1 s2 .
|
||||
while 1 = 1
|
||||
p = s0 + s1 + s2
|
||||
if p > maxperi : return
|
||||
prim += 1
|
||||
total += maxperi div p
|
||||
newtri s0 - 2 * s1 + 2 * s2 2 * s0 - s1 + 2 * s2 2 * s0 - 2 * s1 + 3 * s2
|
||||
newtri s0 + 2 * s1 + 2 * s2 2 * s0 + s1 + 2 * s2 2 * s0 + 2 * s1 + 3 * s2
|
||||
newtri -s0 + 2 * s1 + 2 * s2 -2 * s0 + s1 + 2 * s2 -2 * s0 + 2 * s1 + 3 * s2
|
||||
newtri s0 - 2 * s1 + 2 * s2, 2 * s0 - s1 + 2 * s2, 2 * s0 - 2 * s1 + 3 * s2
|
||||
newtri s0 + 2 * s1 + 2 * s2, 2 * s0 + s1 + 2 * s2, 2 * s0 + 2 * s1 + 3 * s2
|
||||
h0 = -s0 + 2 * s1 + 2 * s2
|
||||
h1 = -2 * s0 + s1 + 2 * s2
|
||||
s2 = -2 * s0 + 2 * s1 + 3 * s2
|
||||
s0 = h0
|
||||
s1 = h1
|
||||
.
|
||||
.
|
||||
for maxperi in [ 100 10000000 ]
|
||||
|
|
|
|||
21
Task/Pythagorean-triples/Euphoria/pythagorean-triples.eu
Normal file
21
Task/Pythagorean-triples/Euphoria/pythagorean-triples.eu
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
function tri(atom lim, sequence in)
|
||||
sequence r
|
||||
atom p
|
||||
p = in[1] + in[2] + in[3]
|
||||
if p > lim then
|
||||
return {0, 0}
|
||||
end if
|
||||
r = {1, floor(lim / p)}
|
||||
r += tri(lim, { in[1]-2*in[2]+2*in[3], 2*in[1]-in[2]+2*in[3], 2*in[1]-2*in[2]+3*in[3]})
|
||||
r += tri(lim, { in[1]+2*in[2]+2*in[3], 2*in[1]+in[2]+2*in[3], 2*in[1]+2*in[2]+3*in[3]})
|
||||
r += tri(lim, {-in[1]+2*in[2]+2*in[3], -2*in[1]+in[2]+2*in[3], -2*in[1]+2*in[2]+3*in[3]})
|
||||
return r
|
||||
end function
|
||||
|
||||
atom max_peri
|
||||
max_peri = 10
|
||||
while max_peri <= 100000000 do
|
||||
printf(1,"%d: ", max_peri)
|
||||
? tri(max_peri, {3, 4, 5})
|
||||
max_peri *= 10
|
||||
end while
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
include "NSLog.incl"
|
||||
|
||||
begin globals
|
||||
long total = 0
|
||||
long prim = 0
|
||||
long maxPeri = 0
|
||||
end globals
|
||||
|
||||
void local fn newTri( s0 as long, s1 as long, s2 as long )
|
||||
long p = s0 + s1 + s2
|
||||
if ( p <= maxPeri )
|
||||
prim++
|
||||
total += maxPeri / p
|
||||
fn newTri( s0 - 2 * s1 + 2 * s2, 2 * s0 - s1 + 2 * s2, 2 * s0 - 2 * s1 + 3 * s2 )
|
||||
fn newTri( s0 + 2 * s1 + 2 * s2, 2 * s0 + s1 + 2 * s2, 2 * s0 + 2 * s1 + 3 * s2 )
|
||||
fn newTri( -s0 + 2 * s1 + 2 * s2, -2 * s0 + s1 + 2 * s2, -2 * s0 + 2 * s1 + 3 * s2 )
|
||||
end if
|
||||
end fn
|
||||
|
||||
void local fn DoIt
|
||||
int i = 2
|
||||
maxPeri = 100
|
||||
while ( maxPeri <= 10000000000 )
|
||||
prim = 0
|
||||
total = 0
|
||||
fn newTri( 3, 4, 5 )
|
||||
NSLog( @"M^%2d %12ld %12ld %-11ld", i, maxPeri, total, prim ) : i++
|
||||
maxPeri *= 10
|
||||
wend
|
||||
end fn
|
||||
|
||||
NSLog( @"below triples primitive" )
|
||||
CFTimeInterval t : t = fn CACurrentMediaTime
|
||||
fn DoIt
|
||||
NsLog( @"\nCompute time: %.3f ms", (fn CACurrentMediaTime-t)*1000 )
|
||||
|
||||
HandleEvents
|
||||
25
Task/Pythagorean-triples/Pluto/pythagorean-triples.pluto
Normal file
25
Task/Pythagorean-triples/Pluto/pythagorean-triples.pluto
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
local sc = os.clock()
|
||||
local total = 0
|
||||
local prim = 0
|
||||
local max_peri = 0
|
||||
|
||||
local function new_tri(s0, s1, s2)
|
||||
local p = s0 + s1 + s2
|
||||
if p <= max_peri then
|
||||
prim += 1
|
||||
total += max_peri // p
|
||||
new_tri( 1*s0 - 2*s1 + 2*s2, 2*s0 - 1*s1 + 2*s2, 2*s0 - 2*s1 + 3*s2)
|
||||
new_tri( 1*s0 + 2*s1 + 2*s2, 2*s0 + 1*s1 + 2*s2, 2*s0 + 2*s1 + 3*s2)
|
||||
new_tri(-1*s0 + 2*s1 + 2*s2, -2*s0 + 1*s1 + 2*s2, -2*s0 + 2*s1 + 3*s2)
|
||||
end
|
||||
end
|
||||
|
||||
max_peri = 100
|
||||
while max_peri <= 1e10 do
|
||||
prim = 0
|
||||
total = 0
|
||||
new_tri(3, 4, 5)
|
||||
local secs = math.round(os.clock() - sc)
|
||||
print($"Up to {max_peri}: {total} triples, {prim} primitives, {secs} seconds")
|
||||
max_peri *= 10
|
||||
end
|
||||
50
Task/Pythagorean-triples/PowerShell/pythagorean-triples.ps1
Normal file
50
Task/Pythagorean-triples/PowerShell/pythagorean-triples.ps1
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
function triples($p) {
|
||||
if($p -gt 4) {
|
||||
# ai + bi + ci = pi <= p
|
||||
# ai < bi < ci --> 3ai < pi <= p and ai + 2bi < pi <= p
|
||||
$pa = [Math]::Floor($p/3)
|
||||
1..$pa | foreach {
|
||||
$ai = $_
|
||||
$pb = [Math]::Floor(($p-$ai)/2)
|
||||
($ai+1)..$pb | foreach {
|
||||
$bi = $_
|
||||
$pc = $p-$ai-$bi
|
||||
($bi+1)..$pc | where {
|
||||
$ci = $_
|
||||
$pi = $ai + $bi + $ci
|
||||
$ci*$ci -eq $ai*$ai + $bi*$bi
|
||||
} |
|
||||
foreach {
|
||||
[pscustomobject]@{
|
||||
a = "$ai"
|
||||
b = "$bi"
|
||||
c = "$ci"
|
||||
p = "$pi"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
Write-Error "$p is not greater than 4"
|
||||
}
|
||||
}
|
||||
function gcd ($a, $b) {
|
||||
function pgcd ($n, $m) {
|
||||
if($n -le $m) {
|
||||
if($n -eq 0) {$m}
|
||||
else{pgcd $n ($m%$n)}
|
||||
}
|
||||
else {pgcd $m $n}
|
||||
}
|
||||
$n = [Math]::Abs($a)
|
||||
$m = [Math]::Abs($b)
|
||||
(pgcd $n $m)
|
||||
}
|
||||
$triples = (triples 100)
|
||||
|
||||
$coprime = $triples |
|
||||
where {((gcd $_.a $_.b) -eq 1) -and ((gcd $_.a $_.c) -eq 1) -and ((gcd $_.b $_.c) -eq 1)}
|
||||
|
||||
"There are $(($triples).Count) Pythagorean triples with perimeter no larger than 100
|
||||
and $(($coprime).Count) of them are coprime."
|
||||
36
Task/Pythagorean-triples/VBScript/pythagorean-triples.vbs
Normal file
36
Task/Pythagorean-triples/VBScript/pythagorean-triples.vbs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
For i=1 To 8
|
||||
WScript.StdOut.WriteLine triples(10^i)
|
||||
Next
|
||||
|
||||
Function triples(pmax)
|
||||
prim=0 : count=0 : nmax=Sqr(pmax)/2 : n=1
|
||||
Do While n <= nmax
|
||||
m=n+1 : p=2*m*(m+n)
|
||||
Do While p <= pmax
|
||||
If gcd(m,n)=1 Then
|
||||
prim=prim+1
|
||||
count=count+Int(pmax/p)
|
||||
End If
|
||||
m=m+2
|
||||
p=2*m*(m+n)
|
||||
Loop
|
||||
n=n+1
|
||||
Loop
|
||||
triples = "Max Perimeter: " & pmax &_
|
||||
", Total: " & count &_
|
||||
", Primitive: " & prim
|
||||
End Function
|
||||
|
||||
Function gcd(a,b)
|
||||
c = a : d = b
|
||||
Do
|
||||
If c Mod d > 0 Then
|
||||
e = c Mod d
|
||||
c = d
|
||||
d = e
|
||||
Else
|
||||
gcd = d
|
||||
Exit Do
|
||||
End If
|
||||
Loop
|
||||
End Function
|
||||
Loading…
Add table
Add a link
Reference in a new issue