### What is simplifying a square root?

If you have a number, like 80, for example, you would start by finding the greatest perfect square divisible by 80. In this case, that's 16. Find the square root of 16, and multiply it by 80 / 16. Answer = 4 √5. 

##### The above example:

![simplify_roots_example.png](https://i.postimg.cc/gjv2NwCm/simplify-roots-example.png)

### Task:

Your job is to write two functions, `simplify`, and `desimplify`, that simplify and desimplify square roots, respectively. (Desimplify isn't a word, but I couldn't come up with a better way to put it.) `simplify` will take an integer and return a string like "x sqrt y", and `desimplify` will take a string like "x sqrt y" and return an integer. For `simplify`, if a square root cannot be simplified, return "sqrt y". 

_Do not modify the input._

### Some examples:

```python
simplify(1)  #=> "1"
simplify(2)  #=> "sqrt 2"
simplify(3)  #=> "sqrt 3"
simplify(8)  #=> "2 sqrt 2"
simplify(15) #=> "sqrt 15"
simplify(16) #=> "4"
simplify(18) #=> "3 sqrt 2"
simplify(20) #=> "2 sqrt 5"
simplify(24) #=> "2 sqrt 6"
simplify(32) #=> "4 sqrt 2"

desimplify("1")        #=> 1
desimplify("sqrt 2")   #=> 2
desimplify("sqrt 3")   #=> 3
desimplify("2 sqrt 2") #=> 8
desimplify("sqrt 15")  #=> 15
desimplify("4")        #=> 16
desimplify("3 sqrt 2") #=> 18
desimplify("2 sqrt 5") #=> 20
desimplify("2 sqrt 6") #=> 24
desimplify("4 sqrt 2") #=> 32
```

Also check out my other creations — [Square Roots: Approximation](https://www.codewars.com/kata/square-roots-approximation), [Square and Cubic Factors](https://www.codewars.com/kata/square-and-cubic-factors), [Keep the Order](https://www.codewars.com/kata/keep-the-order), [Naming Files](https://www.codewars.com/kata/naming-files), [Elections: Weighted Average](https://www.codewars.com/kata/elections-weighted-average), [Identify Case](https://www.codewars.com/kata/identify-case), [Split Without Loss](https://www.codewars.com/kata/split-without-loss), [Adding Fractions](https://www.codewars.com/kata/adding-fractions),
[Random Integers](https://www.codewars.com/kata/random-integers), [Implement String#transpose](https://www.codewars.com/kata/implement-string-number-transpose), [Implement Array#transpose!](https://www.codewars.com/kata/implement-array-number-transpose), [Arrays and Procs #1](https://www.codewars.com/kata/arrays-and-procs-number-1), and [Arrays and Procs #2](https://www.codewars.com/kata/arrays-and-procs-number-2).

Difficulty: introductory

Starter code:

def simplify(n):
	

Solutions:
def simplify(n):
    for d in range(int(n ** .5), 0, -1):
        if not n % d ** 2: break
    if d*d == n: return '%d' % d
    elif d == 1: return 'sqrt %d' % n
    else: return '%d sqrt %d' % (d, n // d ** 2)

def desimplify(s):
    x, _, y = s.partition('sqrt')
    return int(x or '1') ** 2 * int(y or '1')

def simplify(n):
    sq = next(k for k in range(int(n**0.5), 0, -1) if n % (k*k) == 0)
    rt = n // (sq * sq)
    return str(sq) if rt == 1 else f"{sq if sq > 1 else ''} sqrt {rt}".strip()

def desimplify(s):
    rt, _, sq = (int(n.replace("sqrt", "") or 1) for n in s.partition("sqrt"))
    return rt * rt * sq

def simplify(n):
    div, sq, root = 2, 1, 1
    while div <= n:
        r = 0
        while n % div == 0:
            n //= div
            r += 1
        sq *= div ** (r // 2)
        root *= div ** (r % 2)
        div += 1 + (div != 2)
    return (
        f'{sq}' if root == 1 else
        f'sqrt {root}' if sq == 1 else
        f'{sq} sqrt {root}'
    )
    
def desimplify(s):
    xs = ([int(x.strip() or 0) for x in s.split('sqrt')] + [0])[:2]
    return (xs[0] ** 2 * xs[1]) or (xs[0] ** 2 + xs[1])

def simplify(n):
    d = max(m for m in range(1, int(n**0.5)+1) if m*m <= n and n % (m*m) == 0)
    r = n // (d*d)
    return str(d) if r == 1 else "sqrt {}".format(r) if d == 1 else "{} sqrt {}".format(d, r)

def desimplify(s):
    d, r = s.split("sqrt") if "sqrt" in s else (s, "1")
    d = d if d else "1"
    return int(d)*int(d)*int(r)

from math import sqrt
def simplify(n):
        li = [i for i in range(2,n) if n%i==0 and sqrt(i)==int(sqrt(i))]
        return (f"{int(sqrt(li[-1]))} sqrt {n // li[-1]}" if li else f"sqrt {n}") if sqrt(n) != int(sqrt(n)) else f"{int(sqrt(n))}"
def desimplify(s):
    s = s.split()
    try : return int(pow(int(s[0]), 2)) * (int(s[-1]) if len(s)>1 else 1)
    except : return int(s[1])

def simplify(n):
    if (n ** 0.5) % 1 == 0: 
        return f"{int(n**0.5)}"
    d = 1
    r = n
    for i in range(n//2, 1,-1):
        if n % i == 0 and (i**0.5) % 1 == 0:
            r = n // i
            d = int(i**0.5)
            break
    return f"{d} sqrt {r}" if d > 1 else f"sqrt {r}"

def desimplify(s):
    if s.find("sqrt") == -1: 
        return int(s) ** 2
    elif s.count(" ") == 1:
        return int(s[s.index(" ")+1: ])
    else:
        n1 = int(s[:s.index(" ")])
        n2 = int(s[s.rindex(" ")+1: ])
        return n1**2 * n2


def simplify(n):

    for i in range(int(n**.5),1,-1):
        if not n%(i*i):
            s = f'{i} sqrt {n//(i*i)} '
            break
    else:
        s = f' sqrt {n} '

    return s.replace(' sqrt 1 ','').strip() or '1'

def desimplify(s):
    s = s.split()
    if len(s)==3:
        a,b = map(int,(s[0],s[-1]))
        return a*a*b
    if len(s)==1:
        return int(s[-1])**2
    return int(s[-1])

import re
from collections import Counter

def prime_factor(n):
    if n == 1:
        return Counter([1])
    factors = Counter()
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            factors[i] += 1
            n //= i
    if n != 1:
        factors[n] = 1
    return factors


def simplify(n):
    a, b = 1, 1
    for k, v in prime_factor(n).items():
        while v >= 2:
            a *= k
            v -= 2
        b = b * k if v else b
    if a == 1:
        return f'sqrt {b}' if b > 1 else '1'
    if b == 1:
        return str(a)
    return f'{a} sqrt {b}'


def desimplify(s):
    res = re.match(r'(\d+ ?)?(sqrt (\d+))?', s)
    a, b = res.group(1), res.group(3)
    ans = int(a)**2 if a else 1
    return ans * int(b) if b else ans

import re

def simplify(n: int) -> str:
    def inner(n):
        for i in range(2, n):
            div, mod = divmod(n, i * i)
            if not mod:
                sq1, sq2 = inner(div)
                return (i * sq1, sq2)
            if not div:
                break
        return (1, n)
    a, b = inner(n)
    if b == 1:
        return f'{a}'
    elif a != 1:
        return f'{a} sqrt {b}'
    else:
        return f'sqrt {b}'

def desimplify(s: str) -> int:
    m = re.match(r'(?P<INT>\d+)?(?: )?(?:sqrt (?P<SQRT>\d+))?', s)
    x, y = m.groups()
    return int(x or '1') ** 2 * int(y or '1')