MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / get_squarefrees

Function get_squarefrees

project_euler/problem_203/sol1.py:63–97  ·  view source on GitHub ↗

Calculates the squarefree numbers inside unique_coefficients. Based on the definition of a non-squarefree number, then any non-squarefree n can be decomposed as n = p*p*r, where p is positive prime number and r is a positive integer. Under the previous formula, any coefficient

(unique_coefficients: set[int])

Source from the content-addressed store, hash-verified

61
62
63def get_squarefrees(unique_coefficients: set[int]) -> set[int]:
64 """
65 Calculates the squarefree numbers inside unique_coefficients.
66
67 Based on the definition of a non-squarefree number, then any non-squarefree
68 n can be decomposed as n = p*p*r, where p is positive prime number and r
69 is a positive integer.
70
71 Under the previous formula, any coefficient that is lower than p*p is
72 squarefree as r cannot be negative. On the contrary, if any r exists such
73 that n = p*p*r, then the number is non-squarefree.
74
75 >>> get_squarefrees({1})
76 {1}
77 >>> get_squarefrees({1, 2})
78 {1, 2}
79 >>> get_squarefrees({1, 2, 3, 4, 5, 6, 7, 35, 10, 15, 20, 21})
80 {1, 2, 3, 5, 6, 7, 35, 10, 15, 21}
81 """
82
83 non_squarefrees = set()
84 for number in unique_coefficients:
85 divisor = 2
86 copy_number = number
87 while divisor**2 <= copy_number:
88 multiplicity = 0
89 while copy_number % divisor == 0:
90 copy_number //= divisor
91 multiplicity += 1
92 if multiplicity >= 2:
93 non_squarefrees.add(number)
94 break
95 divisor += 1
96
97 return unique_coefficients.difference(non_squarefrees)
98
99
100def solution(n: int = 51) -> int:

Callers 1

solutionFunction · 0.85

Calls 1

addMethod · 0.45

Tested by

no test coverage detected