How do you check if a number is divisible by another number (Python)?

I need to check if each number from 1 to 1000 is a multiple of 3 or a multiple of 5. As I thought, I would do this to divide the number by 3, and if the result is an integer then it will be a multiple of 3. Same with 5 .

How to check if a number is an integer?

here is my current code:

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 
+89
python integer modulus
Nov 03 2018-11-11T00:
source share
8 answers

You do this using the module operator, %

 n % k == 0 

evaluates the truth if and only if n is an exact multiple of k . In elementary mathematics, this is called the remainder of division.

In your current approach, you are doing the division, and the result will be either

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

This is just the wrong way to check for divisibility.

+191
Nov 03 2018-11-11T00:
source share

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

You can also see the operators used in programming.

+3
May 26 '15 at
source share

You can use the % operator to check the divisibility of a given number

Code to check if this is indicated. divided by 3 or 5 when not. Below 1000 is shown 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 
0
May 15 '15 at 13:18
source share

This code seems to do what you ask 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.

0
Jan 04 '17 at 21:00
source share

For small numbers, n%3 == 0 would be fine. For very large numbers, I suggest first calculating the cross sum and then checking 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 
-3
Aug 09 '14 at 6:24
source share

jinja2 template fizzbuz:

 <form> <ol> {% for x in range(1,n+1) %} {% set fizzbuzz_rpm = x %} {% if x % 3 == 0 and x % 5 == 0 %} {% set fizzbuzz_rpm="FizzBuzz" %} {% elif x % 3 == 0 %} {% set fizzbuzz_rpm="Fizz" %} {% elif x %5 == 0 %} {% set fizzbuzz_rpm="Buzz" %} {% endif %} <li>{{fizzbuzz_rpm}}</li> {% endfor %} </ol> </form> 
-6
Jan 05 '17 at 16:49
source share

The easiest way is to check if the number is an integer int(x) == x . Otherwise what David Heffernan said.

-7
Nov 03 2018-11-11T00:
source share

Try it...

 public class Solution { public static void main(String[] args) { long t = 1000; long sum = 0; for(int i = 1; i<t; i++){ if(i%3 == 0 || i%5 == 0){ sum = sum + i; } } System.out.println(sum); } } 
-7
Dec 30 '16 at 9:54
source share



All Articles