Fast Exponentiation: Time Complexity and Number of Bit Operations

In summary: You can try something like this:$$\left|\begin{array}{l} u\leftarrow n-1 \\ k\leftarrow 0 \\ \text{while } u \text{ even} \\\left| \begin{array}{l} u \leftarrow u / 2 \\ k \leftarrow... \end{array} \right.$$This can be solved for $u$ and $k$ using the quadratic Formula.
  • #1
evinda
Gold Member
MHB
3,836
0
Hello! (Wave)

It is stated in my notes that the calculation of $a^{n-1} \pmod{n}$ by fast exponentiation takes $O(\log{n})$ arithmetic operations and $O((\log{n})^3)$ bit operations.By using fast exponentiation, in order to calculate $a^{n-1} \pmod{n}$ we write $n-1$ as $n-1=n_0+2 n_1+ \dots+ 2^k n_k$.

We calculate $a^2, (a^2)^2, \dots, (a^{2^{k-1}})^2$.

Does each calculation $b^2$, where $b=a^{2^{j}}$, require time $\log^2n$ and since we make $k \leq \log{(n-1)}\leq \log{n}$ operations, we need totally $O(\log^3 n)$ time?

Is the time complexity needed equal to the number of bit operations?
 
Mathematics news on Phys.org
  • #2
evinda said:
Does each calculation $b^2$, where $b=a^{2^{j}}$, require time $\log^2n$ and since we make $k \leq \log{(n-1)}\leq \log{n}$ operations, we need totally $O(\log^3 n)$ time?
Yes. Each multiplication takes time proportional to the square of the number of bits. Since computation is done modulo $n$, it requires approximately $\log n$ bits. There is also about $\log n$ additions, but each takes only time proportional to the number of bits.

evinda said:
Is the time complexity needed equal to the number of bit operations?
Yes.
 
  • #3
Evgeny.Makarov said:
There is also about $\log n$ additions, but each takes only time proportional to the number of bits.

You mean the additions we make to write this: $n-1=n_0+ 2n_1+ \dots+ 2^k n_k$ ? If so, don't we also need to make $k$ divisions by $2$ in order to find the $n_i$s for $i=0$ to $k$?
 
  • #4
evinda said:
You mean the additions we make to write this: $n-1=n_0+ 2n_1+ \dots+ 2^k n_k$ ?
I should have said we need up to $k$ additional multiplications. This is what we do with $a^2,\ldots,a^{2^k}$.

evinda said:
If so, don't we also need to make $k$ divisions by $2$ in order to find the $n_i$s for $i=0$ to $k$?
This too. Though if $n$ is considered constant, then the binary representation of $n-1$ can be computed in advance and can be considered known.
 
  • #5
Evgeny.Makarov said:
I should have said we need up to $k$ additional multiplications. This is what we do with $a^2,\ldots,a^{2^k}$.

Ah I see...

Evgeny.Makarov said:
This too. Though if $n$ is considered constant, then the binary representation of $n-1$ can be computed in advance and can be considered known.

Ok, but if we would want to find the time complexity in order to find the binary representation of $n-1$ then we would need to make $k$ divisions by $2$. So the time complexity would be $k \cdot (\log{n} \cdot \log{2} )=O(\log^2{n})$, right?
 
  • #6
The problem statement probably says that we are given binary representations of $a$ and $n$ and have to compute the binary representation of $a^{n-1}\pmod{n}$. In this case computing the binary representation of $n-1$ takes time proportional to the number of bits in $n$, i.e., $\log n$.
 
  • #7
Evgeny.Makarov said:
The problem statement probably says that we are given binary representations of $a$ and $n$ and have to compute the binary representation of $a^{n-1}\pmod{n}$. In this case computing the binary representation of $n-1$ takes time proportional to the number of bits in $n$, i.e., $\log n$.

How do we compute the binary representation of $n-1$ in time proportional to $\log{n}$ ?
I am looking at the algorithm of the Fermat test where the binary representation of $n$ is given , we choose a random $a \in \{2, \dots, n-2\}$ and we want to calculate $a^{n-1} \pmod{n}$, in order to deduce whether $n$ is prime or composite.
 
  • #8
evinda said:
How do we compute the binary representation of $n-1$ in time proportional to $\log{n}$ ?
I am looking at the algorithm of the Fermat test where the binary representation of $n$ is given
There is such operation as subtracting one. (Smile) It can be done on numbers in binary.
 
  • #9
