helo guys good day...

i have a problem in python programming... im new to this langauage..

im having a bit problem on creating a program in python that would determine if the integer is divisible by 3 or not without using a modulus?


im glad if you can help me .... im willing to know more about this language...

thanks...... =)

Modulus operator is the right thing to use. Why not? However, here's another way:

def divisibleBy3theHardWay(anInt):
  while anInt > 10:
    sum = 0
    for c in str(anInt):
      sum += int(c) # I'm assuming anInt is indeed an int
    anInt = sum
  return anInt in (0,3,6,9)

This method is also known as 'striking out threes', and it is much much slower than using the '%' operator on the int.

Exercise for the reader: Prove this is a valid test for 'divisible by three'. Hint: what is (10^N)%3?

Divide by 3, multiply the result by 3, and compare to the original number, assuming that you are using integers only.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.