pub fn primitive_root(p: usize) -> (usize, Vec<usize>) {
        // discrete_logarithm[a-1] = the dlog of a
        // or 0 if a hasn't been occurred
        let mut discrete_logarithm = vec![0; p - 1];
        if p == 2 {
            return (1, discrete_logarithm);
        }
        'a_loop: for a in 2..p {
            if discrete_logarithm[a - 1] != 0 {
                continue;
            }
            discrete_logarithm[a - 1] = 1;
            let mut a_power = ModInt::new_with(a, p);
            // calculate a^2 to a^(p-2)
            for i in 2..p - 1 {
                a_power = a_power * ModInt::new_with(a, p);
                if a_power.value() == 1 {
                    continue 'a_loop;
                }
                discrete_logarithm[a_power.value() - 1] = i;
            }
            return (a, discrete_logarithm);
        }
        panic!();
    }
// Calculate the smallest primitive root mod p
// and the corresponding discrete logarithm table
// The argument p must be a prime
// The dlog of a is in table[a-1]
// Time: O(p)
Calculate the smallest primitive root mod p
and the corresponding discrete logarithm table
The argument p must be a prime
The dlog of a is in table[a-1]
Time: O(p)
Calculate the smallest primitive root mod p and the corresponding discrete logarithm table The argument p must be a prime The dlog of a is in table [ a - 1 ] Time : O ( p )
pub fn primitive_root(p: usize) -> (usize, Vec<usize>) {
        // discrete_logarithm[a-1] = the dlog of a
        // or 0 if a hasn't been occurred
        let mut discrete_logarithm = vec![0; p - 1];
        if p == 2 {
            return (1, discrete_logarithm);
        }
        'a_loop: for a in 2..p {
            if discrete_logarithm[a - 1] != 0 {
                continue;
            }
            discrete_logarithm[a - 1] = 1;
            let mut a_power = ModInt::new_with(a, p);
            // calculate a^2 to a^(p-2)
            for i in 2..p - 1 {
                a_power = a_power * ModInt::new_with(a, p);
                if a_power.value() == 1 {
                    continue 'a_loop;
                }
                discrete_logarithm[a_power.value() - 1] = i;
            }
            return (a, discrete_logarithm);
        }
        panic!();
    }
pub fn primitive_root ( p : usize ) -> ( usize , Vec < usize > ) { // discrete_logarithm[a-1] = the dlog of a // or 0 if a hasn't been occurred let mut discrete_logarithm = vec ! [ 0 p 1 ] ; if p == 2 { return ( 1 , discrete_logarithm ) ; } ' a_loop : for a in 2 .. p { if discrete_logarithm [ a - 1 ] != 0 { continue ; } discrete_logarithm [ a - 1 ] = 1 ; let mut a_power = ModInt :: new_with ( a , p ) ; // calculate a^2 to a^(p-2) for i in 2 .. p - 1 { a_power = a_power * ModInt :: new_with ( a , p ) ; if a_power . value ( ) == 1 { continue ' a_loop ; } discrete_logarithm [ a_power . value ( ) - 1 ] = i ; } return ( a , discrete_logarithm ) ; } panic ! ( ) ; }
Calculate the smallest primitive root mod p
and the corresponding discrete logarithm table
The argument p must be a prime
The dlog of a is in table[a-1]
Time: O(p)
Calculate the smallest primitive root mod p and the corresponding discrete logarithm table The argument p must be a prime The dlog of a is in table [ a - 1 ] Time : O ( p )
p
usize
p
usize