Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
59
Task/Pythagorean-triples/ERRE/pythagorean-triples.erre
Normal file
59
Task/Pythagorean-triples/ERRE/pythagorean-triples.erre
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
PROGRAM PIT
|
||||
|
||||
BEGIN
|
||||
|
||||
PRINT(CHR$(12);) !CLS
|
||||
PRINT(TIME$)
|
||||
|
||||
FOR POWER=1 TO 7 DO
|
||||
PLIMIT=10#^POWER
|
||||
UPPERBOUND=INT(1+PLIMIT^0.5)
|
||||
PRIMITIVES=0
|
||||
TRIPLES=0
|
||||
EXTRAS=0 ! will count the in-range multiples of any primitive
|
||||
|
||||
FOR M=2 TO UPPERBOUND DO
|
||||
FOR N=1+(M MOD 2=1) TO M-1 STEP 2 DO
|
||||
TERM1=2*M*N
|
||||
TERM2=M*M-N*N
|
||||
TERM3=M*M+N*N
|
||||
PERIMETER=TERM1+TERM2+TERM3
|
||||
|
||||
IF PERIMETER<=PLIMIT THEN TRIPLES=TRIPLES+1
|
||||
|
||||
A=TERM1
|
||||
B=TERM2
|
||||
|
||||
REPEAT
|
||||
R=A-B*INT(A/B)
|
||||
A=B
|
||||
B=R
|
||||
UNTIL R<=0
|
||||
|
||||
! we've found a primitive triple if a = 1, since hcf =1.
|
||||
! and it is inside perimeter range. Save it in an array
|
||||
IF (A=1) AND (PERIMETER<=PLIMIT) THEN
|
||||
PRIMITIVES=PRIMITIVES+1
|
||||
|
||||
!-----------------------------------------------
|
||||
!swap so in increasing order of side length
|
||||
!-----------------------------------------------
|
||||
IF TERM1>TERM2 THEN SWAP(TERM1,TERM2)
|
||||
!-----------------------------------------------
|
||||
!we have the primitive & removed any multiples.
|
||||
!Now calculate ALL the multiples in range.
|
||||
!-----------------------------------------------
|
||||
NEX=INT(PLIMIT/PERIMETER)
|
||||
EXTRAS=EXTRAS+NEX
|
||||
END IF
|
||||
|
||||
!scan
|
||||
END FOR
|
||||
END FOR
|
||||
|
||||
PRINT("Primit. with perimeter <=";10#^power;"is";primitives;"&";extras;"non-prim.triples.")
|
||||
PRINT(TIME$)
|
||||
END FOR
|
||||
|
||||
PRINT PRINT("** End **")
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
' version 30-05-2016
|
||||
' compile with: fbc -s console
|
||||
|
||||
' primitive pythagoras triples
|
||||
' a = m^2 - n^2, b = 2mn, c = m^2 + n^2
|
||||
' m, n are positive integers and m > n
|
||||
' m - n = odd and GCD(m, n) = 1
|
||||
' p = a + b + c
|
||||
|
||||
' max m for give perimeter
|
||||
' p = m^2 - n^2 + 2mn + m^2 + n^2
|
||||
' p = 2mn + m^2 + m^2 + n^2 - n^2 = 2mn + 2m^2
|
||||
' m >> n and n = 1 ==> p = 2m + 2m^2 = 2m(1 + m)
|
||||
' m >> 1 ==> p = 2m(m) = 2m^2
|
||||
' max m for given perimeter = sqr(p / 2)
|
||||
|
||||
Function gcd(x As UInteger, y As UInteger) As UInteger
|
||||
|
||||
Dim As UInteger t
|
||||
|
||||
While y
|
||||
t = y
|
||||
y = x Mod y
|
||||
x = t
|
||||
Wend
|
||||
Return x
|
||||
|
||||
End Function
|
||||
|
||||
|
||||
Sub pyth_trip(limit As ULongInt, ByRef trip As ULongInt, ByRef prim As ULongInt)
|
||||
|
||||
Dim As ULongInt perimeter, lby2 = limit Shr 1
|
||||
Dim As UInteger m, n
|
||||
Dim As ULongInt a, b, c
|
||||
|
||||
For m = 2 To Sqr(limit / 2)
|
||||
For n = 1 + (m And 1) To (m - 1) Step 2
|
||||
' common divisor, try next n
|
||||
If (gcd(m, n) > 1) Then Continue For
|
||||
a = CULngInt(m) * m - n * n
|
||||
b = CULngInt(m) * n * 2
|
||||
c = CULngInt(m) * m + n * n
|
||||
perimeter = a + b + c
|
||||
' perimeter > limit, since n goes up try next m
|
||||
If perimeter >= limit Then Continue For, For
|
||||
prim += 1
|
||||
If perimeter < lby2 Then
|
||||
trip += limit \ perimeter
|
||||
Else
|
||||
trip += 1
|
||||
End If
|
||||
Next n
|
||||
Next m
|
||||
|
||||
End Sub
|
||||
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim As String str1, buffer = Space(14)
|
||||
Dim As ULongInt limit, trip, prim
|
||||
Dim As Double t, t1 = Timer
|
||||
|
||||
Print "below triples primitive time"
|
||||
Print
|
||||
|
||||
For x As UInteger = 1 To 12
|
||||
t = Timer
|
||||
limit = 10 ^ x : trip = 0 : prim = 0
|
||||
pyth_trip(limit, trip, prim)
|
||||
LSet buffer, Str(prim) : str1 = buffer
|
||||
Print Using "10^## ################ "; x; trip;
|
||||
If x > 7 Then
|
||||
Print str1;
|
||||
Print Using " ######.## sec."; Timer - t
|
||||
Else
|
||||
Print str1
|
||||
End If
|
||||
Next x
|
||||
|
||||
Print : Print
|
||||
Print Using "Total time needed #######.## sec."; Timer - t1
|
||||
|
||||
|
||||
' empty keyboard buffer
|
||||
While InKey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
' version 30-05-2016
|
||||
' compile with: fbc -s console
|
||||
|
||||
' max m for give perimeter
|
||||
' p = m^2 - n^2 + 2mn + m^2 + n^2
|
||||
' p = 2mn + m^2 + m^2 + n^2 - n^2 = 2mn + 2m^2
|
||||
' m >> n and n = 1 ==> p = 2m + 2m^2 = 2m(1 + m)
|
||||
' m >> 1 ==> p = 2m(m) = 2m^2
|
||||
' max m for given perimeter = sqr(p / 2)
|
||||
|
||||
Function gcd(x As UInteger, y As UInteger) As UInteger
|
||||
|
||||
Dim As UInteger t
|
||||
|
||||
While y
|
||||
t = y
|
||||
y = x Mod y
|
||||
x = t
|
||||
Wend
|
||||
Return x
|
||||
|
||||
End Function
|
||||
|
||||
Sub pyth_trip_fast(limit As ULongInt, ByRef trip As ULongInt, ByRef prim As ULongInt)
|
||||
|
||||
|
||||
Dim As ULongInt perimeter, lby2 = limit Shr 1
|
||||
Dim As UInteger mx2 = 4
|
||||
|
||||
For m As UInteger = 2 To Sqr(limit / 2)
|
||||
perimeter = (CULngInt(m) * m * 2) - IIf(m And 1, 0, m * 2)
|
||||
mx2 = mx2 + 4
|
||||
For n As UInteger = 1 + (m And 1) To (m - 1) Step 2
|
||||
perimeter += mx2
|
||||
' common divisor, try next n
|
||||
If (gcd(m, n) > 1) Then Continue For
|
||||
' perimeter > limit, since n goes up try next m
|
||||
If perimeter >= limit Then Continue For, For
|
||||
prim += 1
|
||||
If perimeter < lby2 Then
|
||||
trip += limit \ perimeter
|
||||
Else
|
||||
trip += 1
|
||||
End If
|
||||
Next n
|
||||
Next m
|
||||
|
||||
End Sub
|
||||
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim As String str1, buffer = Space(14)
|
||||
Dim As ULongInt limit, trip, prim
|
||||
Dim As Double t, t1 = Timer
|
||||
|
||||
Print "below triples primitive time"
|
||||
Print
|
||||
|
||||
For x As UInteger = 1 To 12
|
||||
t = Timer
|
||||
limit = 10 ^ x : trip = 0 : prim = 0
|
||||
pyth_trip_fast(limit, trip, prim)
|
||||
LSet buffer, Str(prim) : str1 = buffer
|
||||
Print Using "10^## ################ "; x; trip;
|
||||
If x > 7 Then
|
||||
Print str1;
|
||||
Print Using " ######.## sec."; Timer - t
|
||||
Else
|
||||
Print str1
|
||||
End If
|
||||
Next x
|
||||
|
||||
Print : Print
|
||||
Print Using "Total time needed #######.## sec."; Timer - t1
|
||||
|
||||
|
||||
' empty keyboard buffer
|
||||
While InKey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
18
Task/Pythagorean-triples/Lasso/pythagorean-triples.lasso
Normal file
18
Task/Pythagorean-triples/Lasso/pythagorean-triples.lasso
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// Brute Force: Too slow for large numbers
|
||||
define num_pythagorean_triples(max_perimeter::integer) => {
|
||||
local(max_b) = (#max_perimeter / 3)*2
|
||||
|
||||
return (
|
||||
with a in 1 to (#max_b - 1)
|
||||
sum integer(
|
||||
with b in (#a + 1) to #max_b
|
||||
let c = math_sqrt(#a*#a + #b*#b)
|
||||
where #c == integer(#c)
|
||||
where #c > #b
|
||||
where (#a+#b+#c) <= #max_perimeter
|
||||
sum 1
|
||||
)
|
||||
)
|
||||
}
|
||||
stdout(`Number of Pythagorean Triples in a Perimeter of 100: `)
|
||||
stdoutnl(num_pythagorean_triples(100))
|
||||
25
Task/Pythagorean-triples/Nim/pythagorean-triples.nim
Normal file
25
Task/Pythagorean-triples/Nim/pythagorean-triples.nim
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
const u = [[ 1, -2, 2, 2, -1, 2, 2, -2, 3],
|
||||
[ 1, 2, 2, 2, 1, 2, 2, 2, 3],
|
||||
[-1, 2, 2, -2, 1, 2, -2, 2, 3]]
|
||||
|
||||
var
|
||||
total, prim = 0
|
||||
maxPeri = 10
|
||||
|
||||
proc newTri(ins) =
|
||||
var p = ins[0] + ins[1] + ins[2]
|
||||
if p > maxPeri: return
|
||||
inc(prim)
|
||||
total += maxPeri div p
|
||||
|
||||
for i in 0..2:
|
||||
newTri([u[i][0] * ins[0] + u[i][1] * ins[1] + u[i][2] * ins[2],
|
||||
u[i][3] * ins[0] + u[i][4] * ins[1] + u[i][5] * ins[2],
|
||||
u[i][6] * ins[0] + u[i][7] * ins[1] + u[i][8] * ins[2]])
|
||||
|
||||
while maxPeri <= 100_000_000:
|
||||
total = 0
|
||||
prim = 0
|
||||
newTri([3, 4, 5])
|
||||
echo "Up to ", maxPeri, ": ", total, " triples, ", prim, " primitives"
|
||||
maxPeri *= 10
|
||||
20
Task/Pythagorean-triples/Phix/pythagorean-triples.phix
Normal file
20
Task/Pythagorean-triples/Phix/pythagorean-triples.phix
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
atom total, prim, maxPeri = 10
|
||||
|
||||
procedure tri(atom s0, s1, s2)
|
||||
atom p = s0 + s1 + s2
|
||||
if p<=maxPeri then
|
||||
prim += 1
|
||||
total += floor(maxPeri/p)
|
||||
tri( s0+2*(-s1+s2), 2*( s0+s2)-s1, 2*( s0-s1+s2)+s2);
|
||||
tri( s0+2*( s1+s2), 2*( s0+s2)+s1, 2*( s0+s1+s2)+s2);
|
||||
tri(-s0+2*( s1+s2), 2*(-s0+s2)+s1, 2*(-s0+s1+s2)+s2);
|
||||
end if
|
||||
end procedure
|
||||
|
||||
while maxPeri<=1e8 do
|
||||
prim := 0;
|
||||
total := 0;
|
||||
tri(3, 4, 5);
|
||||
printf(1,"Up to %d: %d triples, %d primitives.\n", {maxPeri,total,prim})
|
||||
maxPeri *= 10;
|
||||
end while
|
||||
23
Task/Pythagorean-triples/Ring/pythagorean-triples.ring
Normal file
23
Task/Pythagorean-triples/Ring/pythagorean-triples.ring
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
size = 100
|
||||
sum = 0
|
||||
prime = 0
|
||||
for i = 1 to size
|
||||
for j = i + 1 to size
|
||||
for k = 1 to size
|
||||
if pow(i,2) + pow(j,2) = pow(k,2) and (i+j+k) < 101
|
||||
if gcd(i,j) = 1 prime += 1 ok
|
||||
sum += 1
|
||||
see "" + i + " " + j + " " + k + nl ok
|
||||
next
|
||||
next
|
||||
next
|
||||
see "Total : " + sum + nl
|
||||
see "Primitives : " + prime + nl
|
||||
|
||||
func gcd gcd, b
|
||||
while b
|
||||
c = gcd
|
||||
gcd = b
|
||||
b = c % b
|
||||
end
|
||||
return gcd
|
||||
21
Task/Pythagorean-triples/Sidef/pythagorean-triples.sidef
Normal file
21
Task/Pythagorean-triples/Sidef/pythagorean-triples.sidef
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
func triples(limit) {
|
||||
var primitive = 0;
|
||||
var civilized = 0;
|
||||
|
||||
func oyako(a, b, c) {
|
||||
(var perim = a+b+c) > limit || (
|
||||
primitive++;
|
||||
civilized += int(limit / perim);
|
||||
oyako( a - 2*b + 2*c, 2*a - b + 2*c, 2*a - 2*b + 3*c);
|
||||
oyako( a + 2*b + 2*c, 2*a + b + 2*c, 2*a + 2*b + 3*c);
|
||||
oyako(-a + 2*b + 2*c, -2*a + b + 2*c, -2*a + 2*b + 3*c);
|
||||
);
|
||||
}
|
||||
|
||||
oyako(3,4,5);
|
||||
"#{limit} => (#{primitive} #{civilized})";
|
||||
}
|
||||
|
||||
Inf.times { |n|
|
||||
say triples(10**n);
|
||||
}
|
||||
23
Task/Pythagorean-triples/Swift/pythagorean-triples.swift
Normal file
23
Task/Pythagorean-triples/Swift/pythagorean-triples.swift
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
var total = 0
|
||||
var prim = 0
|
||||
var maxPeri = 100
|
||||
|
||||
func newTri(s0:Int, _ s1:Int, _ s2: Int) -> () {
|
||||
|
||||
let p = s0 + s1 + s2
|
||||
if p <= maxPeri {
|
||||
prim += 1
|
||||
total += maxPeri / p
|
||||
newTri( s0 + 2*(-s1+s2), 2*( s0+s2) - s1, 2*( s0-s1+s2) + s2)
|
||||
newTri( s0 + 2*( s1+s2), 2*( s0+s2) + s1, 2*( s0+s1+s2) + s2)
|
||||
newTri(-s0 + 2*( s1+s2), 2*(-s0+s2) + s1, 2*(-s0+s1+s2) + s2)
|
||||
}
|
||||
}
|
||||
|
||||
while maxPeri <= 100_000_000 {
|
||||
prim = 0
|
||||
total = 0
|
||||
newTri(3, 4, 5)
|
||||
print("Up to \(maxPeri) : \(total) triples \( prim) primitives.")
|
||||
maxPeri *= 10
|
||||
}
|
||||
37
Task/Pythagorean-triples/jq/pythagorean-triples-1.jq
Normal file
37
Task/Pythagorean-triples/jq/pythagorean-triples-1.jq
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
def gcd(a; b):
|
||||
def _gcd:
|
||||
if .[1] == 0 then .[0]
|
||||
else [.[1], .[0] % .[1]] | _gcd
|
||||
end;
|
||||
[a,b] | _gcd ;
|
||||
|
||||
# Return: [total, primitives] for pythagorean triangles having
|
||||
# perimeter no larger than peri.
|
||||
# The following uses Euclid's formula with the convention: m > n.
|
||||
def count(peri):
|
||||
|
||||
# The inner function can be used to count for a given value of m:
|
||||
def _count:
|
||||
# state [n,m,p, [total, primitives]]
|
||||
.[0] as $n | .[1] as $m | .[2] as $p
|
||||
| if $n < $m and $p <= peri then
|
||||
if (gcd($m;$n) == 1)
|
||||
then .[3] | [ (.[0] + ((peri/$p)|floor) ), (.[1] + 1)]
|
||||
else .[3]
|
||||
end
|
||||
| [$n+2, $m, $p+4*$m, .] | _count
|
||||
else .
|
||||
end;
|
||||
|
||||
# m^2 < m*(m+1) <= m*(m+n) = perimeter/2
|
||||
reduce range(2; (peri/2) | sqrt + 1) as $m
|
||||
( [1, 2, 12, [0,0]];
|
||||
(($m % 2) + 1) as $n
|
||||
| (2 * $m * ($m + $n) ) as $p # a+b+c for this (m,n)
|
||||
| [$n, $m, $p, .[3]] | _count
|
||||
) | .[3] ;
|
||||
|
||||
# '''Example''':
|
||||
<lang jq>def pow(i): . as $in | reduce range(0; i) as $j (1; . * $in);
|
||||
|
||||
range(1; 9) | . as $i | 10|pow($i) as $i | "\($i): \(count($i) )"
|
||||
9
Task/Pythagorean-triples/jq/pythagorean-triples-2.jq
Normal file
9
Task/Pythagorean-triples/jq/pythagorean-triples-2.jq
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
$ jq -M -c -r -n -f Pythagorean_triples.jq
|
||||
10: [0,0]
|
||||
100: [17,7]
|
||||
1000: [325,70]
|
||||
10000: [4858,703]
|
||||
100000: [64741,7026]
|
||||
1000000: [808950,70229]
|
||||
10000000: [9706567,702309]
|
||||
100000000: [113236940,7023027]
|
||||
Loading…
Add table
Add a link
Reference in a new issue