Skip to content

Instantly share code, notes, and snippets.

@theabbie
Created September 3, 2022 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theabbie/6cd1c81719a8cd913a2ef48fd6578543 to your computer and use it in GitHub Desktop.
Save theabbie/6cd1c81719a8cd913a2ef48fd6578543 to your computer and use it in GitHub Desktop.
Radical of a number
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