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
90
Task/Benfords-law/FreeBASIC/benfords-law.freebasic
Normal file
90
Task/Benfords-law/FreeBASIC/benfords-law.freebasic
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
' version 27-10-2016
|
||||
' compile with: fbc -s console
|
||||
|
||||
#Define max 1000 ' total number of Fibonacci numbers
|
||||
#Define max_sieve 15485863 ' should give 1,000,000
|
||||
|
||||
#Include Once "gmp.bi" ' uses the GMP libary
|
||||
|
||||
Dim As ZString Ptr z_str
|
||||
Dim As ULong n, d
|
||||
ReDim As ULong digit(1 To 9)
|
||||
Dim As Double expect, found
|
||||
|
||||
Dim As mpz_ptr fib1, fib2
|
||||
fib1 = Allocate(Len(__mpz_struct)) : Mpz_init_set_ui(fib1, 0)
|
||||
fib2 = Allocate(Len(__mpz_struct)) : Mpz_init_set_ui(fib2, 1)
|
||||
|
||||
digit(1) = 1 ' fib2
|
||||
For n = 2 To max
|
||||
Swap fib1, fib2 ' fib1 = 1, fib2 = 0
|
||||
mpz_add(fib2, fib1, fib2) ' fib1 = 1, fib2 = 1 (fib1 + fib2)
|
||||
z_str = mpz_get_str(0, 10, fib2)
|
||||
d = Val(Left(*z_str, 1)) ' strip the 1 digit on the left off
|
||||
digit(d) = digit(d) +1
|
||||
Next
|
||||
|
||||
mpz_clear(fib1) : DeAllocate(fib1)
|
||||
mpz_clear(fib2) : DeAllocate(fib2)
|
||||
|
||||
Print
|
||||
Print "First 1000 Fibonacci numbers"
|
||||
Print "nr: total found expected difference"
|
||||
|
||||
For d = 1 To 9
|
||||
n = digit(d)
|
||||
found = n / 10
|
||||
expect = (Log(1 + 1 / d) / Log(10)) * 100
|
||||
Print Using " ## ##### ###.## % ###.## % ##.### %"; _
|
||||
d; n ; found; expect; expect - found
|
||||
Next
|
||||
|
||||
|
||||
ReDim digit(1 To 9)
|
||||
ReDim As UByte sieve(max_sieve)
|
||||
|
||||
'For d = 4 To max_sieve Step 2
|
||||
' sieve(d) = 1
|
||||
'Next
|
||||
Print : Print "start sieve"
|
||||
For d = 3 To sqr(max_sieve)
|
||||
If sieve(d) = 0 Then
|
||||
For n = d * d To max_sieve Step d * 2
|
||||
sieve(n) = 1
|
||||
Next
|
||||
End If
|
||||
Next
|
||||
|
||||
digit(2) = 1 ' 2
|
||||
|
||||
Print "start collecting first digits"
|
||||
For n = 3 To max_sieve Step 2
|
||||
If sieve(n) = 0 Then
|
||||
d = Val(Left(Trim(Str(n)), 1))
|
||||
digit(d) = digit(d) +1
|
||||
End If
|
||||
Next
|
||||
|
||||
Dim As ulong total
|
||||
For n = 1 To 9
|
||||
total = total + digit(n)
|
||||
Next
|
||||
|
||||
Print
|
||||
Print "First";total; " primes"
|
||||
Print "nr: total found expected difference"
|
||||
|
||||
For d = 1 To 9
|
||||
n = digit(d)
|
||||
found = n / total * 100
|
||||
expect = (Log(1 + 1 / d) / Log(10)) * 100
|
||||
Print Using " ## ######## ###.## % ###.## % ###.### %"; _
|
||||
d; n ; found; expect; expect - found
|
||||
Next
|
||||
|
||||
|
||||
' empty keyboard buffer
|
||||
While InKey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
14
Task/Benfords-law/Phix/benfords-law-1.phix
Normal file
14
Task/Benfords-law/Phix/benfords-law-1.phix
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
procedure main(sequence s, string title)
|
||||
sequence f = repeat(0,9)
|
||||
for i=1 to length(s) do
|
||||
f[sprint(s[i])[1]-'0'] += 1
|
||||
end for
|
||||
puts(1,title)
|
||||
puts(1,"Digit Observed% Predicted%\n")
|
||||
for i=1 to length(f) do
|
||||
printf(1," %d %9.3f %8.3f\n", {i, f[i]/length(s)*100, log10(1+1/i)*100})
|
||||
end for
|
||||
end procedure
|
||||
main(fib(1000),"First 1000 Fibonacci numbers\n")
|
||||
main(primes(10000),"First 10000 Prime numbers\n")
|
||||
main(threes(500),"First 500 powers of three\n")
|
||||
39
Task/Benfords-law/Phix/benfords-law-2.phix
Normal file
39
Task/Benfords-law/Phix/benfords-law-2.phix
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
function fib(integer lim)
|
||||
atom a=0, b=1
|
||||
sequence res = repeat(0,lim)
|
||||
for i=1 to lim do
|
||||
{res[i], a, b} = {b, b, b+a}
|
||||
end for
|
||||
return res
|
||||
end function
|
||||
|
||||
function primes(integer lim)
|
||||
integer n = 1, k, p
|
||||
sequence res = {2}
|
||||
while length(res)<lim do
|
||||
k = 3
|
||||
p = 1
|
||||
n += 2
|
||||
while k*k<=n and p do
|
||||
p = floor(n/k)*k!=n
|
||||
k += 2
|
||||
end while
|
||||
if p then
|
||||
res = append(res,n)
|
||||
end if
|
||||
end while
|
||||
return res
|
||||
end function
|
||||
|
||||
function threes(integer lim)
|
||||
sequence res = repeat(0,lim)
|
||||
for i=1 to lim do
|
||||
res[i] = power(3,i)
|
||||
end for
|
||||
return res
|
||||
end function
|
||||
|
||||
constant INVLN10 = 0.43429_44819_03251_82765
|
||||
function log10(object x1)
|
||||
return log(x1) * INVLN10
|
||||
end function
|
||||
21
Task/Benfords-law/Sidef/benfords-law.sidef
Normal file
21
Task/Benfords-law/Sidef/benfords-law.sidef
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
var fibonacci = [0, 1] ;
|
||||
{
|
||||
fibonacci.append(fibonacci[-1] + $fibonacci[-2]);
|
||||
} * (1000 - fibonacci.len);
|
||||
|
||||
var (actuals, expected) = ([], []);
|
||||
|
||||
{ |i|
|
||||
var num = 0;
|
||||
fibonacci.each { |j| j.digit(-1) == i && (num++)};
|
||||
actuals.append(num / 1000);
|
||||
expected.append(1 + (1/i) -> log10);
|
||||
} * 9;
|
||||
|
||||
"%17s%17s\n".printf("Observed","Expected");
|
||||
{ |i|
|
||||
"%d : %11s %%%15s %%\n".printf(
|
||||
i, "%.2f".sprintf(100 * actuals[i - 1]),
|
||||
"%.2f".sprintf(100 * expected[i - 1]),
|
||||
);
|
||||
} * 9;
|
||||
40
Task/Benfords-law/Visual-FoxPro/benfords-law.visual
Normal file
40
Task/Benfords-law/Visual-FoxPro/benfords-law.visual
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#DEFINE CTAB CHR(9)
|
||||
#DEFINE COMMA ","
|
||||
#DEFINE CRLF CHR(13) + CHR(10)
|
||||
LOCAL i As Integer, n As Integer, n1 As Integer, rho As Double, c As String
|
||||
n = 1000
|
||||
LOCAL ARRAY a[n,2], res[1]
|
||||
CLOSE DATABASES ALL
|
||||
CREATE CURSOR fibo(dig C(1))
|
||||
INDEX ON dig TAG dig COLLATE "Machine"
|
||||
SET ORDER TO 0
|
||||
*!* Populate the cursor with the leading digit of the first 1000 Fibonacci numbers
|
||||
a[1,1] = "1"
|
||||
a[1,2] = 1
|
||||
a[2,1] = "1"
|
||||
a[2,2] = 1
|
||||
FOR i = 3 TO n
|
||||
a[i,2] = a[i-2,2] + a[i-1,2]
|
||||
a[i,1] = LEFT(TRANSFORM(a[i,2]), 1)
|
||||
ENDFOR
|
||||
APPEND FROM ARRAY a FIELDS dig
|
||||
CREATE CURSOR results (digit I, count I, prob B(6), expected B(6))
|
||||
INSERT INTO results ;
|
||||
SELECT dig, COUNT(1), COUNT(1)/n, Pr(VAL(dig)) FROM fibo GROUP BY dig ORDER BY dig
|
||||
n1 = RECCOUNT()
|
||||
*!* Correlation coefficient
|
||||
SELECT (n1*SUM(prob*expected) - SUM(prob)*SUM(expected))/;
|
||||
(SQRT(n1*SUM(prob*prob) - SUM(prob)*SUM(prob))*SQRT(n1*SUM(expected*expected) - SUM(expected)*SUM(expected))) ;
|
||||
FROM results INTO ARRAY res
|
||||
rho = CAST(res[1] As B(6))
|
||||
SET SAFETY OFF
|
||||
COPY TO benford.txt TYPE CSV
|
||||
c = FILETOSTR("benford.txt")
|
||||
*!* Replace commas with tabs
|
||||
c = STRTRAN(c, COMMA, CTAB) + CRLF + "Correlation Coefficient: " + TRANSFORM(rho)
|
||||
STRTOFILE(c, "benford.txt", 0)
|
||||
SET SAFETY ON
|
||||
|
||||
FUNCTION Pr(d As Integer) As Double
|
||||
RETURN LOG10(1 + 1/d)
|
||||
ENDFUNC
|
||||
107
Task/Benfords-law/jq/benfords-law.jq
Normal file
107
Task/Benfords-law/jq/benfords-law.jq
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
# Generate the first n Fibonacci numbers: 1, 1, ...
|
||||
# Numerical accuracy is insufficient beyond about 1450.
|
||||
def fibonacci(n):
|
||||
# input: [f(i-2), f(i-1), countdown]
|
||||
def fib: (.[0] + .[1]) as $sum
|
||||
| if .[2] <= 0 then empty
|
||||
elif .[2] == 1 then $sum
|
||||
else $sum, ([ .[1], $sum, .[2] - 1 ] | fib)
|
||||
end;
|
||||
[1, 0, n] | fib ;
|
||||
|
||||
# is_prime is tailored to work with jq 1.4
|
||||
def is_prime:
|
||||
if . == 2 then true
|
||||
else 2 < . and . % 2 == 1 and
|
||||
. as $in
|
||||
| (($in + 1) | sqrt) as $m
|
||||
| (((($m - 1) / 2) | floor) + 1) as $max
|
||||
| reduce range(1; $max) as $i
|
||||
(true; if . then ($in % ((2 * $i) + 1)) > 0 else false end)
|
||||
end ;
|
||||
|
||||
# primes in [m,n)
|
||||
def primes(m;n):
|
||||
range(m;n) | select(is_prime);
|
||||
|
||||
def runs:
|
||||
reduce .[] as $item
|
||||
( [];
|
||||
if . == [] then [ [ $item, 1] ]
|
||||
else .[length-1] as $last
|
||||
| if $last[0] == $item
|
||||
then (.[0:length-1] + [ [$item, $last[1] + 1] ] )
|
||||
else . + [[$item, 1]]
|
||||
end
|
||||
end ) ;
|
||||
|
||||
# Inefficient but brief:
|
||||
def histogram: sort | runs;
|
||||
|
||||
def benford_probability:
|
||||
tonumber
|
||||
| if . > 0 then ((1 + (1 /.)) | log) / (10|log)
|
||||
else 0
|
||||
end ;
|
||||
|
||||
# benford takes a stream and produces an array of [ "d", observed, expected ]
|
||||
def benford(stream):
|
||||
[stream | tostring | .[0:1] ] | histogram as $histogram
|
||||
| reduce ($histogram | .[] | .[0]) as $digit
|
||||
([]; . + [$digit, ($digit|benford_probability)] )
|
||||
| map(select(type == "number")) as $probabilities
|
||||
| ([ $histogram | .[] | .[1] ] | add) as $total
|
||||
| reduce range(0; $histogram|length) as $i
|
||||
([]; . + ([$histogram[$i] + [$total * $probabilities[$i]] ] ) ) ;
|
||||
|
||||
# given an array of [value, observed, expected] values,
|
||||
# produce the χ² statistic
|
||||
def chiSquared:
|
||||
reduce .[] as $triple
|
||||
(0;
|
||||
if $triple[2] == 0 then .
|
||||
else . + ($triple[1] as $o | $triple[2] as $e | ($o - $e) | (.*.)/$e)
|
||||
end) ;
|
||||
|
||||
# truncate n places after the decimal point;
|
||||
# return a string since it can readily be converted back to a number
|
||||
def precision(n):
|
||||
tostring as $s | $s | index(".")
|
||||
| if . then $s[0:.+n+1] else $s end ;
|
||||
|
||||
# Right-justify but do not truncate
|
||||
def rjustify(n):
|
||||
length as $length | if n <= $length then . else " " * (n-$length) + . end;
|
||||
|
||||
# Attempt to align decimals so integer part is in a field of width n
|
||||
def align(n):
|
||||
index(".") as $ix
|
||||
| if n < $ix then .
|
||||
elif $ix then (.[0:$ix]|rjustify(n)) +.[$ix:]
|
||||
else rjustify(n)
|
||||
end ;
|
||||
|
||||
# given an array of [value, observed, expected] values,
|
||||
# produce rows of the form: value observed expected
|
||||
def print_rows(prec):
|
||||
.[] | map( precision(prec)|align(5) + " ") | add ;
|
||||
|
||||
def report(heading; stream):
|
||||
benford(stream) as $array
|
||||
| heading,
|
||||
" Digit Observed Expected",
|
||||
( $array | print_rows(2) ),
|
||||
"",
|
||||
" χ² = \( $array | chiSquared | precision(4))",
|
||||
""
|
||||
;
|
||||
|
||||
def task:
|
||||
report("First 100 fibonacci numbers:"; fibonacci( 100) ),
|
||||
report("First 1000 fibonacci numbers:"; fibonacci(1000) ),
|
||||
report("Primes less than 1000:"; primes(2;1000)),
|
||||
report("Primes between 1000 and 10000:"; primes(1000;10000)),
|
||||
report("Primes less than 100000:"; primes(2;100000))
|
||||
;
|
||||
|
||||
task
|
||||
Loading…
Add table
Add a link
Reference in a new issue