Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,19 @@
(lib 'math.lib)
;; 1 - x^p : P = (1 0 0 0 ... 0 -1)
(define (mono p) (append (list 1) (make-list (1- p) 0) (list -1)))
;; compute (x-1)^p , p >= 1
(define (aks-poly p)
(poly-pow (list -1 1) p))
;;
(define (show-them n)
(for ((p (in-range 1 n)))
(writeln 'p p (poly->string 'x (aks-poly p)))))
;; aks-test
;; P = (x-1)^p + 1 - x^p
(define (aks-test p)
(let ((P (poly-add (mono p) (aks-poly p)))
(test (lambda(a) (zero? (modulo a p))))) ;; p divides a[i] ?
(apply and (map test P)))) ;; returns #t if true for all a[i]

View file

@ -0,0 +1,21 @@
(show-them 13) →
p 1 x -1
p 2 x^2 -2x +1
p 3 x^3 -3x^2 +3x -1
p 4 x^4 -4x^3 +6x^2 -4x +1
p 5 x^5 -5x^4 +10x^3 -10x^2 +5x -1
p 6 x^6 -6x^5 +15x^4 -20x^3 +15x^2 -6x +1
p 7 x^7 -7x^6 +21x^5 -35x^4 +35x^3 -21x^2 +7x -1
p 8 x^8 -8x^7 +28x^6 -56x^5 +70x^4 -56x^3 +28x^2 -8x +1
p 9 x^9 -9x^8 +36x^7 -84x^6 +126x^5 -126x^4 +84x^3 -36x^2 +9x -1
p 10 x^10 -10x^9 +45x^8 -120x^7 +210x^6 -252x^5 +210x^4 -120x^3 +45x^2 -10x +1
p 11 x^11 -11x^10 +55x^9 -165x^8 +330x^7 -462x^6 +462x^5 -330x^4 +165x^3 -55x^2 +11x -1
p 12 x^12 -12x^11 +66x^10 -220x^9 +495x^8 -792x^7 +924x^6 -792x^5 +495x^4 -220x^3 +66x^2 -12x +1
(lib 'bigint)
Lib: bigint.lib loaded.
(for ((p (in-range 2 100)))
(when (aks-test p) (write p))) →
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

View file

@ -0,0 +1,75 @@
'METHOD -- Use the Pascal triangle to retrieve the coefficients
'UPPER LIMIT OF FREEBASIC ULONGINT GETS PRIMES UP TO 70
Sub string_split(s_in As String,char As String,result() As String)
Dim As String s=s_in,var1,var2
Dim As Integer n,pst
#macro split(stri,char,var1,var2)
pst=Instr(stri,char)
var1="":var2=""
If pst<>0 Then
var1=Mid(stri,1,pst-1)
var2=Mid(stri,pst+1)
Else
var1=stri
End If
Redim Preserve result(1 To 1+n-((Len(var1)>0)+(Len(var2)>0)))
result(n+1)=var1
#endmacro
Do
split(s,char,var1,var2):n=n+1:s=var2
Loop Until var2=""
Redim Preserve result(1 To Ubound(result)-1)
End Sub
'Get Pascal triangle components
Function pasc(n As Integer,flag As Integer=0) As String
n+=1
Dim As Ulongint V(n):V(1)=1ul
Dim As String s,sign
For r As Integer= 2 To n
s=""
For i As Integer = r To 1 Step -1
V(i) += V(i-1)
If i Mod 2=1 Then sign="" Else sign="-"
s+=sign+Str(V(i))+","
Next i
Next r
If flag Then 'formatted output
Dim As String i,i2,i3,g
Redim As String a(0)
string_split(s,",",a())
For n1 As Integer=1 To Ubound(a)
If Left(a(n1),1)="-" Then sign="" Else sign="+"
If n1=Ubound(a) Then i2="" Else i2=a(n1)
If n1=2 Then i3="x" Else i3="x^"+Str(n1-1)
If n1=1 Then i="":sign=" " Else i=i3
g+=sign+i2+i+" "
Next n1
g="(x-1)^"+Str(n-1)+" = "+g
Return g
End If
Return s
End Function
Function isprime(num As Integer) As Integer
Redim As String a(0)
string_split(pasc(num),",",a())
For n As Integer=Lbound(a)+1 To Ubound(a)-1
If (Valulng(Ltrim(a(n),"-"))) Mod num<>0 Then Return 0
Next n
Return -1
End Function
'====================================
'Formatted output
For n As Integer=1 To 9
Print pasc(n,1)
Next n
Print
'Limit of Freebasic Ulongint sets about 70 max
Print "Primes up to 70:"
For n As Integer=2 To 70
If isprime(n) Then Print n;
Next n
Sleep

View file

@ -0,0 +1,70 @@
import Data.Vect
-- Computes Binomial Coefficients
binCoef : Nat -> Nat -> Nat
binCoef _ Z = (S Z)
binCoef (S n) (S k) =
if n == k then (S Z) else ((S n) * (binCoef n k)) `div` (S k)
-- Binomial Expansion Of (x - 1)^p
expansion : (n : Nat) -> Vect (S n) Integer
expansion n = expansion' n 1
where
expansion' : (n : Nat) -> Integer -> Vect (S n) Integer
expansion' (S m) s = s * (toIntegerNat $ binCoef n (n `minus` (S m))) ::
expansion' m (s * -1)
expansion' Z s = [s]
showExpansion : Vect n Integer -> String
showExpansion [] = " "
showExpansion (x::xs) {n = S k} = (if x < 0 then "-" else "") ++
term x k ++ showExpansion' xs
where
term : Integer -> Nat -> String
term x n = if n == 0 then (show (abs x)) else
(if (abs x) == 1 then "" else
(show (abs x))) ++ "x" ++
(if n == 1 then "" else "^" ++ show n)
sign : Integer -> String
sign x = if x >= 0 then " + " else " - "
showExpansion' : Vect m Integer -> String
showExpansion' [] = ""
showExpansion' (y::ys) {m = S k} = sign y ++ term y k ++
showExpansion' ys
natToFin' : (m : Nat) -> Fin (S m)
natToFin' n with (natToFin n (S n))
natToFin' n | Just y = y
isPrime : Nat -> Bool
isPrime Z = False
isPrime (S Z ) = False
isPrime n = foldl (\divs, term => divs && (term `mod` (toIntegerNat n)) == 0)
True (fullExpansion $ expansion n)
-- (x - 1)^p - ((x^p) - 1)
where fullExpansion : Vect (S m) Integer -> Vect (S m) Integer
fullExpansion (x::xs) {m} = updateAt (natToFin' m) (+1) $ (x-1)::xs
printExpansions : Nat -> IO ()
printExpansions n = do
putStrLn "-- p: (x-1)^p for small p"
sequence_ $ map printExpansion [0..n]
where printExpansion : Nat -> IO ()
printExpansion n = do
print n
putStr ": "
putStrLn $ showExpansion $ expansion n
main : IO()
main = do
printExpansions 10
putStrLn "\n-- Primes Up To 100:"
putStrLn $ show $ filter isPrime [0..100]

View file

@ -0,0 +1,13 @@
: nextCoef(prev)
| i |
ListBuffer new dup add(0)
prev size 1- loop: i [ dup add(prev at(i) prev at(i 1+) - ) ]
dup add(0) ;
: coefs(n) [ 0, 1, 0 ] #nextCoef times(n) extract(2, n 2 + ) ;
: isPrime(n) coefs(n) extract(2, n) conform(#[n mod 0 == ]) ;
: aks
| i |
0 10 for: i [ System.Out "(x-1)^" << i << " = " << coefs(i) << cr ]
50 seq filter(#isPrime) apply(#[ print " " print ]) printcr ;

View file

@ -0,0 +1,80 @@
-- Does not work for primes above 53, which is actually beyond the original task anyway.
-- Translated from the C version, just about everything is (working) out-by-1, what fun.
sequence c = repeat(0,100)
procedure coef(integer n)
-- out-by-1, ie coef(1)==^0, coef(2)==^1, coef(3)==^2 etc.
c[n] = 1
for i=n-1 to 2 by -1 do
c[i] = c[i]+c[i-1]
end for
end procedure
function is_prime(integer n)
coef(n+1); -- (I said it was out-by-1)
for i=2 to n-1 do -- (technically "to n" is more correct)
if remainder(c[i],n)!=0 then
return 0
end if
end for
return 1
end function
procedure show(integer n)
-- (As per coef, this is (working) out-by-1)
object ci
for i=n to 1 by -1 do
ci = c[i]
if ci=1 then
if remainder(n-i,2)=0 then
if i=1 then
if n=1 then
ci = "1"
else
ci = "+1"
end if
else
ci = ""
end if
else
ci = "-1"
end if
else
if remainder(n-i,2)=0 then
ci = sprintf("+%d",ci)
else
ci = sprintf("-%d",ci)
end if
end if
if i=1 then -- ie ^0
printf(1,"%s",{ci})
elsif i=2 then -- ie ^1
printf(1,"%sx",{ci})
else
printf(1,"%sx^%d",{ci,i-1})
end if
end for
end procedure
procedure AKS_test_for_primes()
for n=1 to 10 do -- (0 to 9 really)
coef(n);
printf(1,"(x-1)^%d = ", n-1);
show(n);
puts(1,'\n');
end for
puts(1,"\nprimes (<=53):");
-- coef(2); -- (needed to reset c, if we want to avoid saying 1 is prime...)
c[2] = 1 -- (this manages "", which is all that call did anyway...)
for n = 2 to 53 do
if is_prime(n) then
printf(1," %d", n);
end if
end for
puts(1,'\n');
if getc(0) then end if
end procedure
AKS_test_for_primes()

View file

@ -0,0 +1,23 @@
func binprime(p) {
p >= 2 || return false
for i in (1 .. p>>1) {
(binomial(p, i) % p) && return false
}
return true
}
func coef(n, e) {
(e == 0) && return "#{n}"
(n == 1) && (n = "")
(e == 1) ? "#{n}x" : "#{n}x^#{e}"
}
func binpoly(p) {
join(" ", coef(1, p), ^p -> map {|i|
join(" ", %w(+ -)[(p-i)&1], coef(binomial(p, i), i))
}.reverse...)
}
say "expansions of (x-1)^p:"
for i in ^10 { say binpoly(i) }
say "Primes to 80: [#{2..80 -> grep { binprime(_) }.join(' ')}]"

View file

@ -0,0 +1,80 @@
func polynomialCoeffs(n: Int) -> [Int] {
var result = [Int](count : n+1, repeatedValue : 0)
result[0]=1
for i in 1 ..< n/2+1 { //Progress up, until reaching the middle value
result[i] = result[i-1] * (n-i+1)/i;
}
for i in n/2+1 ..< n+1 { //Copy the inverse of the first part
result[i] = result[n-i];
}
// Take into account the sign
for i in stride(from: 1, through: n, by: 2) {
result[i] = -result[i]
}
return result
}
func isPrime(n: Int) -> Bool {
var coeffs = polynomialCoeffs(n)
coeffs[0]--
coeffs[n]++
for i in 1 ... n {
if coeffs[i]%n != 0 {
return false
}
}
return true
}
for i in 0...10 {
let coeffs = polynomialCoeffs(i)
print("(x-1)^\(i) = ")
if i == 0 {
print("1")
} else {
if i == 1 {
print("x")
} else {
print("x^\(i)")
if i == 2 {
print("\(coeffs[i-1])x")
} else {
for j in 1...(i - 2) {
if j%2 == 0 {
print("+\(coeffs[j])x^\(i-j)")
} else {
print("\(coeffs[j])x^\(i-j)")
}
}
if (i-1)%2 == 0 {
print("+\(coeffs[i-1])x")
} else {
print("\(coeffs[i-1])x")
}
}
}
if i%2 == 0 {
print("+\(coeffs[i])")
} else {
print("\(coeffs[i])")
}
}
println()
}
println()
print("Primes under 50 : ")
for i in 1...50 {
if isPrime(i) {
print("\(i) ")
}
}

View file

@ -0,0 +1,35 @@
# add_pairs is a helper function for optpascal/0
# Input: an OptPascal array
# Output: the next OptPascal array (obtained by adding adjacent items,
# but if the last two items are unequal, then their sum is repeated)
def add_pairs:
if length <= 1 then .
elif length == 2 then (.[0] + .[1]) as $S
| if (.[0] == .[1]) then [$S]
else [$S,$S]
end
else [.[0] + .[1]] + (.[1:]|add_pairs)
end;
# Input: an OptPascal row
# Output: the next OptPascalRow
def next_optpascal: [1] + add_pairs;
# generate a stream of OptPascal arrays, beginning with []
def optpascals: [] | recurse(next_optpascal);
# generate a stream of Pascal arrays
def pascals:
# pascalize takes as input an OptPascal array and produces
# the corresponding Pascal array;
# if the input ends in a pair, then peel it off before reversing it.
def pascalize:
. + ((if .[-2] == .[-1] then .[0:-2] else .[0:-1] end) | reverse);
optpascals | pascalize;
# Input: integer n
# Output: the n-th Pascal row
def pascal: nth(.; pascals);
def optpascal: nth(.; optpascals);

View file

@ -0,0 +1,4 @@
def coefficients:
def alternate_signs: . as $in
| reduce range(0; length) as $i ([]; . + [$in[$i] * (if $i % 2 == 0 then 1 else -1 end )]);
(.+1) | pascal | alternate_signs;

View file

@ -0,0 +1 @@
range(0;8) | "Coefficient for (x - 1)^\(.): \(coefficients)"

View file

@ -0,0 +1,8 @@
Coefficients for (x - 1)^0: [1]
Coefficients for (x - 1)^1: [1,-1]
Coefficients for (x - 1)^2: [1,-2,1]
Coefficients for (x - 1)^3: [1,-3,3,-1]
Coefficients for (x - 1)^4: [1,-4,6,-4,1]
Coefficients for (x - 1)^5: [1,-5,10,-10,5,-1]
Coefficients for (x - 1)^6: [1,-6,15,-20,15,-6,1]
Coefficients for (x - 1)^7: [1,-7,21,-35,35,-21,7,-1]

View file

@ -0,0 +1,6 @@
def is_prime:
. as $N
| if . < 2 then false
else (1+.) | optpascal
| all( .[2:][]; . % $N == 0 )
end;

View file

@ -0,0 +1 @@
range(0;36) | select(is_prime)

View file

@ -0,0 +1,11 @@
2
3
5
7
11
13
17
19
23
29
31

View file

@ -0,0 +1 @@
[range(0;50) | select(is_prime)]

View file

@ -0,0 +1 @@
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]