# _isdivisibleby3() and _isdivisibleby9()

## Recommended Posts

These functions determine if the number is evenly divisible by 3 or 9

usage:

_isdivisibleby3(number)

_isdivisibleby9(number)

returns 0 for no

returns 1 for yes

Functions:

```func _isdivisibleby3(\$number)
\$start = StringSplit( \$number, "")
\$x = UBound(\$start)
\$y = 1
\$div = 0
Do
\$div = \$div + \$start[\$y]
\$y = \$y + 1
Until \$y = \$x
\$test = \$div / 3
\$1 = StringInStr(\$test, ".")
if \$1 = 0 Then
return 1
Else
return 0
EndIf
EndFunc

func _isdivisibleby9(\$number)
\$start = StringSplit( \$number, "")
\$x = UBound(\$start)
\$y = 1
\$div = 0
Do
\$div = \$div + \$start[\$y]
\$y = \$y + 1
Until \$y = \$x
\$test = \$div / 9
\$1 = StringInStr(\$test, ".")
if \$1 = 0 Then
return 1
Else
return 0
EndIf
EndFuncoÝ÷ ØLZ^jëh×6\$result = _isdivisibleby3(5)
MsgBox(64, "Awnser", \$result)

\$result = _isdivisibleby3(6)
MsgBox(64, "Awnser", \$result)

\$result = _isdivisibleby9(15)
MsgBox(64, "Awnser", \$result)

\$result = _isdivisibleby9(18)
MsgBox(64, "Awnser", \$result)

func _isdivisibleby3(\$number)
\$start = StringSplit( \$number, "")
\$x = UBound(\$start)
\$y = 1
\$div = 0
Do
\$div = \$div + \$start[\$y]
\$y = \$y + 1
Until \$y = \$x
\$test = \$div / 3
\$1 = StringInStr(\$test, ".")
if \$1 = 0 Then
return 1
Else
return 0
EndIf
EndFunc

func _isdivisibleby9(\$number)
\$start = StringSplit( \$number, "")
\$x = UBound(\$start)
\$y = 1
\$div = 0
Do
\$div = \$div + \$start[\$y]
\$y = \$y + 1
Until \$y = \$x
\$test = \$div / 9
\$1 = StringInStr(\$test, ".")
if \$1 = 0 Then
return 1
Else
return 0
EndIf
EndFunc```

but any comments, ideas, or suggestions would be appreciated.

Edited by emoyasha

Spoiler

Admin Of:http://notmyspace.info [Under Development, looking for volunteers to help improve]http://PSNetCards.co.ukhttp://ZacnAndLindsey.com [Under development, not quite sure what to do with it yet]http://revelm.com------------------------------------Radio Streams:http://75.185.53.88:8000 [128kb/s 44kHz]http://75.185.53.88:8002 [22kb/s 22kHz](works on mobile phones)-----------------------------------My Server:Owned By: http://jumpline.comIP:66.84.19.220Bandwidth:200GBStorage Space:1TBNetwork Connection: 1GB/S[up and down]Operating System: Red Hat LinuxInstalled Apps:Webmail, phpBB, Majordomo, phpMyAdmin, MySQL, Active Server Pages, FrontPage Extensions 2002, GraphicsMagick, Mod Perl, Perl, PHP: Hypertext Preprocessor, Python(want cheap good webhosting, or need a place to park your domain? contact me)-----------------------------------

##### Share on other sites

Might want to look at Mod in the help file.

Returns True for yes it is divisible, and False for No it isn't

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

##### Share on other sites

had no idea =] just did this for fun though =]

but thanks for pointing out that function i had no idea it existed.

Spoiler

Admin Of:http://notmyspace.info [Under Development, looking for volunteers to help improve]http://PSNetCards.co.ukhttp://ZacnAndLindsey.com [Under development, not quite sure what to do with it yet]http://revelm.com------------------------------------Radio Streams:http://75.185.53.88:8000 [128kb/s 44kHz]http://75.185.53.88:8002 [22kb/s 22kHz](works on mobile phones)-----------------------------------My Server:Owned By: http://jumpline.comIP:66.84.19.220Bandwidth:200GBStorage Space:1TBNetwork Connection: 1GB/S[up and down]Operating System: Red Hat LinuxInstalled Apps:Webmail, phpBB, Majordomo, phpMyAdmin, MySQL, Active Server Pages, FrontPage Extensions 2002, GraphicsMagick, Mod Perl, Perl, PHP: Hypertext Preprocessor, Python(want cheap good webhosting, or need a place to park your domain? contact me)-----------------------------------

##### Share on other sites

