2. Multiple or factor

VC2M5N10: level 5: Follow a mathematical algorithm involving branching and repetition (iteration); create and use algorithms involving a sequence of steps and decisions and digital tools to experiment with factors, multiples and divisibility; identify, interpret and describe emerging patterns
  • creating algorithms that use multiplication and division facts to determine if a number is a multiple or factor of another number; for example, using a flow chart that determines whether numbers are factors or multiples of other numbers using branching, such as yes/no decisions


2.1. Pseudocode

The pseudocode to determine if one number is a factor or multiple of a second number.
FUNCTION is_multiple_or_factor(a, b)
    IF a = 0 OR b = 0 THEN
        RETURN "Do not use 0 for a or b"
    ENDIF

    IF b % a = 0 THEN
        RETURN a + " is a factor of " + b
    ELSEIF a % b = 0 THEN
        RETURN a + " is a multiple of " + b
    ELSE
        RETURN a + " is neither factor nor multiple of " + b
    ENDIF
END FUNCTION

2.2. Multiple or factor code

The python to determine if one number is a factor or multiple of a second number.
 1
 2def is_multiple_or_factor(a, b):
 3    """Check if a is a multiple or a factor of b.
 4
 5    Parameters:
 6    a (int): The first number
 7    b (int): The second number
 8
 9    Returns:
10    str: A string indicating the relationship between a and b
11    """
12    # Check for 0 values of a or b
13    if a == 0 or b == 0:
14        return "Do not use 0 for a or b"    
15    if b % a == 0:
16        # a is a factor of b
17        return f'{a} is a factor of {b}'
18    elif a % b == 0:
19        # a is a multiple of b
20        return f'{a} is a multiple of {b}'
21    # Otherwise, return "neither", since neither condition is met
22    else:
23        return f'{a} is neither factor nor multiple of {b}'
24
25# Test the function with some examples
26a, b = 6, 18
27print(is_multiple_or_factor(a, b)) # 6 is a factor of 18
28a, b = 20, 4
29print(is_multiple_or_factor(a, b))  # 20 is a multiple of 4
30a, b = 9, 2
31print(is_multiple_or_factor(a, b))  # 9 is neither factor nor multiple of 2
32a, b = 2, 0
33print(is_multiple_or_factor(a, b))  # Do not use 0 for a or b
34
35