Created
September 3, 2022 14:20
-
-
Save theabbie/6cd1c81719a8cd913a2ef48fd6578543 to your computer and use it in GitHub Desktop.
Radical of a number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
def radical(n): | |
primes = [True] * (n + 1) | |
primes[0] = primes[1] = False | |
for i in range(n + 1): | |
if primes[i]: | |
j = 2 | |
while i * j <= n: | |
primes[i * j] = False | |
j += 1 | |
res = 1 | |
for i in range(n + 1): | |
if primes[i] and n % i == 0: | |
res *= i | |
return res | |
N = 100 | |
buckets = defaultdict(list) | |
for i in range(1, N + 1): | |
buckets[radical(i)].append(i) | |
radicals = sorted(buckets.keys(), key = lambda i: -len(buckets[i])) | |
for i in radicals: | |
vals = ", ".join(map(str, buckets[i])) | |
print(f"{i} => {vals}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment