powermod
Modular exponentiation
Syntax
Description
c = powermod(
returns the modular exponentiation ab mod m. The input a,b,m)a,b must be integers, and
m must be a nonnegative integer. For more information, see
Modular Exponentiation.
Examples
Compute the modular exponentiation ab mod m by using powermod. The
powermod function is efficient because it does not
calculate the exponential ab.
c = powermod(3,5,7)
c =
5Fermat's little theorem states that if p is prime and a is not divisible by p, then a(p–1) mod p is 1.
Test Fermat's little theorem for p = 5, a =
3. As expected, powermod returns
1.
p = 5; a = 3; c = powermod(a,p-1,p)
c = 1
Test the same case for all values of a less than
p. The function powermod acts
element-wise to return a vector of ones.
p = 5; a = 1:p-1; c = powermod(a,p-1,p)
c =
1 1 1 1Fermat's little theorem states that if p is a prime number and a is not divisible by p, then a(p–1) mod p is 1. On the contrary, if a(p–1) mod p is 1 and a is not divisible by p, then p is not always a prime number (p can be a pseudoprime).
Test numbers from 300 to 400 for
primality by using Fermat's little theorem with base
2.
p = 300:400; remainder = powermod(2,p-1,p); primesFermat = p(remainder == 1)
primesFermat = 307 311 313 317 331 337 341 347 349 353... 359 367 373 379 383 389 397
Find Fermat pseudoprimes by comparing the results with
isprime. 341 is a Fermat
pseudoprime.
primeNumbers = p(isprime(p)); setdiff(primesFermat,primeNumbers)
ans = 341
Input Arguments
Base, specified as a number, vector, matrix, array, or a symbolic number
or array. a must be an integer.
Exponent or power, specified as a number, vector, matrix, array, or a
symbolic number or array. b must be an integer.
Divisor, specified as a number, vector, matrix, array, or a symbolic
number or array. m must be a nonnegative
integer.
More About
For a positive exponent b, the modular exponentiation c is defined as
c = ab mod m.
For a negative exponent b, the definition can be extended by finding the modular multiplicative inverse d of a modulo m, that is
c = d ‒b mod m.
where d satisfies the relation
ad mod m = 1.
Version History
Introduced in R2018a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.
América
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)