Evgeny.Makarov said:
There is such operation as subtracting one. (Smile) It can be done on numbers in binary.

Ah, so we just subtract 0001 from the number $n$ given?
I see... Thank you very much! (Smirk)
 
  • #10
I have a question. When we have the binary representation of $n-1$, how can we find the $u,k$ such that $n-1=u \cdot 2^k$ ?
 
  • #11
evinda said:
When we have the binary representation of $n-1$, how can we find the $u,k$ such that $n-1=u \cdot 2^k$ ?
You can always take $k=0$ and $u=n-1$...
 
  • #12
Evgeny.Makarov said:
You can always take $k=0$ and $u=n-1$...

Oh sorry, I forgot to mention that $u$ has to be odd.
 
  • #13
And if you have a decimal representation of $n-1$, are you able to find $u$ and $k$ such that $u$ is not a multiple of $10$ and $n-1=u10^k$?
 
  • #14
evinda said:
Oh sorry, I forgot to mention that $u$ has to be odd.

How about something like:
$$
\left|\begin{array}{l} u\leftarrow n-1 \\ k\leftarrow 0 \\ \text{while } u \text{ even} \\
\left| \begin{array}{l} u \leftarrow u / 2 \\ k \leftarrow k+1\end{array}\right.\end{array}\right.
$$
 
  • #15
But we have the binary representation of the number...
 
  • #16
I think that we count the number of successive zeros from the right to the left till we find one nonzero element.
The number of $0$s that we find will be equal to $k$.
We delete these $0$s and $u$ will be equal to the number that corresponds to the binary representation of the remaining elements.
 
  • #17
I agree.
 
  • #18
Evgeny.Makarov said:
I agree.

Nice. (Smile)
And if we are given the binary representation of some number $b$ and want to divide it by $2$ or $4$, we get the result by deleting the last, or the last two elements of $b$, respetively, right?
Also the remainder of $b$ modulo $4$ or modulo $8$ will be equal to the last two or the last three elements of $b$ respectively, right?
 
  • #19
evinda said:
And if we are given the binary representation of some number $b$ and want to divide it by $2$ or $4$, we get the result by deleting the last, or the last two elements of $b$, respetively, right?
Yes, if you through away the remainder and take only the quotient.

evinda said:
Also the remainder of $b$ modulo $4$ or modulo $8$ will be equal to the last two or the last three elements of $b$ respectively, right?
Yes.
 
  • #20
Evgeny.Makarov said:
Yes, if you through away the remainder and take only the quotient.

Yes.

Nice. Thanks a lot! (Happy)
 

1. What is the definition of "number of bit operations"?

The number of bit operations refers to the number of operations or manipulations performed on binary digits (bits) in a computer program or algorithm. These operations can include basic arithmetic, logic, and data movement.

2. Why is the number of bit operations important in computer science?

The number of bit operations is important because it directly affects the efficiency and speed of a computer program or algorithm. The fewer bit operations that are required, the faster the program will run, making it more efficient.

3. How is the number of bit operations calculated?

The number of bit operations is calculated by counting the number of individual operations performed on bits in a given program or algorithm. This can include addition, subtraction, multiplication, division, and logical operations such as AND, OR, and NOT.

4. What is the significance of reducing the number of bit operations?

Reducing the number of bit operations can lead to significant improvements in the efficiency and speed of a computer program or algorithm. This can result in faster execution times, reduced memory usage, and ultimately, more efficient use of computing resources.

5. How can the number of bit operations be optimized?

The number of bit operations can be optimized by using efficient algorithms, data structures, and programming techniques. This can include using bitwise operations, optimizing loops, and minimizing unnecessary operations. Additionally, advancements in computer hardware, such as faster processors and increased memory, can also contribute to reducing the number of bit operations.

Similar threads

Replies
4
Views
931
  • Precalculus Mathematics Homework Help
Replies
7
Views
825
  • General Math
Replies
16
Views
3K
Replies
2
Views
3K
  • General Math
Replies
3
Views
815
Replies
5
Views
2K
  • General Math
Replies
8
Views
2K
Replies
3
Views
277
  • Precalculus Mathematics Homework Help
Replies
17
Views
1K
  • General Math
3
Replies
96
Views
10K
Back
Top