import sympy
# Given values
n = 391
e = 291
# Step 1: Factorize n to find p and q
factors = sympy.factorint(n)
p, q = factors.keys()
# Step 2: Calculate φ(n) = (p - 1) * (q - 1)
phi_n = (p - 1) * (q - 1)
# Step 3: Find the modular inverse of e modulo φ(n) to get d
d = sympy.mod_inverse(e, phi_n)
# Print the results
print(f"Factors of n: p = {p}, q = {q}")
print(f"φ(n) = {phi_n}")
print(f"Private key d = {d}")