I found an interesting problem on https://www.codewars.com/kata/53c945d750fe7094ee00016b:
A common problem in number theory is to find x given a such that:

a * x = 1 mod [n] 

Then x is called the inverse of a modulo n. 

Your goal is to code a function inverseMod wich take a and n as parameters and return x.

You may be interested by these pages:

http://en.wikipedia.org/wiki/Modular_multiplicative_inverse

http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm


a and n should be co-prime to have a solution, if it is not the case, you should return None (Python), nil (Ruby) or null (Javascript). 

a and n will be positive integers. The problem can easily be generalised to negative integer with some sign changes so we won't deal with them.

I tried it in Python, but could not do it. Can you solve it?
import math
def phi(a):
    res = a
    b = a
    i = 2
    while i*i <= a:
        if a % i == 0:
            res -= res//i
            while a % i == 0:
                a //= i
        i = i+1
    if(a>1):
        res -= res//a
    return res
    
def inverseMod(a, m):
    if math.gcd(a,m) != 1:
        return None
    _phi = phi(m)
    return pow(a,_phi-1,m)