Advertisement
nac13k

funciones Matematicas

Jul 3rd, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from random import randint
  4. from gcd import egcd
  5. from math import sqrt
  6.  
  7. def prime(n):
  8.     if n <=1:
  9.         return False
  10.     if n % 2 == 0:
  11.         if n == 2:
  12.             return True
  13.         else:
  14.             return False
  15.     i = 3
  16.     while i <= sqrt(n):
  17.         if n % i == 0:
  18.             return False
  19.         i += 2
  20.     return True
  21.  
  22.  
  23. def inversa(e, n):
  24.     (gcd,tmp,d) = egcd(e, n)
  25.     if not gcd == 1:
  26.         return None
  27.     else:
  28.         return (tmp+n) %n
  29.  
  30. def rprime(digits):
  31.     minimum = 10**(digits-1)
  32.     maximum = minimum * 10 - 1
  33.     while True:
  34.         r = randint(minimum,maximum)
  35.         if prime(r):
  36.             return r
  37.  
  38. def checkgen(n,mod):
  39.     t = 1
  40.     c = 0
  41.     while not t == 1 or c == 0:
  42.         t = (t*n) % mod
  43.         c+=1
  44.         if c == mod:
  45.             break
  46.     if c == mod - 1:
  47.         return True
  48.     else:
  49.         return False
  50.  
  51. def findgen(mod):
  52.     while True:
  53.         cand = randint(2,mod -1)
  54.         if checkgen(cand,mod):
  55.             return cand
  56.  
  57. def pickone(mod):
  58.     return randint(2,mod-1)
  59.  
  60. def potmod(x, pot, mod):
  61.     bits = pot
  62.     pot
  63.     res = 1
  64.     temp = x
  65.     while bits > 0:
  66.         if bits %2 == 1:
  67.             res = (res * temp  % mod)
  68.         temp = (temp * temp) % mod
  69.         bits = bits >> 1
  70.     return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement