September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
44
Task/Fibonacci-word/Aime/fibonacci-word.aime
Normal file
44
Task/Fibonacci-word/Aime/fibonacci-word.aime
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
real
|
||||
entropy(data b)
|
||||
{
|
||||
integer count, i;
|
||||
real ones, zeros;
|
||||
|
||||
ones = zeros = 0;
|
||||
|
||||
i = -(count = b_length(b));
|
||||
while (i) {
|
||||
if (b[i] == '0') {
|
||||
zeros += 1;
|
||||
} else {
|
||||
ones += 1;
|
||||
}
|
||||
|
||||
i += 1;
|
||||
}
|
||||
|
||||
return -(ones /= count) * log2(ones) - (zeros /= count) * log2(zeros);
|
||||
}
|
||||
|
||||
integer
|
||||
main(void)
|
||||
{
|
||||
data a, b;
|
||||
integer i;
|
||||
|
||||
a = "1";
|
||||
b = "0";
|
||||
|
||||
o_form("%2d %9d /w12p10d10/ ~\n", 1, b_length(a), 0r, a);
|
||||
o_form("%2d %9d /w12p10d10/ ~\n", 2, b_length(b), 0r, b);
|
||||
i = 3;
|
||||
while (i <= 37) {
|
||||
b_stock(a, 0, b);
|
||||
o_form("%2d %9d /w12p10d10/ ~\n", i, b_length(a), entropy(a),
|
||||
__hold(i < 10, a, ""));
|
||||
i += 1;
|
||||
b_swap(a, b);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
26
Task/Fibonacci-word/Julia/fibonacci-word.julia
Normal file
26
Task/Fibonacci-word/Julia/fibonacci-word.julia
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# Code for "entropy" taken from https://rosettacode.org/wiki/Entropy#Julia
|
||||
entropy(s::String)::Float32 = -sum(x -> x * log(2, x), [count(x -> x == c, s) / length(s) for c in unique(s)])
|
||||
|
||||
function fibboword(n::Int64)::Array{String}
|
||||
# Initialize the result
|
||||
r = Array{String}(n)
|
||||
# First element
|
||||
r[1] = "0"
|
||||
# If more than 2, set the second element
|
||||
if (n ≥ 2)
|
||||
r[2] = "1"
|
||||
end
|
||||
# Recursively create elements > 3
|
||||
for i in 3:n
|
||||
r[i] = r[i - 1] * r[i - 2]
|
||||
end
|
||||
return r
|
||||
end
|
||||
|
||||
function testfibbo(n::Int64)
|
||||
fib = fibboword(n)
|
||||
for i in 1:length(fib)
|
||||
println(i, "\t", length(fib[i]), "\t", entropy(fib[i]))
|
||||
end
|
||||
return 0
|
||||
end
|
||||
32
Task/Fibonacci-word/Kotlin/fibonacci-word.kotlin
Normal file
32
Task/Fibonacci-word/Kotlin/fibonacci-word.kotlin
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// version 1.0.6
|
||||
|
||||
fun fibWord(n: Int): String {
|
||||
if (n < 1) throw IllegalArgumentException("Argument can't be less than 1")
|
||||
if (n == 1) return "1"
|
||||
val words = Array(n){ "" }
|
||||
words[0] = "1"
|
||||
words[1] = "0"
|
||||
for (i in 2 until n) words[i] = words[i - 1] + words[i - 2]
|
||||
return words[n - 1]
|
||||
}
|
||||
|
||||
fun log2(d: Double) = Math.log(d) / Math.log(2.0)
|
||||
|
||||
fun shannon(s: String): Double {
|
||||
if (s.length <= 1) return 0.0
|
||||
val count0 = s.count { it == '0' }
|
||||
val count1 = s.length - count0
|
||||
val nn = s.length.toDouble()
|
||||
return -(count0 / nn * log2(count0 / nn) + count1 / nn * log2(count1 / nn))
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
println("N Length Entropy Word")
|
||||
println("-- -------- ------------------ ----------------------------------")
|
||||
for (i in 1..37) {
|
||||
val s = fibWord(i)
|
||||
print(String.format("%2d %8d %18.16f", i, s.length, shannon(s)))
|
||||
if (i < 10) println(" $s")
|
||||
else println()
|
||||
}
|
||||
}
|
||||
51
Task/Fibonacci-word/OoRexx/fibonacci-word.rexx
Normal file
51
Task/Fibonacci-word/OoRexx/fibonacci-word.rexx
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/* REXX ---------------------------------------------------------------
|
||||
* 09.08.2014 Walter Pachl 'copied' from REXX
|
||||
* lists the # of chars in fibonacci words and the words' entropy
|
||||
* as well as (part of) the Fibonacci word and the number of 0's and 1's
|
||||
* Note: ooRexx allows for computing up to 47 Fibonacci words
|
||||
*--------------------------------------------------------------------*/
|
||||
Numeric Digits 20 /* use more precision, default=9.*/
|
||||
Parse Arg n fw.1 fw.2 . /* get optional args from the C.L.*/
|
||||
If n=='' Then n=50 /* Not specified? Then use default*/
|
||||
If fw.1=='' Then fw.1=1 /* " " " " " */
|
||||
If fw.2=='' Then fw.2=0 /* " " " " " */
|
||||
hdr1=' N length Entropy Fibonacci word ',
|
||||
'# of zeroes # of ones'
|
||||
hdr2='-- ---------- ---------------------- --------------------',
|
||||
'--------- ---------'
|
||||
Say hdr1
|
||||
Say hdr2
|
||||
Do j=1 For n /* display N fibonacci words. */
|
||||
j1=j-1
|
||||
j2=j-2
|
||||
If j>2 Then /* calculate FIBword if we need to*/
|
||||
fw.j=fw.j1||fw.j2
|
||||
If length(fw.j)<20 Then
|
||||
fwd=left(fw.j,20) /* display the Fibonacci word */
|
||||
Else
|
||||
fwd=left(fw.j,5)'...'right(fw.j,12) /* display parts thereof */
|
||||
Say right(j,2)' 'right(length(fw.j),9)' 'entropy(fw.j)' 'fwd,
|
||||
right(aa.0,9) right(aa.1,9)
|
||||
End
|
||||
Say hdr2
|
||||
Say hdr1
|
||||
Exit
|
||||
|
||||
entropy: Procedure Expose aa.
|
||||
Parse Arg dd
|
||||
l=length(dd)
|
||||
d=digits()
|
||||
aa.0=l-length(space(translate(dd,,0),0)) /*fast way to count zeroes*/
|
||||
aa.1=l-aa.0 /* and figure the number of ones. */
|
||||
If l==1 Then
|
||||
Return left(0,d+2) /* handle special case of one char*/
|
||||
s=0 /* [?] calc entropy for each char */
|
||||
do i=1 for 2
|
||||
_=i-1 /* construct a chr from the ether.*/
|
||||
p=aa._/l /* 'probability of aa-_ in fw */
|
||||
s=s-p*rxmlog(p,d,2) /* add (negatively) the entropies.*/
|
||||
End
|
||||
If s=1 Then
|
||||
Return left(1,d+2) /* return a left-justified "1". */
|
||||
Return format(s,,d) /* normalize the number (sum or S)*/
|
||||
::requires rxm.cls
|
||||
37
Task/Fibonacci-word/Phix/fibonacci-word.phix
Normal file
37
Task/Fibonacci-word/Phix/fibonacci-word.phix
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
function log2(atom v)
|
||||
return log(v)/log(2)
|
||||
end function
|
||||
|
||||
function entropy(sequence s)
|
||||
sequence symbols = {},
|
||||
counts = {}
|
||||
integer N = length(s)
|
||||
for i=1 to N do
|
||||
object si = s[i]
|
||||
integer k = find(si,symbols)
|
||||
if k=0 then
|
||||
symbols = append(symbols,si)
|
||||
counts = append(counts,1)
|
||||
else
|
||||
counts[k] += 1
|
||||
end if
|
||||
end for
|
||||
atom H = 0
|
||||
integer n = length(counts)
|
||||
for i=1 to n do
|
||||
atom ci = counts[i]/N
|
||||
H -= ci*log2(ci)
|
||||
end for
|
||||
return H
|
||||
end function
|
||||
|
||||
sequence F_words = {"1","0"}
|
||||
for i=3 to 37 do
|
||||
F_words = append(F_words,F_words[i-1]&F_words[i-2])
|
||||
end for
|
||||
|
||||
for i=1 to length(F_words) do
|
||||
printf(1,"%2d: length %9d, entropy %f %s\n",
|
||||
{i,length(F_words[i]),entropy(F_words[i]),
|
||||
iff(i<10?F_words[i],"...")})
|
||||
end for
|
||||
42
Task/Fibonacci-word/Scheme/fibonacci-word.ss
Normal file
42
Task/Fibonacci-word/Scheme/fibonacci-word.ss
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
(import (scheme base)
|
||||
(scheme inexact)
|
||||
(scheme write))
|
||||
|
||||
(define *words* (make-vector 38 ""))
|
||||
|
||||
(define (create-words)
|
||||
(vector-set! *words* 1 "1")
|
||||
(vector-set! *words* 2 "0")
|
||||
(do ((i 3 (+ 1 i)))
|
||||
((= i (vector-length *words*)) )
|
||||
(vector-set! *words* i (string-append (vector-ref *words* (- i 1))
|
||||
(vector-ref *words* (- i 2))))))
|
||||
|
||||
;; in this context, word only contains 1 or 0
|
||||
(define (entropy word)
|
||||
(let* ((N (string-length word))
|
||||
(num-ones 0)
|
||||
(num-zeros 0))
|
||||
(string-for-each (lambda (c)
|
||||
(if (char=? c #\1)
|
||||
(set! num-ones (+ 1 num-ones))
|
||||
(set! num-zeros (+ 1 num-zeros))))
|
||||
word)
|
||||
(if (or (zero? num-ones) (zero? num-zeros))
|
||||
0
|
||||
(- 0
|
||||
(* (/ num-ones N) (log (/ num-ones N) 2))
|
||||
(* (/ num-zeros N) (log (/ num-zeros N) 2))))))
|
||||
|
||||
;; display values
|
||||
(create-words)
|
||||
(do ((i 1 (+ 1 i)))
|
||||
((= i (vector-length *words*)) )
|
||||
(display (string-append (number->string i)
|
||||
" "
|
||||
(number->string
|
||||
(string-length (vector-ref *words* i)))
|
||||
" "
|
||||
(number->string
|
||||
(entropy (vector-ref *words* i)))
|
||||
"\n")))
|
||||
31
Task/Fibonacci-word/Scilab/fibonacci-word-1.scilab
Normal file
31
Task/Fibonacci-word/Scilab/fibonacci-word-1.scilab
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
exec('.\entropy.sci',0);
|
||||
|
||||
function word=fiboword(n)
|
||||
word_1 = '1'; word_2 = '0';
|
||||
select n
|
||||
case 1
|
||||
word = word_1
|
||||
case 2
|
||||
word = word_2;
|
||||
case 3
|
||||
word = strcat([word_2 word_1]);
|
||||
else
|
||||
word = strcat([fiboword(n-1) fiboword(n-2)])
|
||||
end
|
||||
endfunction
|
||||
|
||||
final_length = 37;
|
||||
|
||||
N=[1:final_length]';
|
||||
char_length = zeros(N);
|
||||
entropies = zeros(N);
|
||||
tic();
|
||||
for i=1:final_length
|
||||
word = fiboword(i);
|
||||
char_length(i) = length(word);
|
||||
entropies(i) = entropy(word);
|
||||
end
|
||||
time = toc();
|
||||
|
||||
disp('EXECUTION TIME: '+string(time)+'s.');
|
||||
disp(['N', 'LENGTH', 'ENTROPY'; string([N char_length entropies])]);
|
||||
33
Task/Fibonacci-word/Scilab/fibonacci-word-2.scilab
Normal file
33
Task/Fibonacci-word/Scilab/fibonacci-word-2.scilab
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
exec('.\entropy.sci',0);
|
||||
|
||||
final_length = 37;
|
||||
|
||||
word_n = '';
|
||||
word_n_1 = '';
|
||||
word_n_2 = '';
|
||||
|
||||
N = [1:final_length]';
|
||||
word_length = zeros(N);
|
||||
entropies = zeros(N);
|
||||
|
||||
tic();
|
||||
for i = 1:final_length
|
||||
if i == 1 then
|
||||
word_n = '1';
|
||||
elseif i == 2
|
||||
word_n = '0';
|
||||
elseif i == 3
|
||||
word_n = '01';
|
||||
word_n_1 = '0';
|
||||
else
|
||||
word_n_2 = word_n_1;
|
||||
word_n_1 = word_n;
|
||||
word_n = word_n_1 + word_n_2;
|
||||
end
|
||||
word_length(i) = length(word_n);
|
||||
entropies(i) = entropy(word_n);
|
||||
end
|
||||
time = toc();
|
||||
|
||||
disp('EXECUTION TIME: '+string(time)+'s.');
|
||||
disp(['N', 'LENGTH', 'ENTROPY'; string([N word_length entropies])]);
|
||||
14
Task/Fibonacci-word/Zkl/fibonacci-word.zkl
Normal file
14
Task/Fibonacci-word/Zkl/fibonacci-word.zkl
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
fcn entropy(bs){ //binary String-->Float
|
||||
len:=bs.len(); num1s:=(bs-"0").len();
|
||||
T(num1s,len-num1s).filter().apply('wrap(p){ p=p.toFloat()/len; -p*p.log() })
|
||||
.sum(0.0) / (2.0).log();
|
||||
}
|
||||
|
||||
" N Length Entropy Fibword".println();
|
||||
ws:=L("1","0");
|
||||
foreach n in ([1..37]){
|
||||
if(n>2) ws.append(ws[-1]+ws[-2]);
|
||||
w:=ws[-1];
|
||||
"%3d %10d %2.10f %s".fmt(n,w.len(),entropy(w),
|
||||
w.len()<50 and w or "<too long>").println();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue