1

I am using the C++ language. I want to calculate these 2 expressions:-

In our case, $x= 100000000000000000$

Expression(1)

$$((3^x-1)/2)\mod7$$

The numerator $3^x-1$ is always divisible by $2$(basic number theory)

I calculated the above expression using Extended-Euclid-Gcd algorithm. The above algorithm only works when gcd(denominator,mod-value)=1...in our case $\gcd(2,7)=1$ . So we were able to calculate it using the above algorithm.

Expression(2)

$$((3^x-1)/2)\mod6$$

The numerator $3^x-1$ is always divisible by $2$(basic number theory-again)

Now, how do I calculate the above expression as $\gcd(2,6)=2$ which is not equal to $1$ ?

Bill Dubuque
  • 272,048

4 Answers4

3

For your first case, the extended Euclidean algorithm is probably the best way in general, as long as the denominator and modulus are coprime.

For your second case, consider $4\div 2$ modulo $6$. What should that be? Should it be $2$, as $2\cdot 2\equiv 4$? Or should it perhaps be $5$, as $5\cdot2\equiv 4$? There just isn't a single answer, and thus you can't really divide by $2$ modulo $6$.

If you're interested in what $\frac k2$ corresponds to modulo $6$, you have to find $k$ modulo $2\cdot 6=12$ and work your way from there. For instance, if $k\equiv_{12}4$, then $\frac k2$ is either $2$ or $8$ modulo $12$, meaning it must be $2$ modulo $6$.

Arthur
  • 199,419
  • See, an answer to the second expression does exist, so why are we not able to calculate it?? – Firex Firexo Jun 25 '19 at 09:04
  • 1
    @Guhfggehlthon We are unable to calculate it because there isn't "an" answer. There are several (or, if our number happens to be odd, there are none), and no way to distinguish between them directly. Most direct calculations abhor any kind of choice. Hence the detour through modulo 12 before performing any division is necessary. – Arthur Jun 25 '19 at 09:11
  • (+1) This is pretty much what I say in my answer, but I think our presentations show things in sufficiently different ways that I can leave my answer, but if you think they are too close, I will remove my answer. – robjohn Jun 25 '19 at 09:32
  • Nope the answer exists. The first answer gives a formula which is working all the time :-) – Firex Firexo Jun 25 '19 at 09:39
  • @Guhfggehlthon If you go via modulo 12, yes, sure. That's exactly what your favourite answer says, and it's exactly what my answer says. – Arthur Jun 25 '19 at 10:13
  • @Guhfg There is a simpler formula $\ a/2\bmod 6 = (a\bmod 12)/2,$ when $,2\mid a,,$ see my answer. – Bill Dubuque Jun 25 '19 at 15:37
  • Any of your valuable ideas on a similar problem :- https://math.stackexchange.com/questions/3275101/how-to-calculate-modulo-of-a-factorial – Firex Firexo Jun 26 '19 at 16:20
3

Dividing by $\boldsymbol{q}$ when the modulus is divisible by $\boldsymbol{q}$

