Struggling with calculating the inverse using Eulers-thereom - discrete-mathematics

been struggling this whole day with trying to figure out the multiplicative inverse of 17 modulo 31. I know by "manual" computation that the actual inverse is 11 but how do I prove this with Euler's theorem.
We know that 31 is a prime, φ(n)=30, so i end up with 17^30=(cong)1 (mod 31). But how do proceed from this? Would be very thankful if someone could help me out since im stuck.
Thanks in advance.

Well, lets formalize it. Lets a = 17, p = 31, you want to find a^(-1). So we get by Euler's theorem a^(p - 1) = 1(mod p). Lets divide both parts by a: a^(p - 2) = a^(-1) (mod p)
Answer: 17^29 (mod 31)

Related

Calculating d value in RSA

I saw a couple questions about this but most of them were answered in unhelpful way or didn't get a proper answer at all. I have these variables:
p = 31
q = 23
e - public key exponent = 223
phi - (p-1)*(q-1) = 660
Now I need to calculate d variable (which I know is equal 367). The problem is that I don't know how. I found this equation on the internet but it doesn't work (or I can't use it):
e⋅d=1modϕ(n)
When I see that equation i think that it means this:
d=(1modϕ(n))/e
But apparently it doesn't because 367 (1modϕ(n))/e = 1%660/223 = 1/223 != 367
Maybe I don't understand and I did something wrong - that's why I ask.
I did some more research and I found second equation:
d=1/e mod ϕ(n)
or
d=e^-1 mod ϕ(n)
But in the end it gives the same result:
1/e mod ϕ(n) = 1/223 % 660 = 1/223 != 367
Then I saw some guy saying that to solve that equation you need extended Euclidean algorithm If anyone knows how to use it to solve that problem then I'd be very thankful if you help me.
If you want to calculate something like a / b mod p, you can't just divide it and take division remainder from it. Instead, you have to find such b-1 that b-1 = 1/b mod p (b-1 is a modular multiplicative inverse of b mod p). If p is a prime, you can use Fermat's little theorem. It states that for any prime p, ap = a mod p <=> a(p - 2) = 1/a mod p. So, instead of a / b mod p, you have to compute something like a * b(p - 2) mod p. b(p - 2) can be computed in O(log(p))
using exponentiation by squaring. If p is not a prime, modular multiplicative inverse exists if and only if GCD(b, p) = 1. Here, we can use extended euclidean algorithm to solve equation bx + py = 1 in logarithmic time. When we have bx + py = 1, we can take it mod p and we have bx = 1 mod p <=> x = 1/b mod p, so x is our b-1. If GCD(b, p) ≠ 1, b-1 mod p doesn't exist.
Using either Fermat's theorem or the euclidean algorithm gives us same result in same time complexity, but the euclidean algorithm can also be used when we want to compute something modulo number that's not a prime (but it has to be coprime with numer want to divide by).

How to Solve a linear matrix equation of an array M = B*M*C where B and C are known

Adding to the question's description :
I am doing Feature extraction from videos and i am trying to implement this one line of mathematical equation to matlab or even any algorithm .
let's say I have B and C which are 10 x 10 matrices
R is also a 10 x 10 matrix but it should satisfy two conditions
1) M == BxMxC
2) The Values in M should be within a given range [x( which is min) y(max)] given by the user
i can handle the constraints of the range but i have no idea on how to proceed with the 1st condition at all , much less converting it into matlab
I have found several solutions to this type of problem
1)Sylvester's equation which approximates AX+XB = C or AX + XB + C = 0
2)Lyapunov Matrix equation which approximates AX+XA'+Q = 0
both of these implementations of matlab i have found in this link given below
https://github.com/ajt60gaibb/freeLYAP
but i need help in the mathematics area as to how i can use those (if i can) to M = BMC
if anyone can help me get through this ... i'll be really glad
peace out

Modular arithmetic AND Eucledian Algorithm

I was studying how to find the modular inverse. Suppose the example is:
27*x is congruent to 1 (mod 392) .
Now we have to find x. In the process we write this Equation as:
x is congruent to 27^(-1) (mod 392).
Here is my confusion does in modular arithmetic we can simply take 27 from left hand side and move it to right hand side and write it as 1/(27) (mod 392) without considering the 1 (mod 392) present their already and inserting 1/27 in between of 1 and (mod 392).
Because 27*x was congruent to 1(mod 392) but now we take x is congruent to 1/27 (mod 392).
This seems confused. If 27x = 1 (mod 392) then by definition x is 27^-1 (mod 392). You don't solve this equation by "moving" things from the left hand side to the right hand side. You solve it by using the Extended Euclidean Algorithm to write 27x + 392y = 1 in which case x is the inverse you seek since you can rearrange the equation as 392y = 1 - 27x which shows that 27x differs from 1 by a multiple of 392 hence 27x = 1 (mod 392)

