odd and even numbers
Paul D. DeRocco
pderocco at ix.netcom.com
Mon Aug 5 18:53:10 EDT 2013
> From: Alex Tweedly
>
> Can you please define the problem more precisely ?
>
> It seems to me there are 3 possible answers to come out from any
> solution (or maybe more)
>
> 1 --> odd
> 2 --> even
> 2.5 --> neither odd nor even (or, if you prefer, "not whole")
>
> AFAICS, all the proposed solutions so far have been
> incomplete, because
> they failed to produce "neither" (i.e. they tested for
> evenness, so if
> not even then you'd have to assume it was odd).
I thought the original question stated the assumption that the number was
whole. Normally, one tests to see if a number is an integer by comparing it
to its integer part. In LC you just ask if it "is an integer".
It's interesting how people approach things differently when they're using
an interpreted scripting language. For instance, in C, one would never
divide by two and then see if the remainder is nonzero, because a divide is
a comparatively slow operation; one would use a bitwise AND, since that is
typically a single-clock op. But in LC, that difference is swamped by the
time spent processing the script.
And in C, one would NEVER base the analysis on the printed representation,
because that would involve generating the printed representation. I don't
know how LC represents things, but I would think that it would represent
values by the combination of a numeric representation and a string
representation, either one of which (but not both) may be missing, deferring
generation until it is needed. If that's the case, just asking for the last
character of a number might very well cause the representation to be
generated, which would be significantly costlier than doing arithmetic on
it.
--
Ciao, Paul D. DeRocco
Paul mailto:pderocco at ix.netcom.com
More information about the use-livecode
mailing list