0
def is_multiple(m, n):
    """
      >>> is_multiple(12, 3)
      True
      >>> is_multiple(12, 4)
      True
      >>> is_multiple(12, 5)
      False
      >>> is_multiple(12, 6)
      True
      >>> is_multiple(12, 7)
      False
    """

    c = m / n
    d = int(m / n)
    if (c-d)== 0:
        return True
    elif (c-d)!= 0:
        return False

I am trying to make a function that decides if m is a multiple of n. I can only use what I have learned which is functions, if/else, if/elif/else statements, Boolean expressions and simple things like that. I have not gotten to for, while, do/while expressions yet. My code keeps returning True for everything which it is false in two cases. So why does it keep returning True, and also is there a more simpler way to write a function for finding a multiple of a number?

Updated code:

def is_factor(f,n):
    """
      >>> is_factor(3, 12)
      True
      >>> is_factor(5, 12)
      False
      >>> is_factor(7, 14)
      True
      >>> is_factor(2, 14)
      True
      >>> is_factor(7, 15)
      False
    """
    if n % f == 0:
        return True
    else:
        return False

So that code can be used to find a multiple of a number also?

Correct updated code that produces correct result:

def is_multiple(m, n):
    """
      >>> is_multiple(12, 3)
      True
      >>> is_multiple(12, 4)
      True
      >>> is_multiple(12, 5)
      False
      >>> is_multiple(12, 6)
      True
      >>> is_multiple(12, 7)
      False
    """

    c = float (m) / n
    d = m / n
    if (c-d)== 0:
        return True
    else:
        return False
Randy Goldsmith
  • 327
  • 3
  • 17
  • I did a quick test in python and found out why it is always returning true. Whenever I convert (m /n) into an int it will always give me c which will always be 0 so everything will be True. In that case, I have no idea how to make a function to try and find a multiple. – Randy Goldsmith Jul 08 '15 at 22:41
  • Even when using floats it will always return True because in the variable d I am always converting it into an integer so c and d will always be the same number that equals 0 correct? – Randy Goldsmith Jul 08 '15 at 22:45
  • Did you try to adapt the solution from the above link? – vaultah Jul 08 '15 at 22:45
  • Vaultah I looked at the example and it uses while loops which I have not learned and can not use yet. It gives me a hint at the modulus operator though. – Randy Goldsmith Jul 08 '15 at 22:48
  • The top answer: "You do this using the modulus operator, %". It does not even mention any loops. You *don't need* any loops. – vaultah Jul 08 '15 at 22:49
  • So what is the difference between finding a multiple of a number and finding a factor of a number syntactically? – Randy Goldsmith Jul 08 '15 at 22:51
  • @RandyGoldsmith Note that `is_multiple(12.0, 7)` will work fine. That's because you force `m / n` to do *float* divison. So changing `c = float(m) / n` will do the trick. That's one way to do that. BTW: you can remove all those ifs and do simple `return c-d == 0`. – freakish Jul 08 '15 at 22:53
  • When you divide an int by an int, it always returns an int. You need to convert `m` or `n` to floats before dividing. – Barmar Jul 08 '15 at 22:55
  • You also shouldn't use `elif` when the second condition is just the opposite of the first condition. Just use `else`. – Barmar Jul 08 '15 at 22:56
  • @Barmar `m` or `n`, just one of them will suffice – Dleep Jul 08 '15 at 22:56

2 Answers2

1

When you divide in , the division is rounded. Instead, import division from __future__ at the top of your file:

from __future__ import division
def is_multiple(m, n):
    """
      >>> is_multiple(12, 3)
      True
      >>> is_multiple(12, 4)
      True
      >>> is_multiple(12, 5)
      False
      >>> is_multiple(12, 6)
      True
      >>> is_multiple(12, 7)
      False
    """
    c = m / n
    d = int(m / n)
    if (c-d)== 0:
        return True
    elif (c-d)!= 0:
        return False

>>> 12/5 == 12/6
True
>>> 12/6
2
>>> 12/5
2
>>> from __future__ import division #To allow for unrounded division
>>> 12/5 == 12/6
False
>>> 12/5
2.4
>>> 12/6
2.0

You could also use modulo %:

Modulo % divided the two numbers, and returns the remainder:

>>> 12 % 7 #12 divided y 7 is 1 remainder 5, the 5 is returned
5
>>> 12 % 6 #12 divided y 7 is 2 remainder 0, the 0 is returned
0
>>> 12 % 5 #12 divided y 5 is 2 remainder 1, the 1 is returned
2
>>> 

Thus, whenever the remainder is 0, the second number goes into the first one perfectly, which simplifies your function to:

def is_multiple(m, n):
    return m % n == 0
AJ Uppal
  • 136
  • 6
0

Your is_factor looks correct. You could simplify it to

def is_factor(f, n):
    return n % f == 0

If you want to check if x is a multiple of y, this is the same as saying y is a factor of x. Thus, you can do

def is_multiple(m, n):
    return is_factor(n, m)
James
  • 1,198
  • 8
  • 14
  • You also answered the next problem in my book that asks "how could you use the function is_factor inside the function is_multiple. It is still a little confusing but I get it enough. They seem like they are inverses of each other but this does answer all my questions. Very helpful again thanks – Randy Goldsmith Jul 08 '15 at 23:17