Last Digit of $x^0 + x^1 + x^2 + \cdots + x^{p-1} + x^p$

109 Views Asked by At

Given $x$ and $p$. Find the last digit of $x^0 + x^1 + x^2 + \cdots + x^{p-1} + x^p$

I need a general formula. I can find that the sum is equal to $\dfrac{x^{p+1}-1}{x-1}$
But how to find the last digit.

P.S: $x\leq 999999$ and $p \leq 10^{15}$

3

There are 3 best solutions below

3
On BEST ANSWER

Assuming that $x>0$.

Let $d$ denote the last digit of the series.

You can split the answer into $10$ different cases.


The following cases are rather simple:

  • $x\equiv0\pmod{10} \implies d=1$
  • $x\equiv1\pmod{10} \implies d=(1+p)\bmod{10}$
  • $x\equiv4\pmod{10} \implies d=1+4(p\bmod{2})$
  • $x\equiv5\pmod{10} \implies d=1+5(p\bmod{2})$
  • $x\equiv9\pmod{10} \implies d=1-1(p\bmod{2})$

The following cases are a little more complicated:

  • $x\equiv2\pmod{10} \implies d=\frac{-4(p\bmod{4})^3+15(p\bmod{4})^2-5(p\bmod{4})+3}{3}$
  • $x\equiv3\pmod{10} \implies d=\frac{+1(p\bmod{4})^3-9(p\bmod{4})^2+17(p\bmod{4})+3}{3}$
  • $x\equiv6\pmod{10} \implies d=\frac{-5(p\bmod{5})^4+40(p\bmod{5})^3-100(p\bmod{5})^2+83(p\bmod{5})+3}{3}$
  • $x\equiv7\pmod{10} \implies d=\frac{+1(p\bmod{4})^3-15(p\bmod{4})^2+35(p\bmod{4})+3}{3}$
  • $x\equiv8\pmod{10} \implies d=\frac{+11(p\bmod{4})^3-54(p\bmod{4})^2+67(p\bmod{4})+3}{3}$
0
On

We'll work it out mod 5 and mod 2. We'll assume $p > 2$.

Mod 2, we have $x^i \equiv x$ for all $i$, except for $x^0$. Therefore the parity of the sum is just the same as the opposite of that of $x$ [unless $p=2$].

Mod 5: if $x \equiv 0 \pmod{5}$ then the result is certain to be $1$ mod $5$. Otherwise, $x^4 \equiv 1 \pmod{5}$, so we just have lots of expressions $1+x+x^2+x^3$ all added together; this is $4$ if $x \equiv 1 \pmod{5}$, and $0$ otherwise.

Hence:

  • If $x$ is $0$ mod 5, the result is 1 mod 5.
  • If $x$ isn't 1 mod 5, we'll get $1+x \pmod{5}$ if $p$ is $1$ mod 4, and $1+x+x^2+x^3 = 0$ if $p$ is $3$ mod 4.
  • If $x$ is 1 mod 5, we'll get $-(p-1)/4 + (1+x) \pmod{5}$ if $p$ is $1$ mod 4, and $-(p+1)/4 \pmod{5}$ if $p$ is $3$ mod 4.

These together are enough to assemble the answer.

0
On

Let $s = \sum_{k=0}^{p}x^k$. Calculate $0 \leq a_1, a_2 < 5$, where $$ a_1 \equiv s \pmod 2 \\ a_2 \equiv s \pmod 5 $$

$a_1$ is easy. If $x \equiv_2 0$, then $a_1 = 1$, else $a_1 \equiv_2 (1+p)$, cause it's $1 + 1^1 + 1^2 + 1^3 + \dots + 1^p$.

$a_2$ isn't harder, $x \equiv_5 0, 1$ are same. For $2, 3, 4$ you can easily observe regularity. Eg. $$ x \equiv_5 2 \Longrightarrow\\ s \equiv_5 1 + 2^1 + 2^2 + \dots + 2^{p} \equiv_5 \\ 1 + 2 + 4 + 8 + \dots + 2^{p} \equiv_5 \\ 1 + 2 + 4 + 3 + 1 + 2 \dots + 2^{p} \equiv_5\\ (1 + 2 + 4 + 3) + 1 + 2 \dots + 2^{p} \equiv_5\\ 2 \cdot 5 + 1 + 2 \dots + 2^{p} $$

If you know $a_1, a_2$ you can use Chinese remainder theorem, to calculate number $r \equiv_{10} s$ and it's last digit.