Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,33 +0,0 @@
|
|||
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;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
triples: new []
|
||||
triples: []
|
||||
loop 1..50 'x [
|
||||
loop 1..50 'y [
|
||||
loop (max @[x y])..100 'z [
|
||||
|
|
@ -14,7 +14,7 @@ unique 'triples
|
|||
print ["Found" size triples "pythagorean triples with a perimeter no larger than 100:"]
|
||||
print triples
|
||||
|
||||
primitive: select triples => [1 = gcd]
|
||||
primitive: select triples => [1 = gcd &]
|
||||
|
||||
print ""
|
||||
print [size primitive "of them are primitive:"]
|
||||
|
|
|
|||
|
|
@ -1,21 +0,0 @@
|
|||
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
|
||||
|
|
@ -1,23 +1,44 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">total</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">prim</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">maxPeri</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">10</span>
|
||||
with javascript_semantics
|
||||
function count_triplets(atom max_perimeter)
|
||||
integer limit = floor(sqrt(max_perimeter/2))+1
|
||||
atom total = 0, primitive = 0, t0 = time(), t1 = t0+1
|
||||
// The formulas below generate primitive triples if:
|
||||
// 0 < n < m
|
||||
// m and n are relatively prime (gcd == 1)
|
||||
// m + n is odd
|
||||
for m=2 to limit do
|
||||
for n=1+odd(m) to m-1 by 2 do
|
||||
-- if gcd(m,n)=1 then
|
||||
-- inlined for a 5-fold speedup:
|
||||
integer u = m, v = n, t
|
||||
do
|
||||
t = u
|
||||
u = v
|
||||
v = rmdr(t,v)
|
||||
until v=0
|
||||
if u=1 then
|
||||
-- a = m*m - n*n
|
||||
-- b = 2*m*n
|
||||
-- c = m*m + n*n
|
||||
-- perimeter = a + b + c
|
||||
atom perimeter = 2*m*(m+n)
|
||||
if perimeter <= max_perimeter then
|
||||
primitive += 1
|
||||
total += floor(max_perimeter/perimeter)
|
||||
end if
|
||||
end if
|
||||
end for
|
||||
if time()>t1 then
|
||||
progress("m:%d/%d (%d%%)\r",{m,limit,m/limit*100})
|
||||
t1 = time()+1
|
||||
end if
|
||||
end for
|
||||
string e = elapsed_short(time()-t0,1," (%s)")
|
||||
return {max_perimeter, total, primitive, e}
|
||||
end function
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">tri</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">s0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">s2</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s0</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">s1</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">s2</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">p</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">maxPeri</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">prim</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #000000;">total</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">maxPeri</span><span style="color: #0000FF;">/</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">tri</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">s0</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*(-</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">+</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*(</span> <span style="color: #000000;">s0</span><span style="color: #0000FF;">+</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*(</span> <span style="color: #000000;">s0</span><span style="color: #0000FF;">-</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">+</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">);</span>
|
||||
<span style="color: #000000;">tri</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">s0</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*(</span> <span style="color: #000000;">s1</span><span style="color: #0000FF;">+</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*(</span> <span style="color: #000000;">s0</span><span style="color: #0000FF;">+</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*(</span> <span style="color: #000000;">s0</span><span style="color: #0000FF;">+</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">+</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">);</span>
|
||||
<span style="color: #000000;">tri</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">s0</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*(</span> <span style="color: #000000;">s1</span><span style="color: #0000FF;">+</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*(-</span><span style="color: #000000;">s0</span><span style="color: #0000FF;">+</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*(-</span><span style="color: #000000;">s0</span><span style="color: #0000FF;">+</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">+</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">);</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">maxPeri</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">1e8</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">prim</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">;</span>
|
||||
<span style="color: #000000;">total</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">;</span>
|
||||
<span style="color: #000000;">tri</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">);</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Up to %d: %d triples, %d primitives.\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">maxPeri</span><span style="color: #0000FF;">,</span><span style="color: #000000;">total</span><span style="color: #0000FF;">,</span><span style="color: #000000;">prim</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #000000;">maxPeri</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">;</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<!--
|
||||
integer limit = iff(platform()=JS?9:10)
|
||||
for max_perimeter in sq_power(10,tagset(limit)) do
|
||||
printf(1,"Up to %,d: %,d triples, %,d primitives%s.\n",
|
||||
count_triplets(max_perimeter))
|
||||
end for
|
||||
|
|
|
|||
|
|
@ -1,50 +0,0 @@
|
|||
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."
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
from fractions import gcd
|
||||
|
||||
from math import gcd
|
||||
|
||||
def pt1(maxperimeter=100):
|
||||
'''
|
||||
|
|
|
|||
|
|
@ -1,14 +1,16 @@
|
|||
from sys import setrecursionlimit
|
||||
from functools import reduce
|
||||
|
||||
setrecursionlimit(2000) # 2000 ought to be big enough for everybody
|
||||
|
||||
def triples(lim, a = 3, b = 4, c = 5):
|
||||
l = a + b + c
|
||||
if l > lim: return (0, 0)
|
||||
return reduce(lambda x, y: (x[0] + y[0], x[1] + y[1]), [
|
||||
(1, lim / l),
|
||||
(1, lim // l),
|
||||
triples(lim, a - 2*b + 2*c, 2*a - b + 2*c, 2*a - 2*b + 3*c),
|
||||
triples(lim, a + 2*b + 2*c, 2*a + b + 2*c, 2*a + 2*b + 3*c),
|
||||
triples(lim, -a + 2*b + 2*c, -2*a + b + 2*c, -2*a + 2*b + 3*c) ])
|
||||
|
||||
for peri in [10 ** e for e in range(1, 8)]:
|
||||
print peri, triples(peri)
|
||||
print(peri, triples(peri))
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
10 (0, 0)
|
||||
100 (7, 17)
|
||||
1000 (70, 325)
|
||||
10000 (703, 4858)
|
||||
100000 (7026, 64741)
|
||||
1000000 (70229, 808950)
|
||||
10000000 (702309, 9706567)
|
||||
|
|
@ -1,36 +0,0 @@
|
|||
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