What does the equivalence sign with the small 3 next to it mean... I'm unfamiliar with that sign.
≡3
Thanks!
This means "is congruent to modulo 3." For example, 7 ≡3 1 because 7 mod 3 = 1 = 1 mod 3. More formally, a ≡n b iff there exists integral c0, c1, along with an integral k with 0 ≤ k < n, such that a = nc0 + k and b = nc1 + k.
Related
I included a function (a%b + b) % b in some old cold and remember concluding that this was due to some special cases of a%b that I needed to be careful about. a and b are c ints and % is the c modulo operator. Now I am having trouble seeing where these two expressions ever differ. Are they completely equivalent?
The mathematical long division demands that the remainder is zero or positive, a=q*b+r with 0 <= r < b.
In the computer implementations of this operation it is possible that a%b is negative. Thus adding b then gives the non-negative remainder. To be universally useful you either need an if-branching or another remainder operation for the case where a%b already was non-negative.
The % operator does not implement a true modulo. In fact,
a ≥ 0 -> a % b = a mod b
a < 0 -> a % b = - ((-a) mod b)
Now,
a -4 -3 -2 -1 0 1 2 3 4
a mod 4 0 1 2 3 0 1 2 3 0
a % 4 0 -3 -2 -1 0 1 2 3 0
(a % 4 + 4) % b 0 1 2 3 0 1 2 3 0
Unfortunately, this doubles the cost of the modulo, which is significant.
How to find generators of a finite field Fp[x]/f(x) with f(x) is a irreducible polynomial over Fp.
Input: p (prime number), n (positive number), f (irreducible polynomial)
Output: g (generator)
I have p = 2, n =3, f = x^3 + x + 1
I am a newbie so I don't know where to start.
Do you have any solution? Plese help me step by step
To find a generator (primitive element) α(x) of a field GF(p^n), start with α(x) = x + 0, then try higher values until a primitive element α(x) is found.
For smaller fields, a brute force test to verify that powers of α(x) will generate every non-zero number of a field can be done.
cnt = 0
m = 1
do
cnt = cnt + 1
m = (m*α)%f(x)
while (m != 1)
if cnt == (p^n-1) then α(x) is a generator for GF(p^n).
For a faster approach with larger fields, find all prime factors of p^n-1. Let q = any of those prime factors. If α(x) is a generator for GF(p^n), then while operating in GF(p^n):
α(x)^(p^n-1) % f(x) == 1
α(x)^((p^n-1)/q) % f(x) != 1, for all q that are prime factors of p^n-1
In this case GF(2^3) is a 3 bit field and since 2^3-1 = 7, which is prime, then it's just two tests, shown in hex: x^3 + x + 1 = b (hex)
α(x)^7 % b == 1
α(x)^1 % b != 1
α(x) can be any of {2,3,4,5,6,7} = {x,x+1,x^2,...,x^2+x+1}
As another example, consider GF(2^4), f(x) = x^4 + x^3 + x^2 + x + 1 (hex 1f). The prime factors of 2^4-1 = 15 are 3 and 5, and 15/3 = 5 and 15/5 = 3. So the three tests are:
α(x)^f % 1f == 1
α(x)^5 % 1f != 1
α(x)^3 % 1f != 1
α(x) can be any of {3,5,6,7,9,a,b,e}
For larger fields, finding all prime factors of p^n-1 requires special algorithms and big number math. Wolfram alpha can handle up to around 2^128-1:
https://www.wolframalpha.com/input/?i=factor%282%5E64-1%29
This web page can factor large numbers and includes an explanation and source code:
https://www.alpertron.com.ar/ECM.HTM
To test for α(x)^(large number) = 1 or != 1, use exponentiation by repeated squaring while performing the math in GF(p^n).
https://en.wikipedia.org/wiki/Exponentiation_by_squaring
For large fields, where p^n is greater than 2^32 (4 billion), a primitive polynomial where α(x) = x is searched for, using the test mentioned above.
I want to find (num * (pow(b, p) - 1) / den) % mod. I know about binary exponentiation. But we can't do it straightforward. It is guaranteed that the numerator is divisible by the denominator. That means
[num * (pow(b, p) - 1)] % den == 0
constraints on mod: are 1 <= mod <= 10 ^ 9 and mod might be prime or composite
constraints on b: 1 <= b <= 10
constraints on p: 1 <= p <= (10^18)
constraints on num: 1 <= num <= (10^9)
constraints on den: 1 <= den <= (10^9)
Here pow(b, p) means b raised to power p(b ^ p). It is guaranteed that the numerator is divisible by the denominator. How can I do it with binary exponentiation
Your expression should rewritten to simplIfy it. First let k=num/den, with k integer according to your question.
So you have to compute
(k×(b^p-1))mod m=( (k mod m) × ((b^p -1) mod m) ) mod m
= ( (k mod m) × ( (b^p mod m) -1 mod m ) mod m ) mod m
= ((k mod m) × ((b^p mod m) + m-1) mod m) mod m (1)
So the real problem is to compute b^p mod m
Many languages (python, java, etc) already have a modular exponentiation in their standard libraries. Consult the documentation and use it. Otherwise, here is a C implementation.
unsigned long long modexp(unsigned long long b, unsigned long long e, unsigned long long m) {
if (m==1) return 0;
unsigned long long res=1;
unsigned long long bb = b % m;
while (e) {
if (e & 1)
res = (res*b) % m;
e >>= 1;
bb = (bb*bb) % m;
}
return res;
}
The implementation uses long long to fit your constraints. It relies on the classical trick of binary exponentiation. All values of b^l, where l is a power of two (l=2^t) are computed and stored in var bb and if the corresponding tth bit of e is set, this value of b^l is integrated in the result. Bit testing is done by checking the successive parities of e, while shifting e rightward at each step.
Last, the fact that (a×b)mod m=((a mod m)×(b mod m))mod m is used to avoid computation on very large numbers. We always have res<m and bb<m and hence res and bb are codable on standard integers.
Then you just have to apply (1) to get the final result.
EDIT according to the precisions given in the comments
To compute n=(3^p-1)/2 mod m, one can remark that
(3^p-1)/2 = x*m + n (as 3^p-1 is even, x is an integer, 0≤n<m)
3^p-1=x*2*m+2n (0≤2n<2m)
so 2n=(3^p-1) mod 2m
We can just apply the previous method with a modulo of 2*m, and divide the result (that will be even) by 2.
fact :: Int -> Int
fact n
|n < 0 = 0
|n == 0 = 1
|n > 0 && n `mod` 2 == 1 = fact (n-1) * n
|n > 0 && n `mod` 2 == 0 = n-1
When I enter an odd number for example: fact 5 will give 15, as it should 1 * 3 * 5 = 15. However I realized that if I do fact 7 or any other odd number, it only multiplies the first two odd numbers. How do I get the function to multiply all the odd numbers and not just the first 2. Eg. fact 7 = 35 (ie. 3 * 5). Also note, that if an even number is entered, it will work out the factorial of all the odd numbers up until and not including the even number.
This reminds me of the famous Evolution of a Haskell Programmer. Paraphrasing the tenured professor's answer:
factorialOfOdds :: Integer -> Integer
factorialOfOdds n = product [1,3..n]
You're problem is that your case for an even number is n-1, which means that when you get to an odd number you're really just doing
n * (n - 1 - 1)
When what you want is
n * n-2 * n-4 ...
So try this instead
fact :: Integer -> Integer -- Overflows
fact n
|n < 0 = 0
|n == 0 || n == 1 = 1
|n `mod` 2 == 1 = fact (n-2) * n
|n `mod` 2 == 0 = fact (n-1)
I also took the liberty of removing some redundant logic. Here we decrement by two if it's odd, so 5 -> 3. And in the even case we decrement by one to end up on an odd number and that recurse on that.
I'm having trouble figuring out the difference between the two. Say you have these givens:
a[0] = 10
a[1] = 13
a[2] = 17
a[3] = 19
x = 0
y = 3
OPERATOR PRECEDENCE:
++, --
*, /, % Left Associative
+, - Left Associative
OPERAND EVALUATION ORDER:
Right to Left
Given the rules above, how would I evaluate the expression below?
a[++x] + ++x % 7 % y
According to my professor, the answer is 18, but I cannot figure out why. From what I understand associativity is the order same precedence operators are evaluated and operand evaluation order is the order operands get evaluated so something like 2 % 7 would be 2 with left to right operand evaluation order and 1 with operation evaluation order. Can anyone explain how my professor got the answer of 18?
The precedence and associativity tell you how the expression is (implicitly) parenthesised. The evaluation order then determines in which order the subexpressions are evaluated.
Let us look at the example:
a[++x] + ++x % 7 % y
On the top level, there are + and % as operators. + has lower precedence, so that's
a[++x] + (++x % 7 % y)
The right subexpression has two %, and that is left associative, hence
a[++x] + ((++x % 7) % y)
Now with right-to-left evaluation order, ((++x % 7) % y) is evaluated first. Again with right-to-left evaluation order, y is evaluated first, resulting in 3. Then ++x % 7 is evaluated. First 7, then ++x. The latter results in 1. So that's 1 % 7 = 1. I'll leave the rest to you, since it's homework.
You have () + () % 7 % y. Based on the rules, () % 7 is evaluated before ... % y and that before () + ....
In ++x % 7 you first evaluate ++x and get 1 and x=1. 1 % 7 = 1.
Then you do 1 % y or 1 % 3 and get 1.
Now you do a[++x] + 1. Remembering that x=1, you get a[2] + 1 = 17 + 1 = 18.