43 lines
1.4 KiB
Plaintext
43 lines
1.4 KiB
Plaintext
include "NSLog.incl"
|
|
|
|
NSUInteger local fn Minimum( a as NSUInteger, b as NSUInteger )
|
|
return (a < b) ? a : b
|
|
end fn = 0
|
|
|
|
UInt64 local fn HammingNumberAtPosition( position as NSUInteger )
|
|
if (position == 0) then return 0
|
|
CFMutableArrayRef hammingNumbers = fn MutableArrayWithCapacity( position )
|
|
MutableArrayAddObject( hammingNumbers, @1 )
|
|
|
|
NSUInteger i2 = 0, i3 = 0, i5 = 0
|
|
for NSUInteger i = 1 to position - 1
|
|
NSUInteger nxt2 = fn NumberUnsignedLongLongValue( hammingNumbers[i2] ) * 2
|
|
NSUInteger nxt3 = fn NumberUnsignedLongLongValue( hammingNumbers[i3] ) * 3
|
|
NSUInteger nxt5 = fn NumberUnsignedLongLongValue( hammingNumbers[i5] ) * 5
|
|
NSUInteger nxt = fn Minimum( nxt2, fn Minimum( nxt3, nxt5 ) )
|
|
|
|
NSUInteger nextHamming = fn Minimum( nxt2, fn Minimum( nxt3, nxt5 ) )
|
|
MutableArrayAddObject( hammingNumbers, @(nextHamming) )
|
|
|
|
if (nxt == nxt2) then i2++
|
|
if (nxt == nxt3) then i3++
|
|
if (nxt == nxt5) then i5++
|
|
next
|
|
|
|
return fn NumberUnsignedLongLongValue( hammingNumbers[position - 1] )
|
|
end fn = 0
|
|
|
|
local fn RunHammingNumberTests
|
|
CFMutableArrayRef mutArr = fn MutableArrayNew
|
|
|
|
for NSUInteger i = 1 to 20
|
|
MutableArrayAddObject( mutArr, @(fn HammingNumberAtPosition(i)) )
|
|
next
|
|
NSLog( @"First 20 Hamming Numbers: %@", fn ArrayComponentsJoinedByString( mutArr, @" " ) )
|
|
NSLog( @" 1691st Hamming Number: %llu", fn HammingNumberAtPosition( 1691 ) )
|
|
end fn
|
|
|
|
fn RunHammingNumberTests
|
|
|
|
HandleEvents
|