1. more RANT
This problem is much simpler than the Wildcard one (maybe!).
What is the most efficient way to tell if a number divides EVENLY into
another one? For example:
object a,b
A=320 B=321
A/32=10 --10 is a whole number because there is no fraction
B/32=9.x --9.x is a whole number and a fraction
How can I tell if the action taken on A or B is a situation where there is
an EVEN division or an UNEVEN division?
Thanks again
Norm
2. Re: more RANT
> What is the most efficient way to tell if a number divides EVENLY into
> another one? For example:
> object a,b
> A=320 B=321
> A/32=10 --10 is a whole number because there is no fraction
> B/32=9.x --9.x is a whole number and a fraction
I see two methods, off the top of my head...
First would be the remainder function... simple enough...
Second, and perhaps faster??? is testing the result
for "integerness"...
ex:
object result
result = 9/5
if integer(result) then
puts(1,"9/5 is an integer")
else puts((1,"9/5 is a real")
end if
result = 100/10
if integer(result) then
puts(1,"100/10 is an integer")
else puts((1,"100/10 is a real")
end if
The only trick here is that the variable result must be an atom or
object...
hope this helps--Hawke'
3. Re: more RANT
Hi Norm,
-----Original Message-----
Date: Sunday, April 11, 1999 12:24 PM
Subject: more RANT
>This problem is much simpler than the Wildcard one (maybe!).
>What is the most efficient way to tell if a number divides EVENLY into
>another one? For example:
>object a,b
> A=320 B=321
>
>How can I tell if the action taken on A or B is a situation where there is
>an EVEN division or an UNEVEN division?
If you need a true/false function:
function isevendiv(atom num, atom div)
return (remainder(num,div)=0)
end function
example (Assume 0 = False= uneven division, and 1 = True = even division):
? isevendiv(320,10)
1
? isevendiv(32,9)
0
Hope this helps,
Greg Harris
4. Re: more RANT
A simple function would be:
function DivEvenly(atom num, atom den) -- num is Numerator den is
Denominator
return num/den = floor(num/den)
end function
The return value will be zero if it doesn't divide evenly and nonzero if it
does.
So,
If DivEvenly(10, 5) then
puts(1,"Yes")
end if
will print "Yes"
and
If DivEvenly(23, -14) then
puts(1,"Yes")
end if
will print nothing.
This may be faster than using integer() or remainder(), although it does use
floor().
You could also just use the formula num/den = floor(num/den) in the actual
line testing the two numbers instead of putting it into a function. That
might also speed things up.
5. Re: more RANT
> This problem is much simpler than the Wildcard one (maybe!).
> What is the most efficient way to tell if a number divides EVENLY into
> another one? For example:
You can quickly tell if an integer is evenly divisble by a power of two by
using and_bits. Check to see if the results of and_bits with the number
and the power of two minus one is equal to zero.
object a,b
A=320 B=321
if and_bits(A, 31) == 0 then
puts(1, "A is evenly divisible by 32\n")
end if
if and_bits(B, 31) != 0 then
puts(1, "B is not evenly divisible by 32\n")
end if
if and_bits(A, 63) == 0 then
puts(1, "A is evenly divisible by 64\n")
end if
For checking divisiblity by non-powers-of-two, use one of the solutions
already posted. I usually go with the remainder equals zero method.
_______ ______ _______ ______
[ _ \[ _ ][ _ _ ][ _ ]
[/| [_] |[/| [_\][/ | | \][/| [_\]
| ___/ | _] | | | _]
[\| [/] [\| [_/] [\| |/] [\| [_/]
[_____] [______] [_____] [______]
xseal at harborside.com ICQ:13466657
http://www.harborside.com/home/x/xseal/euphoria/
6. Re: more RANT
------=_NextPart_000_0014_01BE8507.E2C6D7A0
charset="iso-8859-1"
I made a little program to test the three proposed tests for even
divisibility. (It is included with this email)
The results were:
1st fastest:
remainder(numerator,denominator) = 0
2nd:
integer(numerator/denominator)
and 3rd: (unfortunately the one I proposed) :)
numerator/denominator = floor(numerator/denominator)
The results on the program will show you time for numbers 1-100, 1-1000,
1-10000, etc. up to 1-1000000
I just hope that there isn't any error in my program that would cause the
results to be false.
------=_NextPart_000_0014_01BE8507.E2C6D7A0
name="EvenDivi.ex"