Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 726

# Fastest way to check if a number is divisible by three?

Hi experts,

What's the best way to tell if a number is evenly divisible by three?  For instance, I have been doing it like this:

For i = 1 to 10,000
if i / 3 = i \ 3 then
'do something
end if
next i

That's quite neat... but I don't think it's very fast because the computer hates the / symbol.  I could do it using the ROUND function as well, but that is also slow.  I suppose I could also do:
blnfirstmatch = true
blnsecondmatch = false

For i = 1 to 10,000
if blnFirstMatch = true then
blnFirstMatch = false
blnSecondMatch = true
elseif blnSecondMatch = true then
blnSecondMatch = false
else
blnFirstMatch = true
'do something
end if
next i

There are probably other ways too.  If any experts can let me know what is definitely the fastest way, I would be very very grateful.

Thank you,

PatternNut
0
PatternNut
3 Solutions

Commented:
IF i % 3 = 0 THEN
'WAHOO
END IF
0

Commented:
Hi PatternNut,

Use Mod....

For i = 1 to 10,000
if i Mod 3 = 0 then
'do something
end if
next i

Regards,

Wayne
0

Commented:
Use Modulus operator:

For i = 1 to 10,000
if i Mod 3 = 0  then
'do something
end if
next i
0

Commented:
shoot, i originally meant:

IF i MOD 3 = 0 THEN
'WAHOO
END IF

(% is for other languages)
0

Author Commented:
Damn... superfast guys, thanks.

I split poiints as all three were so quick it's hard to say one is better than the other.  I gave highest points to Wayne since you all agreed with his answer and the board suggests that was the first one that was 100% correct  I hope that's fair.

Thanks a bunch, that's a really big help and I really appreciate it.

Cheers,

PatternNut
0

## Featured Post

Tackle projects and never again get stuck behind a technical roadblock.