The squarefree part of a positive integer is the largest divisor of that integer which itself has no square factors (other than 1). For example, the squareefree part of 12 is 6, since the only larger divisor is 12, and 12 has a square factor (namely, 4).

Your challenge, should you choose to accept it, is to implement a `squareFreePart` function which accepts a number `n` and returns the squarefree part of `n`. 

In the case that `n = 1`, your function should return 1. Also, if the input doesn't make sense (e.g. if it is not a positive integer), the function should return `null/None`. 

Here are some examples:

```python
square_free_part(2) # returns 2
square_free_part(4) # returns 2
square_free_part(24) # returns 6, since any larger divisor is divisible by 4, which is a square
square_free_part("hi") # returns None
square_free_part(0) # returns None
```

Good luck!

Difficulty: EASY
Tags: Number theory

Mathematics

Starter code:
def square_free_part(n):
	

Solutions:
def square_free_part(n):
	if type(n) != int or n < 1:
		return None
	for i in xrange(2, int(n ** 0.5) + 1):
		while n % i ** 2 == 0:
			n /= i
	return n


def square_free_part(n):
	fac = lambda n: [i for i in range(2, n // 2 + 1) if n % i == 0] + [n]
	issquare = lambda n: True if str(n ** 0.5)[-1] == '0' else False
	if isinstance(n, bool):
		return None
	if isinstance(n, int) and n > 0:
		if n == 1:
			return 1
		if n == 2:
			return 2
		for i in sorted(fac(n), reverse=True):
			if sum([issquare(f) for f in fac(i)]) == 0:
				return i
	else:
		return None


def square_free_part(n):
	if type(n) != int or n < 1:
		return
	while n % 4 == 0:
		n //= 2
	k = 3
	while k * k <= n:
		while n % (k * k) == 0:
			n //= k
		k += 2
	return n


def square_free_part(n):
	if type(n) == int and n > 0:
		part = 1
		for p in range(2, int(n ** 0.5) + 1):
			if not n % p:
				part *= p
				while not n % p:
					n //= p
		return part * n


def square_free_part(n):
	if type(n) != int or n <= 0:
		return None
	(r, d) = (1, 2)
	while d * d <= n:
		if n % d == 0:
			while n % d == 0:
				n //= d
			r *= d
		d += 1
	return r * n if n > 1 else r


def square_free_part(n):
	import math
	lst = []
	squares = []
	x = []
	if isinstance(n, int):
		if n > 1:
			for i in range(2, n // 2 + 1):
				if n % i == 0:
					lst.append(i)
			lst.append(n)
			lst.reverse()
			for elem in lst:
				if math.sqrt(elem).is_integer():
					squares.append(elem)
			for i in lst:
				for j in squares:
					if i % j == 0:
						break
				else:
					x.append(i)
			if len(x) > 0:
				return x[0]
			else:
				return lst[0]
		elif n is 1:
			return 1
		else:
			return None
	else:
		return None


def square_free_part(n):
	if not isinstance(n, int):
		return None
	if n < 1:
		return None
	for i in range(n, 1, -1):
		if not n % i:
			b = True
			for j in range(i // 2, 1, -1):
				if not i % (j * j):
					b = False
					break
			if b:
				return i


def getAllPrimeFactors(n):
	if not isinstance(n, int):
		return []
	if n == 1:
		return [1]
	factors = []
	i = 2
	while i * i <= n:
		while n % i == 0:
			factors.append(i)
			n //= i
		i += 1
	if n > -1:
		factors.append(n)
	return factors

def getUniquePrimeFactorsWithCount(n):
	if n == 1:
		return [[1], [1]]
	lis = getAllPrimeFactors(n)
	if lis == []:
		return [[], []]
	return [sorted(set(lis)), [lis.count(i) for i in sorted(set(lis))]]

def get_factors(num):
	lis = []
	for i in range(2, num + 1):
		if num % i == 0:
			lis.append(i)
	return lis

def square_free_part(n):
	if not type(n) == int:
		return None
	if n == 1:
		return 1
	facs = get_factors(n)
	for factor in facs[::-1]:
		lis = getUniquePrimeFactorsWithCount(factor)
		if any((en > 1 for en in lis[1])):
			continue
		else:
			return factor


def square_free_part(n):
	if type(n) != int or n < 1:
		return None
	result = 1
	divisor = 2
	while n > 1:
		if n % divisor == 0:
			result *= divisor
			n //= divisor
			while n % divisor == 0:
				n //= divisor
		divisor += 1
	return result


def square_free_part(n):
	if type(n) != int or n < 1:
		return
	ans = 1
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			ans *= i
			while n % i == 0:
				n //= i
	if n > 1:
		ans *= n
	return ans