[DSA in Python] is_multiple.py
This was the first exercise of Chapter 1 (Python Primer) in the book 'Data Structures and Algorithms in Python' by Goodrich et al.
Problem: "Write a short Python function, is_multiple(n, m), that takes two integer values and returns True if n is a multiple of m, that is, n = mi for some integer i, and False otherwise."
Solution:
There are some different variations of creating this function, but it boils down to checking on whether an operation behaves in a certain way.
The function will return the truthness or falseness of m % n == 0. If m % n is equal to 0, then we know that m is divisible by n. This means that m must also be a multiple of n.
Remarks:
Problem: "Write a short Python function, is_multiple(n, m), that takes two integer values and returns True if n is a multiple of m, that is, n = mi for some integer i, and False otherwise."
Solution:
There are some different variations of creating this function, but it boils down to checking on whether an operation behaves in a certain way.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_multiple(n, m): | |
return m % n == 0 |
Remarks:
- The % is a modulo operator, which returns the remainder that occurs from the division of m / n.
- By divisibility, a number divided by another number must not leave a remainder.
Comments
Post a Comment