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
  • Last Modified:

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
Asked:
PatternNut
3 Solutions
 
SweatCoderCommented:
IF i % 3 = 0 THEN
   'WAHOO
END IF
0
 
Wayne Taylor (webtubbs)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
 
PaulHewsCommented:
Use Modulus operator:

For i = 1 to 10,000
  if i Mod 3 = 0  then
    'do something
  end if
next i
0
 
SweatCoderCommented:
shoot, i originally meant:

IF i MOD 3 = 0 THEN
   'WAHOO
END IF

(% is for other languages)
0
 
PatternNutAuthor 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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