We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Mathematics
- Algebra
- Simple One
- Discussions
Simple One
Simple One
Sort by
recency
|
9 Discussions
|
Please Login in order to post a comment
Python 3 Solution :
I got the values of final p and q but I didn't get the modulo thing..can anyone help me in this.............
Consider without modulo.
a = bx
, so to solve for x,x = a/b
. Unfortunately we can't really divide under modulo, but we can write this equation asa * b^-1 = x
or x is a times the multiplicative inverse of b.Luckily we can find the mulitplicative inverse of b under modulo M (because
M=10^9 + 7
is prime, sob | b < M
is coprime to M). We can calculate it by(b^(M-2))%M
Actually, the euclidean extended algorithm is faster than the fast exponentiation of b^(M-2)%m https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm because b and M are corpime (statted in the problem and M is prime) there exists a bezout identity X*b + Y*M = 1, so in modulo M, the inverse of b is X.
In deriving tan(n * alpha) we can use tangent identity recursively until it reaches tan(alpha).
The right hand side expanded recursively overflows. To avoid overflow we can ues modular division.
To find modular multiplicative inverse of b modulo n, i.e. x below,
use Extended Eucleadean Algorithm that finds quotients x and y in addition to gcd(b, n).
The first and most straightforward approach is to use the addition formula for tangents: This gives us a way to compute given similarly to fast exponentiation: def tan_add(t,u): [// compute tan(a+b) given tan(a) = t and tan(b) = u return (a + b) / (1 - a*b)
def tan(n,t): // compute tan(n*a) given tan(a) = t if n == 1:
def tan_add(t,u): // compute tan(a+b) given tan(a) = t and tan(b) = u return (a + b) / (1 - a*b)
def tan(n,t): // compute tan(n*a) given tan(a) = t if n == 1: return t else if n % 2 == 0: result = tan(n/2,t) return tan_add(result, result) else: result = tan(n-1,t) return tan_add(result, t)