## 数论基础-原根与指标

| |
[ 所属分类 开发（python） | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

#! /usr/bin/env python # -*- coding=UTF-8 -*- def gcd(a, b): # 求最大公约数 while b != 0: (a,b) = (b, a%b) return a def factorize(n, wheel=3): # 因数分解 if n < 2: return [] primes = (2, 3, 5, 7, 11) if wheel < 2 or wheel > len(primes): wheel = 3 primes = primes[:wheel] q = [] for p in primes: if n % p != 0: continue e = 1 n //= p while n % p == 0: n //= p e += 1 q.append((p, e)) if n > 1: m = reduce(lambda x, y:x*y, primes, 1) offs = [x for x in xrange(2, m + 1) if gcd(m, x) == 1] + [m + 1] k, done = 0, False while n > 1: for offset in offs: p = k + offset if p ** 2 > n: done = True break if n % p != 0: continue e = 1 n //= p while n % p == 0: n //= p e += 1 q.append((p, e)) if done: break k += m if n > 1: q.append((n, 1)) return q def Euler(n, wheel=3): # 求欧拉函数 ''' Euler Totient Function of n using a prime wheel criterion. It's almost as fast as the phi(n, p) function ''' if n < 2: return n q = factorize(n, wheel) r = 1 for (p, e) in q: r *= (p - 1) * (p ** (e - 1)) return r def primitive_root(mod): # 求原根 simple = [] root = [] phi_m = Euler(mod) for i in range(1,mod): if gcd(i, mod) == 1: simple.append(i) for a in simple: for j in range(1,phi_m+1): if a ** j % mod == 1: if j == phi_m: root.append(a) else: break return root if __name__ == '__main__': mod = raw_input('Please input mod:') print primitive_root(int(mod))`

1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责；
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性，不作出任何保证或承若；
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。