PPA-8
Question
Write the following functions:
factors
: accept a positive integer \(n\) as argument. Return the set of all factors of \(n\).common_factors
: accept two positive integers \(a\) and \(b\) as arguments. Return the set of common factors of the two numbers. This function must make use offactors
.factors_upto
: accept a positive integer \(n\) as argument. Return a dictD
, whose keys are integers and values are sets. Each integer in the range \([1, n]\), endpoints inclusive, is a key ofD
. The value corresponding to akey
, is the set of all factors ofkey
. This function must use offactors
.
The idea we are trying to bring out here is to make use of pre-defined functions whenever needed.
You do not have to accept input from the user or print output to the console. You just have to write the definition of all three functions. Each test case will correspond to one function call.
Solution
Note that the method to add an element to a set is add
and not append
.
factors
common_factors
If A
and B
are two Pythonic sets, then their intersection is given by A.intersection(B)
. Alternatively, we can use A & B
. Mathematically, this would return \(A \cap B\).
factors_upto