Five programming problems every Software Engineer should be able to solve in less than 1 hour

Mark Waddingham mark at livecode.com
Sun May 10 11:24:10 EDT 2015


> Based on what I’ve seen in this discussion and info from reading the
> list for several years, LiveCode has difficulty with very large
> numbers. So, that is why you can simply do this for Problem 3.
> Correct?
> function ShowTheFirst100Fibonacci
>    local tTheFirst100Fibonacci
>    put "0,1," into tTheFirst100Fibonacci
> 
>    repeat 98
>       put (item -1 tTheFirst100Fibonacci + item -2
> tTheFirst100Fibonacci) & "," after tTheFirst100Fibonacci
>    end repeat
>    delete char -1 tTheFirst100Fibonacci -- removes trailing comma
>    return tTheFirst100Fibonacci
> end ShowTheFirst100Fibonacci

Yes - as it stands, the engine can only represent integers accurately up 
to 52 bits so that's the maximum of 4503599627370496. This is because 
numbers are all represented internally (in variables) as IEEE double's. 
Above numbers of that size, the 'floating point' aspect starts coming 
into play (at any particular magnitude of number you get 52 bits of 
precision, so numbers get 'less accurate' as they get much bigger, or 
much smaller).

> But, what about something like this for Problem 4…
> function ShowLargestNumber theList
>    local tCharsToUse
>    local tLargestNumber
> 
>    -- create each digit as a separte item
>    repeat for each char theCharToCheck in theList
>       if theCharToCheck is a number then
>          put theCharToCheck & "," after tCharsToUse
>       end if
>    end repeat
>    sort items of tCharsToUse descending numeric
>    delete char -1 tCharsToUse -- remove trailing comma
> 
>    -- remove all commas to make 1 number
>    repeat for each char theCharToCheck in tCharsToUse
>       if theCharToCheck is a number then
>          put theCharToCheck after tLargestNumber
>       end if
>    end repeat
>    return tLargestNumber
> end ShowLargestNumber

I think this is the one that has perhaps caused a bit of consternation. 
That method works for 'most' sequences, but there are some edge cases 
which it doesn't catch. In particular, when you have numbers with the 
same initial prefix, the order they should be chosen in depends on both 
the leading digit and the length of the number.

-- 
Mark Waddingham ~ mark at livecode.com ~ http://www.livecode.com/
LiveCode: Everyone can create apps




More information about the use-livecode mailing list