Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
56
Task/Fibonacci-word/Ada/fibonacci-word.adb
Normal file
56
Task/Fibonacci-word/Ada/fibonacci-word.adb
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Strings.Unbounded,
|
||||
Ada.Strings.Unbounded.Text_IO, Ada.Numerics.Long_Elementary_Functions,
|
||||
Ada.Long_Float_Text_IO;
|
||||
use Ada.Text_IO, Ada.Integer_Text_IO, Ada.Strings.Unbounded,
|
||||
Ada.Strings.Unbounded.Text_IO, Ada.Numerics.Long_Elementary_Functions,
|
||||
Ada.Long_Float_Text_IO;
|
||||
|
||||
procedure Fibonacci_Words is
|
||||
|
||||
function Entropy (S : Unbounded_String) return Long_Float is
|
||||
CF : array (Character) of Natural := (others => 0);
|
||||
Len : constant Natural := Length (S);
|
||||
H : Long_Float := 0.0;
|
||||
Ratio : Long_Float;
|
||||
begin
|
||||
for I in 1 .. Len loop
|
||||
CF (Element (S, I)) := CF (Element (S, I)) + 1;
|
||||
end loop;
|
||||
for C in Character loop
|
||||
Ratio := Long_Float (CF (C)) / Long_Float (Len);
|
||||
if Ratio /= 0.0 then
|
||||
H := H - Ratio * Log (Ratio, 2.0);
|
||||
end if;
|
||||
end loop;
|
||||
return H;
|
||||
end Entropy;
|
||||
|
||||
procedure Print_Line (Word : Unbounded_String; Number : Integer) is
|
||||
begin
|
||||
Put (Number, 4);
|
||||
Put (Length (Word), 10);
|
||||
Put (Entropy (Word), 2, 15, 0);
|
||||
if Length (Word) < 35 then
|
||||
Put (" " & Word);
|
||||
end if;
|
||||
New_Line;
|
||||
end Print_Line;
|
||||
|
||||
First, Second, Result : Unbounded_String;
|
||||
|
||||
begin
|
||||
Set_Col (4); Put ("N");
|
||||
Set_Col (9); Put ("Length");
|
||||
Set_Col (16); Put ("Entropy");
|
||||
Set_Col (35); Put_Line ("Word");
|
||||
First := To_Unbounded_String ("1");
|
||||
Print_Line (First, 1);
|
||||
Second := To_Unbounded_String ("0");
|
||||
Print_Line (Second, 2);
|
||||
for N in 3 .. 37 loop
|
||||
Result := Second & First;
|
||||
Print_Line (Result, N);
|
||||
First := Second;
|
||||
Second := Result;
|
||||
end loop;
|
||||
end Fibonacci_Words;
|
||||
91
Task/Fibonacci-word/Fortran/fibonacci-word.f
Normal file
91
Task/Fibonacci-word/Fortran/fibonacci-word.f
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
! Fibonacci word
|
||||
! tested with Intel ifx (IFX) 2025.2.1 20250806 on Kubuntu 25.10
|
||||
! GNU gfortran (Ubuntu 15.2.0-4ubuntu4) 15.2.0 on Kubuntu 25.10
|
||||
! but not VSI Fortran x86-64 V8.7-001 because that compiler does not accept
|
||||
! allocatable character variables
|
||||
!
|
||||
|
||||
program FibonacciWord
|
||||
|
||||
character (len=:), allocatable :: fw1,fw2,fw3
|
||||
integer :: i, j, ones, zeros
|
||||
|
||||
fw1 = '1'
|
||||
fw2 = '0'
|
||||
|
||||
i = 2
|
||||
|
||||
! Print top line and first 2 lines that aren't within the do-loop
|
||||
print *, 'N Length Entropy Fibword'
|
||||
call printLine (1,fw1)
|
||||
call printLine (2,fw2)
|
||||
|
||||
! Print lines 3 through 37 as requested in the task descrtiption
|
||||
do j=3,37
|
||||
!
|
||||
! Use rotating index i instead of moving long strings around:
|
||||
! calculate fw(i) as concatenation of the two other fw's in the correct order.
|
||||
!
|
||||
i = i+1
|
||||
if (i .gt.3) i=1
|
||||
select case (i)
|
||||
case (1)
|
||||
fw1 = fw3 // fw2
|
||||
call printLine (j, fw1)
|
||||
case (2)
|
||||
fw2 = fw1 // fw3
|
||||
call printLine (j, fw2)
|
||||
case (3)
|
||||
fw3 = fw2 // fw1
|
||||
call printLine (j, fw3)
|
||||
end select
|
||||
enddo
|
||||
|
||||
contains
|
||||
|
||||
! =================================================================================================
|
||||
! Print 1 output line: N, the Length of the FibWord, its Entropy, and the word unless it's too long
|
||||
! =================================================================================================
|
||||
subroutine printLine (ii, txt)
|
||||
integer, intent(in) :: ii ! Line number
|
||||
character (len=*),intent(in) :: txt ! The FibWord to print
|
||||
integer :: l, i ! Length, loop index
|
||||
|
||||
real (kind=8) :: count_0,count_1,tot ! Count 0's and 1's in txt
|
||||
real (kind=8) :: Entropy ! Resulting entropy
|
||||
|
||||
count_1 = 0
|
||||
count_0 = 0
|
||||
l = len(txt)
|
||||
do i=1,l
|
||||
if (txt(i:i) .eq. '0') then
|
||||
count_0 = count_0 + 1_8
|
||||
else
|
||||
count_1 = count_1 + 1_8
|
||||
endif
|
||||
end do
|
||||
tot = count_1+count_0
|
||||
|
||||
if (count_1 .eq. 0 .or. count_0 .eq. 0) then ! Calc log(0) is undefined. Use 0
|
||||
Entropy =0.0_8
|
||||
else
|
||||
Entropy = - count_1 / tot * log2( count_1 / tot);
|
||||
Entropy = Entropy - count_0 / tot * log2( count_0 / tot);
|
||||
endif
|
||||
if (l .lt. 60) then
|
||||
write (6,'(i2,X, i8, X, F10.8, X, A )') ii, l, Entropy, txt
|
||||
else
|
||||
write (6,'(i2,X, i8, X, F10.8, X, A )') ii, l, Entropy, '(...)'
|
||||
endif
|
||||
end subroutine printLine
|
||||
|
||||
! =================================================
|
||||
! Helper, Fortran knows LOG and LOG10 but not LOG2.
|
||||
! Just a trivial base change
|
||||
! =================================================
|
||||
function log2 (x) result (r)
|
||||
real (kind=8), intent(in) ::x
|
||||
real (kind=8) ::r
|
||||
r = log(x) / log(2.)
|
||||
end function log2
|
||||
end program FibonacciWord
|
||||
|
|
@ -1,9 +1,10 @@
|
|||
using DataStructures
|
||||
using DataStructures, Printf
|
||||
|
||||
entropy(s::AbstractString) = -sum(x -> x / length(s) * log2(x / length(s)), values(counter(s)))
|
||||
|
||||
function fibboword(n::Int64)
|
||||
# Initialize the result
|
||||
r = Array{String}(n)
|
||||
r = fill("", n)
|
||||
# First element
|
||||
r[1] = "0"
|
||||
# If more than 2, set the second element
|
||||
|
|
@ -17,10 +18,9 @@ end
|
|||
|
||||
function testfibbo(n::Integer)
|
||||
fib = fibboword(n)
|
||||
for i in 1:length(fib)
|
||||
for i in eachindex(fib)
|
||||
@printf("%3d%9d%12.6f\n", i, length(fib[i]), entropy(fib[i]))
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
println(" n\tlength\tentropy")
|
||||
|
|
|
|||
36
Task/Fibonacci-word/Pluto/fibonacci-word.pluto
Normal file
36
Task/Fibonacci-word/Pluto/fibonacci-word.pluto
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
local fmt = require "fmt"
|
||||
|
||||
local function entropy(s)
|
||||
local m = {}
|
||||
for i = 1, #s do
|
||||
local c = s[i]
|
||||
local d = m[c]
|
||||
m[c] = (d) ? d + 1 : 1
|
||||
end
|
||||
local hm = 0
|
||||
for m:keys() as k do
|
||||
local c = m[k]
|
||||
hm += c * math.log(c, 2)
|
||||
end
|
||||
local l = #s
|
||||
return math.log(l, 2) - hm / l
|
||||
end
|
||||
|
||||
local function fibword(n)
|
||||
if n < 2 then return tostring(n) end
|
||||
local a = "1"
|
||||
local b = "0"
|
||||
local i = 3
|
||||
while i <= n do
|
||||
local c = b .. a
|
||||
a, b = b, c
|
||||
i += 1
|
||||
end
|
||||
return b
|
||||
end
|
||||
|
||||
fmt.print("%2s %10s %10s %s", "n", "Length", " Entropy ", "Fib word")
|
||||
for i = 1, 37 do
|
||||
local fw = fibword(i)
|
||||
fmt.print("%2d %10s %0.8f %s", i, fmt.int(#fw), entropy(fw), fmt.abridge(fw, 20))
|
||||
end
|
||||
76
Task/Fibonacci-word/Zig/fibonacci-word.zig
Normal file
76
Task/Fibonacci-word/Zig/fibonacci-word.zig
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
const std = @import("std");
|
||||
|
||||
fn findEntropy(fiboword: []const u8) f64 {
|
||||
// Instead of using a hash map (like std::map in C++), using a flat
|
||||
// array is extremely fast for mapping bytes to frequency counts.
|
||||
var frequencies = [_]usize{0} ** 256;
|
||||
for (fiboword) |c| {
|
||||
frequencies[c] += 1;
|
||||
}
|
||||
|
||||
const numlen: f64 = @floatFromInt(fiboword.len);
|
||||
var infocontent: f64 = 0;
|
||||
|
||||
for (frequencies) |count| {
|
||||
if (count > 0) {
|
||||
const freq = @as(f64, @floatFromInt(count)) / numlen;
|
||||
infocontent += freq * std.math.log2(freq);
|
||||
}
|
||||
}
|
||||
|
||||
// Multiply by -1 as in the original code
|
||||
// Use `+ 0.0` trick to avoid negative zero (-0.0) if infocontent is 0
|
||||
return -infocontent + 0.0;
|
||||
}
|
||||
|
||||
fn printLine(writer: anytype, fiboword: []const u8, n: u32) !void {
|
||||
const entropy = findEntropy(fiboword);
|
||||
|
||||
// Formatting equivalents:
|
||||
// {d:<5} -> std::setw(5) << std::left << n
|
||||
// {d:>12} -> std::setw(12) << std::right << fiboword.size()
|
||||
// {d:<16.13} -> std::setw(16) << std::setprecision(13) << std::left << entropy
|
||||
try writer.print("{d:<5}{d:>12} {d:<16.13}\n", .{ n, fiboword.len, entropy });
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
// A GeneralPurposeAllocator works perfectly for the large strings we will generate.
|
||||
// Fib(37) string length expands to ~24 Megabytes.
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
|
||||
// Print header
|
||||
try stdout.print("{s:<5}{s:>12} {s:<16}\n", .{ "N", "length", "entropy" });
|
||||
|
||||
// We start off by allocating the base strings on the heap so they
|
||||
// can be naturally freed in the memory cycle below without compiler complaints.
|
||||
var first: []const u8 = try allocator.dupe(u8, "1");
|
||||
var n: u32 = 1;
|
||||
try printLine(stdout, first, n);
|
||||
|
||||
var second: []const u8 = try allocator.dupe(u8, "0");
|
||||
n += 1;
|
||||
try printLine(stdout, second, n);
|
||||
|
||||
while (n < 37) {
|
||||
// Concatenate `first` and `second` into a new allocated string
|
||||
const result = try std.mem.concat(allocator, u8, &.{ first, second });
|
||||
|
||||
// C++ uses `.assign()`, which copies the string's characters.
|
||||
// Here we can simply free the oldest string, step the slice references
|
||||
// forward, and save execution time avoiding memory copy overhead!
|
||||
allocator.free(first);
|
||||
first = second;
|
||||
second = result;
|
||||
|
||||
n += 1;
|
||||
try printLine(stdout, result, n);
|
||||
}
|
||||
|
||||
// Clean up our remaining two string references
|
||||
allocator.free(first);
|
||||
allocator.free(second);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue