70 lines
1.7 KiB
Plaintext
70 lines
1.7 KiB
Plaintext
include "NSLog.incl"
|
|
|
|
begin globals
|
|
CFStringRef cur, nex
|
|
end globals
|
|
|
|
|
|
CFStringRef local fn Increment
|
|
CFStringRef ret = cur
|
|
cur = nex
|
|
nex = fn StringWithFormat( @"%@%@", ret, nex )
|
|
end fn = ret
|
|
|
|
double local fn GetEntropy( s as CFArrayRef )
|
|
double entropy = 0.0
|
|
double hist(256)
|
|
NSUInteger i
|
|
|
|
for i = 0 to 255
|
|
hist(i)= 0
|
|
next
|
|
|
|
for CFNumberRef num in s
|
|
hist( fn NumberIntegerValue(num) ) += 1
|
|
next
|
|
|
|
for i = 0 to 255
|
|
if ( hist(i) > 0 )
|
|
double rat = hist(i) / fn ArrayCount( s )
|
|
entropy -= rat * log2(rat)
|
|
end if
|
|
next
|
|
return entropy
|
|
end fn = 0.0
|
|
|
|
CFStringRef local fn ReverseString( string as CFStringRef )
|
|
CFMutableStringRef reversedStr = fn MutableStringNew
|
|
for NSInteger i = len(string) - 1 to 0 step -1
|
|
MutableStringAppendString( reversedStr, fn StringWithFormat( @"%c", fn StringCharacterAtIndex( string, i ) ) )
|
|
next
|
|
end fn = reversedStr
|
|
|
|
local fn DoIt
|
|
cur = @"1"
|
|
nex = @"0"
|
|
|
|
NSLog( @"%5s %10s %11s %33s", "No.", "Length", "Entrophy", "Binary Fibonacci Word" )
|
|
CFTimeInterval t = fn CACurrentMediaTime
|
|
for int i = 0 to 36
|
|
CFStringRef string = fn Increment
|
|
CFMutableArrayRef asciiValues = fn MutableArrayNew
|
|
for NSUInteger j = 0 to len(string) - 1
|
|
unichar character = fn StringCharacterAtIndex( string, j )
|
|
MutableArrayAddObject( asciiValues, @(character) )
|
|
next
|
|
double ent = fn GetEntropy( asciiValues )
|
|
|
|
if ( i <= 10 )
|
|
NSLog( @"%3d. %9lu %19.15f %@", i+1, (unsigned long)len(string), ent, fn ReverseString( string ) )
|
|
else
|
|
NSLog( @"%3d. %9lu %19.15f [length exceeds task limits]", i+1, (unsigned long)len(string), ent )
|
|
end if
|
|
next
|
|
NSLog( @"\nCompute time: %.3f ms",(fn CACurrentMediaTime-t) * 1000 )
|
|
end fn
|
|
|
|
fn DoIt
|
|
|
|
HandleEvents
|