Skip to content

Instantly share code, notes, and snippets.

View KevinMichelle's full-sized avatar

Kevin Michelle Contreras González KevinMichelle

  • Nuevo León, México
View GitHub Profile
@KevinMichelle
KevinMichelle / chinese.py
Last active August 29, 2015 14:06
Teorema chino del resto (chinese remainder theorem)
def egcd(a, b):
auxiliar = (a, b)
a = max(auxiliar)
b = min(auxiliar)
x = (1, 0)
y = (0, 1)
while a % b != 0:
q = a / b
c = a % b
nx = x[0] - (x[1] * q)
@KevinMichelle
KevinMichelle / resultados-chinese
Last active August 29, 2015 14:06
Resultados del teorema chino del resto
>>> chinese()
29246
>>> a = [4, 5, 6, 7, 8, 9]
>>> m = [2, 3, 5, 7, 11, 13]
>>> x = 29246
>>> for i in xrange(0, len(a)):
if (a[i] - x) % m[i] == 0:
print "{} es congruente con {} modulo {}".format(a[i], x, m[i])
@KevinMichelle
KevinMichelle / resultados-expmod
Created September 18, 2014 14:02
Resultados de la exponenciación modular
>>> expmod(33, 93, 97)
64
>>> pow(33, 93, 97)
64
>>> expmod(12822, 1498, 997)
313
>>> pow(12822, 1498, 997)
313
>>>
@KevinMichelle
KevinMichelle / expmod.py
Created September 18, 2014 13:59
Exponenciación modular
def expmod(a, b, n):
rexpo = 1
pot = a % n
while b > 0:
if b % 2 == 1:
rexpo = (rexpo * pot) % n
b = b / 2
pot = (pot * pot) % n
return rexpo
@KevinMichelle
KevinMichelle / resultados-gcd
Created September 18, 2014 13:55
Resultados del gcd
>>> gcd(765, 4321)
son relativamente primos
>>> gcd(765, 4323)
no son relativamente primos
>>> gcd(765, 4324)
son relativamente primos
>>> gcd(765, 4325)
no son relativamente primos
>>>
@KevinMichelle
KevinMichelle / gcd.py
Last active August 29, 2015 14:06
GCD
def gcd(a, b):
auxiliar = (a, b)
a = max(auxiliar)
b = min(auxiliar)
while a % b != 0:
c = a % b
a = b
b = c
resultado = b
if resultado == 1:
@KevinMichelle
KevinMichelle / resultados-egcd
Created September 18, 2014 13:46
Resultados del egcd
>>> egcd(765, 4321)
(1, 91, -514)
>>> 4321 * (91) + 765 * (-514)
1
>>>
@KevinMichelle
KevinMichelle / egcd.py
Created September 18, 2014 13:44
EGCD
def egcd(a, b):
auxiliar = (a, b)
a = max(auxiliar)
b = min(auxiliar)
x = (1, 0)
y = (0, 1)
while a % b != 0:
q = a / b
c = a % b
nx = x[0] - (x[1] * q)
@KevinMichelle
KevinMichelle / resultados-factorizar
Created September 18, 2014 13:42
Resultados de la factorización
>>> for i in xrange(1, 51):
resultado = factorizar(i)
print str(i) + ' - ' + str(resultado)
1 - [1]
2 - [2]
3 - [3]
4 - [2, 2]
5 - [5]
@KevinMichelle
KevinMichelle / factorizar.py
Created September 18, 2014 13:39
Factorización con división por tentativa
import math
def factorizar(n):
factores = []
if n > 1:
for i in xrange(2, n + 1):
while n % i == 0:
n = n / i
factores.append(i)
if n == 1: