There are two basic facts that you should know that will help you remember the formula.
First, if $n$ is a power of a prime, say $n=p^k$, then $\phi(p^k)=p^k-p^{k-1}$. This is because the only numbers less than or equal to $p^k$ that are not relatively prime to $p$ are $p,p^2,\ldots,p^{k-1}$. Note that $\phi(n)=\phi(p^k)=p^k(1-\frac{1}{p})$.
The second is that if $m$ and $n$ are relatively prime, then $\phi(mn)=\phi(m)\phi(n)$. This can be shown with not too much trouble using the chinese remainder theorem.
Putting all this together, if you can find the prime factorization $n=p_1^{k_1}p_2^{k_2}\cdots p_m^{k_m}$, where $p_1,\ldots,p_m$ are distinct primes, then $$\phi(n)=p_1^{k_1}(1-\frac{1}{p_1})p_2^{k_2}(1-\frac{1}{p_2})\cdots p_m^{k_m}(1-\frac{1}{p_m})\\ =n(1-\frac{1}{p_1})(1-\frac{1}{p_2})\cdots(1-\frac{1}{p_m}).$$
For example, if $n=140$ and you find that $n=(2^2)(5)(7)$, then $\phi(140)=140(1-\frac{1}{2})(1-\frac{1}{5})(1-\frac{1}{7})$.
If primes $p_1, p_2$ are the only prime factors of $n$, then we get numbers relatively prime to $n$ and less than $m$ are (assuming $p_1, p_2$ are not factors of $m$):
$$m - \lfloor\frac{m}{p_1}\rfloor - \lfloor\frac{m}{p_2}\rfloor + \lfloor\frac{m}{p_1p_2}\rfloor$$
We can have a generalized expression for any number of prime factors of $n$ using PIE.
Best Answer
It comes with a hint which is to consider the prime factorization of $n$. (The one who gave me the question didn't declare the hint).
Since $p | n$, the prime factorization of n is
$n = $$p^e$$p^{e_1}_1$$p^{e_2}_2$$\cdots$$p^{e_k}_k$, for some $k$.
Thus
$ϕ(n) = ϕ($$p^e)$ $ϕ($$p^{e_1}_1$$p^{e_2}_2$$\cdots$$p^{e_k}_k)$
We know that $ϕ(n) = p-1$, and since it is $p.p.p\cdots$ $e$ times, so we took one $p$ and multiplied the rest $p^{e-1}$ with it
$ϕ(n)= (p − 1)$$p^{e−1}$ $ϕ($$p^{e_1}_1$$p^{e_2}_2$$\cdots$$p^{e_k}_k)$
$ϕ(np) = ϕ($$p^{e+1}$$p^{e_1}_1$$p^{e_2}_2$$\cdots$$p^{e_k}_k)$
As they are primes, their $gcd$ for every two of them is $1$, thus we can use this rule:
$= ϕ($$p^{e+1}) ϕ($$p^{e_1}_1$$p^{e_2}_2$$\cdots$$p^{e_k}_k)$
$= (p − 1)$$p^e$ $ϕ($$p^{e_1}_1$$p^{e_2}_2$$\cdots$$p^{e_k}_k)$
$= pϕ(n)$