hmmm - nothing original in this ... it would have been if you used an arythmetic trick:

- divide by 3 = if the sum of all its digits is dividing by 3 then the number itself divides by 3 (213 -> 2+1+3 = 6 -> divisible by 3 ... and so on)

- divide by 9 = if the sum of all its digits is dividing by 9 then the number itself divides by 9 (425637 -> 4+2+5+6+3+7 = 27 -> divisible by 9 ...)

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

##### Share on other sites

You could also optimize for testing division by 9 by simply adding all the numbers together, then adding them again, until you get a single digit number. If it's 9, then the number is divisible by 9.

Example = 989181994522221 is divisible by 9, because

9+8+9+1+8+1+9+9+4+5+2+2+2+2+1 = 72

7 + 2 = 9

That would make large numbers like 989181994522221 817263543627333636363732732732 easily testable

##### Share on other sites

You could also optimize for testing division by 9 by simply adding all the numbers together, then adding them again, until you get a single digit number. If it's 9, then the number is divisible by 9.

Example = 989181994522221 is divisible by 9, because

9+8+9+1+8+1+9+9+4+5+2+2+2+2+1 = 72

7 + 2 = 9

That would make large numbers like 989181994522221 817263543627333636363732732732 easily testable

Wow, I had never seen that.. It's pretty cool. I haven't tested it though..

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

##### Share on other sites

Wow, I had never seen that.. It's pretty cool. I haven't tested it though..

You can test it for as long as you want - IT IS true

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

##### Share on other sites

I understand that Mod is already out there...

And that trick would be great if you're doing a problem on paper...

But isn't this one-liner the obvious and ancient method to get the result?

```Dim \$result

\$divisor = 3
\$x = 5
_isdivisibleby(\$x,\$divisor)
MsgBox(64, "Awnser", \$result)

\$x = 6
_isdivisibleby(\$x,\$divisor)
MsgBox(64, "Awnser", \$result)

\$divisor = 9
\$x = 15
_isdivisibleby(\$x,\$divisor)
MsgBox(64, "Awnser", \$result)

\$x = 18
_isdivisibleby(\$x,\$divisor)
MsgBox(64, "Awnser", \$result)

;------------------------------------------------------

Func _isdivisibleby(\$x,\$divisor)
\$result = (\$x/\$divisor = int(\$x/\$divisor))
Endfunc```
Edited by Spiff59

##### Share on other sites

It's no more obvious than using Mod(). What you're essentially testing in your function is whether the decimal is .000000, but that's exactly equivalent to testing whether the remainder (Mod()) is 0, concept-wise. The only thing that's less obvious about Mod() is the name, but simply because the person needing to find the remainder might not actually know the operation is called (or handled by) the modulo operator.

FWIW, you could still optimize your check slightly using IsInt(\$x/\$divisor), as you're dividing the number twice in your snippet (redundant), and that's exactly what you're trying to test anyway

Edit: Interestingly, the "IsInt(\$x/\$y)" method scales a bit better than the "Not Mod(\$x, \$y)" method, performance-wise (negligible difference, but oh well ). Not sure if there's any difference accuracy-wise, though (floating point precision stuff, perhaps). Results of a simple/tight For loop repeated 1000000 times:

IsInt(\$x/\$y): 1921.85 ms

Not Mod(\$x, \$y): 2171.50 ms

0 = Mod(\$x, \$y): 2357.92 ms

\$x/\$y = Int(\$x/\$y): 2459.93 ms

[Mod] Returns True for yes it is divisible, and False for No it isn't

Not quite -- it returns the actual remainder, which can then be tested for parameter divisibility. Interpreting the returned numbers directly, "True" (non-zero) is not divisible, and "False" (zero) is divisible Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

##### Share on other sites

you're dividing the number twice in your snippet (redundant)

Purposely. I'd considered dividing \$x and \$y into a third floating point variable, then using that on both sides of the compare, but decided I would offer just the standard coding formula, for clarity.

I haven't been around here very long <blush> and wasn't aware of IsInt.

I'm betting it also gets to an internal routine to shift some bits and perfom the compare in less ticks than a compare coded directly into the source. I wonder how many of those rescued clock cycles are from suppressing the second divide, and how many from replacing the compare with IsInt?

Where does this score in your time trials?

```\$z = \$x/\$y
If (\$z = Int(\$z)) Then```
Edited by Spiff59

##### Share on other sites

Terribly (relatively, anyhow): 2888.03 ms

It's not that surprising, as I've always observed variable assignment (for caching results) to have slightly more overhead than just being a little redundant with simple operations. False optimization of sorts in these cases, I suppose

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

## Create an account

Register a new account