1. Implement a Python function square(n) that takes a single positive integer ar
ID: 668677 • Letter: 1
Question
1. Implement a Python function square(n) that takes a single positive integer argument n and returns True if and only if n is a perfect square (i.e., there exists an integer k such that k2 = n).
2. Implement a Python function squarefree(n) that takes a single positive integer argument n and returns True if and only if n has no square factors. This happens only when the following logical formula is true for all integers k greater than 1 but less than n:
k | n implies k is not a perfect square
3. Implement a Python function properSquareFactors(n) that takes a single positive integer argument n and returns a finite set of integers containing all the square proper factors of that number.
4. Implement a Python function shared(S) that takes any finite set of positive integers S and returns a relation over S in which any two numbers in S that share at least one square proper factor are related. If a number has no proper square factors, then it shares no proper square factors with any other number (including itself).
5. Determine which of the three properties of an equivalence relation (reflexivity, symmetry, and transitivity) always apply to any result of shared() (assuming its input is a finite set of positive integers). Define three veriables in your code; each will represent a counterexample input (if it exists) that leads to an output that does not have the corresponding property:
reflexive = ?
symmetric = ?
transitive = ?
If a property always applies to any output of shared(), simply assign None to the corresponding variable. If a property does not apply to all outputs of shared(), assign an input set to the corresponding variable that returns an output relation that does not satisfy that property. For example, if we were doing the same thing for the property of asymmetry, we would choose a value for asymmetric such that:
>>> isAsymmetric(asymmetric, shared(asymmetric))
False
k | n implies k is not a perfect square
Explanation / Answer
1.
>>> square(16)
True
>>> square(12)
False
2.>>> squarefree(39)
True
>>> squarefree(16)
False
>>> squarefree(20)
False
3.>>> properSquareFactors(18)
{9}
>>> properSquareFactors(108)
{4, 9, 36}
4.
>>> shared({4,5,6,7,8,9,10,11,12})
{(8,8), (8,12), (12,8), (12,12)}
>>> shared({12,18,28})
{(12,12), (12,28), (18,18), (28,12), (28,28)}
>>> shared({11,12})
{(12,12)}
5.>>> isAsymmetric(asymmetric, shared(asymmetric))
False
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.