184

I need to test whether each number from 1 to 1000 is a multiple of 3 or a multiple of 5.

I tried this code in Python 2.x:

n = 0 s = 0 while (n < 1001): x = n/3 if isinstance(x, (int, long)): print 'Multiple of 3!' s = s + n if False: y = n/5 if isinstance(y, (int, long)): s = s + n print 'Number: ' print n print 'Sum:' print s n = n + 1 

The idea is to try dividing the number and see if the result is an integer. However, I'm not getting the expected result.

How do I test whether the number is an integer?


In 2.x, division like this will produce an integer, discarding the remainder; see How can I force division to be floating point? Division keeps rounding down to 0? for details.

In 3.x, the division will produce a floating-point value; the result is not "an integer" even if it is a whole number, so the isinstance check will fail. See Why does integer division yield a float instead of another integer? for details.

If you need the remainder from integer division rather than just testing for divisibility, see Find the division remainder of a number.

4
  • 2
    In Python 2.x, integer division always yields an integer. Commented Nov 3, 2011 at 22:19
  • 28
    You should be printing "Fizz" and "Buzz" Commented Nov 3, 2011 at 23:21
  • 13
    Project Euler Problem 1 ? Commented Jan 30, 2013 at 13:56
  • This is a special case of finding the remainder when doing integer division. Commented Oct 27, 2022 at 19:20

7 Answers 7

359

You do this using the modulus operator, %

n % k == 0 

evaluates true if and only if n is an exact multiple of k. In elementary maths this is known as the remainder from a division.

In your current approach you perform a division and the result will be either

  • always an integer if you use integer division, or
  • always a float if you use floating point division.

It's just the wrong way to go about testing divisibility.

Sign up to request clarification or add additional context in comments.

3 Comments

0 % 5 == 0 would be True as well, but zero is not exact multiple of zero.
@Fusion 0 == 0*5
@Chris_Rands Put that into an answer, people who do code-golf (like me) would be very happy. In fact, I came here looking for a golfed answer like that.
33

You can simply use % Modulus operator to check divisibility.
For example: n % 2 == 0 means n is exactly divisible by 2 and n % 2 != 0 means n is not exactly divisible by 2.

Comments

3

I had the same approach. Because I didn't understand how to use the module (%) operator.

6 % 3 == 0 
  • This means if you divide 6 by 3 you will not have a remainder, 3 is a factor of 6.

Now you have to relate it to your given problem.

if n % 3 == 0 
  • This is saying, if my number (n) is divisible by 3 leaving a 0 remainder.

Add your then (print, return) statement and continue your

Comments

2

You can use % operator to check divisiblity of a given number

The code to check whether given no. is divisible by 3 or 5 when no. less than 1000 is given below:

n=0 while n<1000: if n%3==0 or n%5==0: print n,'is multiple of 3 or 5' n=n+1 

Comments

1

This code appears to do what you are asking for.

for value in range(1,1000): if value % 3 == 0 or value % 5 == 0: print(value) 

Or something like

for value in range(1,1000): if value % 3 == 0 or value % 5 == 0: some_list.append(value) 

Or any number of things.

Comments

-2
a = 1400 a1 = 5 a2 = 3 b= str(a/a1) b1 = str(a/a2) c =b[(len(b)-2):len(b)] c1 =b[(len(b1)-2):len(b1)] if c == ".0": print("yeah for 5!") if c1 == ".0": print("yeah for 3!") 

3 Comments

Its better to explain the fix you have given and what made it worked
That is NOT a good way to test divisibility: doing a float division, converting to a string and then doing string manipulations to find if the fractional part is (literally) ".0" is at the very least inefficient, and possibly wrong depending on the floating point implementation and the code that does the conversions.
E.g. try x=10000000000000000; b = str(x/(x-1)); b in the python interpreter.
-4

For small numbers n%3 == 0 will be fine. For very large numbers I propose to calculate the cross sum first and then check if the cross sum is a multiple of 3:

def is_divisible_by_3(number): if sum(map(int, str(number))) % 3 != 0: my_bool = False return my_bool 

2 Comments

Is calculating the cross sum and using the modulus operation really faster than directly using the modulus operation? If so, shouldn't you call your function recursively until the number is "small" enough?
This is in fact extremely inefficient - so inefficient that by default, recent Python versions will outright refuse to perform the operation for large inputs, as large string/int conversion is considered a vector for denial of service attacks. By default, the str call will raise a ValueError for inputs where the result would be longer than 4300 decimal digits.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.