ph
0812 large a n mod p 문서 원본 보기
←
0812 large a n mod p
다음으로 이동:
둘러보기
,
검색
문서 편집 작업을 수행할 권한이 없습니다. 다음 이유를 확인해주세요:
요청한 작업은 다음 권한을 가진 사용자에게 제한됩니다:
사용자
.
문서의 원본을 보거나 복사할 수 있습니다.
problem : for very large natural numbers \(a, n, p\), find the value of \(a^n \pmod{p}\). Below process is finding the period. But it will appear to be unnecessary. <pre> def a_exp_n_mod_p(a,n,p): # `a` can be factorized. but it does not applied here, maybe it will make this much slower. # Factorization is a very expensive process. assert a > 0 and n > 0 and p > 0 if a%p ==0 : return 0 # find the period. # a^(2^n) has a period. LIMIT = 10000 # period limit x = [0]*LIMIT anmodp = a%p for i in range(LIMIT): if anmodp in x: x = x[:i] for j in range(len(x)): if x[j]==anmodp: break break x[i] = anmodp anmodp *= anmodp anmodp %= p assert len(x) < LIMIT period_len = len(x)-j #print period_len #print len(x) digit = 0 r = 1 while n>0: if n%2 == 1: r *= x[digit] r %= p n /= 2 digit += 1 if digit >= len(x): digit -= period_len assert digit >= 0 return r assert a_exp_n_mod_p(3,20,20) == pow(3,20)%20 assert a_exp_n_mod_p(12,15,20) == pow(12,15)%20 assert a_exp_n_mod_p(12,15,19) == pow(12,15)%19 assert a_exp_n_mod_p(4,15,497) == pow(4,15)%497 print a_exp_n_mod_p(2032343532,24234234324,2342434234) 1368242842 </pre> Below will be enough. <pre> def anmodp(a, n, p): if n==1: return a%p tmp = anmodp(a, n/2, p) if n%2==1: #odd return (a*(tmp**2))%p else: return (tmp**2)%p </pre> \(O(\log n)\) process can use recursive call freely. <disqus />
0812 large a n mod p
문서로 돌아갑니다.