matlab wrong modulo result when the divident is raised to a power

Just wondering... I tried doing by hand (with the multiply and square method) the operation (111^11)mod143 and I got the result 67. I also checked that this is correct, in many online tools. Yet, in matlab plugging:
mod(111^11,143)
gives 127! Is there any particular reason for this? I didn't find anything in the documentation...
The value of 111^11 (about 3.1518e+022) exceeds the maximum integer that is guaranteed to be represented exactly as a double, which is 2^53 (about 9.0072e+015). So the result is spoilt by insufficient numerical precision.
To achieve the correct result, use symbolic computation:
>> syms x y z
>> r = mod(x^y, z);
>> subs(r, [x y z], [111 11 143])
ans =
67
Alternatively, for this specific operation (modulo of a large number that is expressed as a product of small numbers), you can do the computation very easily using the following fact (where ∗ denotes product):
mod(a∗b, z) = mod(mod(a,z)∗mod(b,z), z)
That is, you can apply the modulo operation to factors of your large number and the final result is unchanged. If you choose factors sufficiently small so that they can be represented exactly as double, you can do the computation numerically without any loss of precision.
For example: using the decomposition 111^11 = 111^4*111^4*111^3, since all factors are small enough, gives the correct result:
>> mod((mod(111^4, 143))^2 * mod(111^3, 143), 143)
ans =
67
Similarly, using 111^2 and 111 as factors,
>> mod((mod(111^2, 143))^5 * mod(111, 143), 143)
ans =
67
from the matlab website they recommend using powermod(b, e, m) (b^e mod m)
"If b and m are numbers, the modular power b^e mod m can also be computed by the direct call b^e mod m. However, powermod(b, e, m) avoids the overhead of computing the intermediate result be and computes the modular power much more efficiently." ...
Another way is to use symfun
syms x y z
f = symfun(mod(x^y,z), [x y z])
f(111,11,143)

Is there anyway to calculate mean of beta distribution with MATLAB?

This is my code for calculate the mean:
syms a b x
f=1/(beta(a,b))*x^(a-1)*(1-x)^(b-1);
int(x*f,x,0,1)
Warning: Explicit integral could not be found.
ans =
int((x*x^(a - 1)*(1 - x)^(b - 1))/beta(a, b), x == 0..1)
How can I fix this?
This is my result for TryHArd's:
syms x a b
f=int(x^(a-1)*(1-x)^(b-1),x,0,1)
Warning: Explicit integral could not be found.
f =
piecewise([0 < real(a) and 0 < real(b), beta(a, b)], [real(a) <= 0 or real(b) <= 0, int(x^(a - 1)*(1 - x)^(b - 1), x == 0..1)])
My result did not show the same gamma(a)*gamma(b)/gamma(a+b) as TryHard did.
I assume by beta you mean
beta(z,w) = integral from 0 to 1 of t.^(z-1) .* (1-t).^(w-1) dt.
Then break down the problem:
>> int((x^(a-1))*((1-x)^(b-1)),x,0,1)
ans =
gamma(b)*gamma(a)/gamma(a+b)
The desired integral is equivalent to:
>> bint=int((x^(a-1))*((1-x)^(b-1)),x,0,1);
>> int( x*((x^(a-1))*((1-x)^(b-1)))/bint,x,0,1)
ans =
a/(a+b)
(This was computed with an earlier version of the Matlab SMT (on R14), but should serve as a guide.)
The mean of the Beta distritution is 1/(1+b/a). See for example here
Wolfram alpha is capable of it.
Computing the definite integral directly exceeds maximum standard computation time, but it can find the indefinite integral. Limiting that expression to x between 0 and 1, means simply equating x to 1. This results in this expression, the alternate form of which is the standard form of the mean of the beta distribution.
Wolfram Mathematica (& Alpha) is simply better at symbolic math than MuPad. I'd advise to use MATLAB only for what it rocks at: numerical math. Other than perhaps some fringe cases, I do not expect MuPad to ever come close to what has been achieved by Wolfram.
If you need the result for some specific parameters alpha and beta, you can use betastat. This function can be used also with vectors, like that:
A=[4 4 4];
B=[5 6 7];
[m,v]=betastat(A,B)
This will give you a 3-elements vector for both m (mean) and v (variance). For this example, m will be the means of beta(4,5), beta(4,6) and beta(4,7). (Note that "beta" here denotes the distribution, not the beta-function, like beta function on Matlab).
If you need a general (mathematical) solution, see Rody's answer. Wolfram is much appropriate for this.
EDIT: You can define f (in your code) by the function betapdf instead of the whole formula you wrote over there.