RosettaCodeData/Task/Zeckendorf-number-representation/FutureBasic/zeckendorf-number-representation.basic
2026-04-30 12:34:36 -04:00

49 lines
1.3 KiB
Text

include "NSLog.incl"
CFArrayRef local fn GenerateFibonacciSequenceUpTo( limit as NSUInteger )
NSInteger a = 0
NSInteger b = 1
CFMutableArrayRef fibonacciSequence = fn MutableArrayNew
while ( a <= limit )
MutableArrayAddObject( fibonacciSequence, @(a) )
NSInteger temp = a
a = b
b = temp + b
wend
return fibonacciSequence
end fn = @[]
CFStringRef local fn Zeckendorf( n as NSUInteger, fibonacciSequence as CFArrayRef )
CFMutableStringRef representation = fn MutableStringNew
NSInteger leftover = n
if ( n == 0 ) then return @"0"
for NSInteger i = fn ArrayCount( fibonacciSequence ) - 1 to 2 step -1
NSInteger fib = fn NumberUnsignedIntegerValue( fibonacciSequence[i] )
if ( fib <= leftover )
MutableStringAppendString( representation, @"1" )
leftover -= fib
else
if ( len(representation) > 0 )
MutableStringAppendString( representation, @"0" )
end if
end if
next
return representation
end fn = NULL
local fn DoIt( limit as NSInteger )
CFArrayRef fibonacciSequence = fn GenerateFibonacciSequenceUpTo( limit )
NSLog( @"Decimal Zeckendorf" )
for NSUInteger i = 0 to limit
CFStringRef z = fn Zeckendorf( i, fibonacciSequence )
NSLog( @"%7ld : %@", (long)i, z )
next
end fn
fn DoIt( 20 )
HandleEvents