Is there an algorithm that can solve a non-linear congruence in modular arithmetic? I read that such a problem is classified as NP-complete.
In my specific case the congruence is of the form:
x^3 + ax + b congruent to 0 (mod 2^64)
where a and b are known constants and I need to solve it for x.
Look at Hensel's lemma.
Yes, the general problem is NP-Complete.
This is because boolean algebra is arithmetic modulo 2! So any 3SAT formula can be rewritten as an equivalent arithmetic expression in arithmetic modulo 2. Checking if a 3SAT formula is satisfiable becomes equivalent to checking if the corresponding arithemetic expression can be 1 or not.
For example, a AND b becomes a.b in arithemetic.
NOT a is 1-a etc.
But in your case, talking about NP-Compleness makes no sense, as it is one specific problem.
Also, lhf is right. Hensel's Lifting lemma can be used. The basic essence is that to solve P(x) = 0 mod 2^(e+1) we can solve P(x) = 0 mod 2^e and 'lift' those solutions to mod 2^(e+1)
Here is a pdf explaining how to use that: http://www.cs.xu.edu/math/math302/04f/PolyCongruences.pdf
Related
I'm trying to write a code which decrypts any Affine cipher.
Now, I found that the decryption function is :
y = a^(-1) * (x-b) mod 26
The problem is : when x is smaller than b the answer is negative.
I know that it is a Math question rather than a Code question, but I hope that there are some nice guys who may help me.
It's actually a question that straddles maths and programming.
Firstly mathematicians and programmers use "mod" somewhat differently.
Mathematicians use it as a statement about the equation they have just written. When they say "a = b + c mod m" what they mean is that "a = b + c" in modulo m arithmetic.
Programmers on the other hand use mod as an operator that provides the remainder after integer division.
Secondly there are multiple ways of defining integer division "floored division", "truncated division" and "euclidian division" and hence multiple ways of defning the modulo operator.
Unfortunately what you need for your algorithm is the "remainder after floored division" but what your programming language is giving you is the "remainder after truncated division.
One possible fix is to simply add an if statement.
if (y < 0) y += 26
I have a dumb question, and I am embarrassed to even ask.
Due to my little knowledge in math I couldn't figure out what should I search.
I'm dealing with the following equation:
[(a*x)^b]*c=d
where ^ stands for XOR and * for Multiplication.
How can I isolate x?
[(a*x)^b]*c=d
[(a*x)^b]=d/c
(a*x)^b^b=(d/c)^b //double xor with b retrieves initial value
(a*x)=(d/c)^b
x = ((d/c)^b) / a
Based on properties of xor the following holds:
A xor A = 0
B xor 0 = B
Plus, it's commutative. The rest is plain equation solving math.
My problem is one which should be quite common in statistical inference:
min{(P - k)'S(P - k)} subject to k >= 0
So my choice variable is k, a 3x1 vector. The 3x1 vector P and 3x3 matrix S are known. Is it possible to reformulate this problem so I can use R's solve.QP quadratic programming solver? This solver requires the problem to be in the form
min{-d'b + 0.5 b' D b} subject to A'b >= b_0.
So here the choice vector is is b. Is there a way I can make my problem fit into solve.QP? Thanks so much for any help.
I have to code to evaluate the value of following sequence :
( pow(1,k) + pow(2,k) + ... + pow(n,k) ) % MOD
for given value of n,k and MOD.
I have tried searching it on internet. I got an equation . It contains zeta functions and it seems difficult in implementation. I want any simple approach for implementing the same. Note that the value of n is large, so that we cannot simply use brute force to pass the time limit.
Newton's identities might be of help. Calculate the coefficients of the polynomial with 1..n as roots. That pretty trivial. Then use the identities.
It's just the first thing that comes to mind when I see sums of powers.
I think it is nicely compatible with modular arithmetics - there are only multiplications and additions.
I must admit, that Newton's identities are only the rearrangement of the terms, so not much speed gain here.
JUST USE PYTHON
k=input("Enter value for K: ")
n=input("Enter value for N: ")
mod=input("Enter value for MOD: ")
sum=0
for i in range(1,n+1):
sum+=pow(i,k)
result=sum % mod
print mod
May be this code is gonna help.
I agree that math.stackexchange.com is a better bet.
But here are random facts that, depending on parameters, may make the problem more manageable.
First, factor MOD, solve for each prime power factor, then use the Chinese Remainder Theorem to find the answer for MOD. Thus without loss of generality, you may assume that MOD is a prime power.
Next, note that 1^k + ... + MOD^k is always divisible by MOD. Therefore you can replace n by n mod MOD.
Next, if MOD = p^i and j is not divisible by p, then j^((p-1) * p^(i-1)) is 1 mod MOD, so we can reduce the size of k.
Of course if (k, n) < MOD and MOD is prime, this will not help you at all. (Which, depending on how this problem arises, may well be the case.)
(If k is small enough, there are explicit formulas that you can produce for the sum. But it seems that for you k can be large enough to make that approach intractable.)
I face the following problem in a cryptographical application: I have given a set of linear congruences
a[1]*x[1]+a[2]*x[2]+a[3]*x[3] == d[1] (mod p)
b[1]*x[1]+b[2]*x[2]+b[3]*x[3] == d[2] (mod p)
c[1]*x[1]+c[2]*x[2]+c[3]*x[3] == d[3] (mod p)
Here, x is unknown an a,b,c,d are given
The system is most likely underdetermined, so I have a large solution space. I need an algorithm that finds an equidistributed solution (that means equidistributed in the solution space) to that problem using a pseudo-random number generator (or fails).
Most standard algorithms for linear equation systems that I know from my linear algebra courses are not directly applicable to congruences as far as I can see...
My current, "safe" algorithm works as follows: Find all variable that appear in only one equation, and assign a random value. Now if in each row, only one variable is unassigned, assign the value according to the congruence. Otherwise fail.
Can anyone give me a clue how to solve this problem in general?
You can use gaussian elimination and similar algorithms just like you learned in your linear algebra courses, but all arithmetic is performed mod p (p is a prime). The one important difference is in the definition of "division": to compute a / b you instead compute a * (1/b) (in words, "a times b inverse"). Consider the following changes to the math operations normally used
addition: a+b becomes a+b mod p
subtraction: a-b becomes a-b mod p
multiplication: a*b becomes a*b mod p
division: a/b becomes: if p divides b, then "error: divide by zero", else a * (1/b) mod p
To compute the inverse of b mod p you can use the extended euclidean algorithm or alternatively compute b**(p-2) mod p.
Rather than trying to roll this yourself, look for an existing library or package. I think maybe Sage can do this, and certainly Mathematica, and Maple, and similar commercial math tools can.