Note that $$ aq\equiv bq\pmod{pq} $$ precisely when $$ a\equiv b\pmod{p} $$ Thus, for $n\gt0$, $$ 3^n-1\equiv\left\{\begin{array}{}2&\text{if $n$ is odd}\\8&\text{if $n$ is even}\end{array}\right.\pmod{12} $$ implies $$ \frac{3^n-1}2\equiv\left\{\begin{array}{}1&\text{if $n$ is odd}\\4&\text{if $n$ is even}\end{array}\right.\pmod{6} $$


A More Detailed Answer

This can be solved mod $6$ by solving mod $3$ and mod $2$, then applying the Chinese Remainder Theorem.

For $n\gt0$, $3^n\equiv0\pmod3$; therefore, $$ \frac{3^n-1}2\equiv1\pmod3 $$ To compute $$ \frac{3^n-1}2\pmod2 $$ we can to look at $$ 3^n-1\equiv(-1)^n-1\pmod4 $$ to get $$ \frac{3^n-1}2\equiv\frac{(-1)^n-1}2\equiv\left\{\begin{array}{}0&\text{if $n$ is even}\\1&\text{if $n$ is odd}\end{array}\right.\pmod2 $$ Then the Chinese Remainder Theorem says we can combine the results mod $3$ and mod $2$ to get $$ \frac{3^n-1}2\equiv\left\{\begin{array}{}4&\text{if $n$ is even}\\1&\text{if $n$ is odd}\end{array}\right.\pmod6 $$

robjohn
  • 345,667
  • Can you generalize this formula for any even 'x' where 'x' is the mod value ? Like mod2,mod4,mod6andso...on.... @robjohn – Firex Firexo Jun 25 '19 at 09:35
  • Anyways, the first answer has given a formula which works everytime :-) – Firex Firexo Jun 25 '19 at 09:39
  • @Guhfggehlthon: Look at what I wrote at the beginning: $pq\mid aq-bq\iff p\mid a-b$. So, if I understand your question, the answer is yes. – robjohn Jun 25 '19 at 09:40
  • One should prove the claimed results $\bmod 12.,$ As is they are pulled out of a hat like magic. But magic is not mathematics. – Bill Dubuque Jun 25 '19 at 14:04
  • @BillDubuque: the OP seems to be able to solve the equations with the Euclidean Algorithm; the question seemed to be more about the division by $2$ when $2$ was a factor of the modulus. My original answer solved the equation mod $3$ and mod $4$ (which are both extremely simple) and then put them together with the Chinese Remainder Theorem and reduced to mod 6. It seemed to hide the main point however. – robjohn Jun 25 '19 at 14:23
  • I didn't see the original version. It would be helpful to append that as a remark. – Bill Dubuque Jun 25 '19 at 14:25
  • Glad to see you added it back. In the meantime I added another approach using the mod distributive law. Between the two that should hopefully be enough for the OP. – Bill Dubuque Jun 25 '19 at 14:59
  • Any of your valuable ideas on a similar problem :- https://math.stackexchange.com/questions/3275101/how-to-calculate-modulo-of-a-factorial – Firex Firexo Jun 26 '19 at 16:20
2

Set $$y = \frac{3^x - 1}{2}.$$

Let $0 \leq b < 6$ such that $y = 6a + b$ for some integer $a$. Then $$2y = 12a + 2b$$ and $0 \leq 2b < 12$, so you could simply compute $3^x - 1 \mod{12}$.

Dirk
  • 6,359
  • something is wrong, say we have to calculate( (power(3,5)-1)/2)%6 , its real answer is 1 , but if I apply your formula, which is (power(3,5)-1)%12= 2 which is different from the real answer @Dirk. – Firex Firexo Jun 25 '19 at 09:10
  • 1
    @Guhfggehlthon Of course you compute $2b$ when doing stuff mod 12, so you have to divide the final result by two. :) – Dirk Jun 25 '19 at 09:31
  • Your answer is the perfect one :-) Thanks man. I figured out that we divide by 2 as soon as I ended writing the first comment. But I waited for you to correct me.:-) Wish I could upvote this 1000 times :-) – Firex Firexo Jun 25 '19 at 09:44
  • Any of your valuable ideas on a similar problem :- https://math.stackexchange.com/questions/3275101/how-to-calculate-modulo-of-a-factorial – Firex Firexo Jun 26 '19 at 16:20
1

For even $\,n,\,$ if we wish to compute $\ n/2\bmod 6\ $ then we need to know $\,n\bmod 12,\,$ i.e. we need to double the modulus to balance the division by $\,2,\,$ namely

$$\begin{align} {\rm notice}\ \ \ \, \color{}{n/2 \equiv r\!\!\!\pmod{\!6}}&\!\iff n\equiv 2r\!\!\!\!\pmod{\!12}\\[.4em] {\rm because}\ \ \ n/2\ =\ r\ +\ 6\,k\ \ &\!\iff n = 2r\ +\ 12\,k\end{align}\qquad$$

So first we compute $\,n\bmod 12 = 2r,\, $ then $\ r = n/2 \bmod 6\ $ by above, i.e.

$$\begin{align} r\,=\, \color{#c00}{n/2\bmod 6} \,&=\ \color{#c00}{(n\bmod 12)/2}\\[.4em] {\rm e.g.}\ \ \ 18/2 \bmod 6\, &= (18 \bmod 12)/2 = 6/2 = 3\end{align}\qquad$$

Beware $ $ We get the wrong result $\,0\,$ if we use $18\bmod 6$ vs. $18\bmod 12.\,$ See here for more.

Remark $ $ Below is a simpler way to compute $\,3^x\bmod 12\ $ (vs. using CRT mod $3\ \& 4)$

$$ 3^{K+{\large 1}}\!\bmod 12 = 3(3^K\!\bmod 4) = 3((-1)^K\!\bmod 4) = 3\ \ {\rm if}\ \ 2\mid K, \ {\rm else}\, \ 9$$

where we used $\ ab\bmod ac = a(b\bmod c) = $ mod Distributive Law

Note that our formula above is a special case of this law, namely

$$ 2\mid n\,\Rightarrow\ \color{#c00}{n\bmod 12\, =\, 2(n/2\bmod 6)}\qquad\qquad\qquad $$

Bill Dubuque
  • 272,048