This repository has been archived by the owner on Jun 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loops_p2.py
41 lines (36 loc) · 1.66 KB
/
loops_p2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# This function counts the number of integer factors for a
# "given_number" variable, passed through the function’s parameters.
# The "count" return value includes the "given_number" itself as a
# factor (n*1).
def count_factors(given_number):
# To include the "given_number" variable as a "factor", initialize
# the "factor" variable with the value 1 (if the "factor" variable
# were to start at 2, the "given_number" itself would be excluded).
factor = 1
count = 1
# This "if" block will run if the "given_number" equals 0.
if given_number == 0:
# If True, the return value will be 0 factors.
return 0
# The while loop will run while the "factor" is still less than
# the "given_number" variable.
while factor < given_number:
# This "if" block checks if the "given_number" can be divided by
# the "factor" variable without leaving a remainder. The modulo
# operator % is used to test for a remainder.
if given_number % factor == 0:
# If True, then the "factor" variable is added to the count of
# the "given_number"’s integer factors.
count += 1
# When exiting the if block, increment the "factor" variable by 1
# to divide the "given_number" variable by a new "factor" value
# inside the while loop.
factor += 1
# When the interpreter exits either the while loop or the top if
# block, it will return the value of the "count" variable.
return count
print(count_factors(0)) # Count value will be 0
print(count_factors(3)) # Should count 2 factors (1x3)
print(count_factors(10)) # Should count 4 factors (1x10, 2x5)
print(count_factors(24)) # Should count 8 factors (1x24, 2x12, 3x8,
# and 4x6).