Skip to content

Instantly share code, notes, and snippets.

View mdnestor's full-sized avatar

Michael Nestor mdnestor

View GitHub Profile
@mdnestor
mdnestor / diffe-hellman.py
Created May 9, 2024 09:25
Diffe-Hellman key exchange in Python 3
# Diffe-Hellman key exchange
# https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange
import math
def least_prime_factor(n: int) -> int:
assert(n > 1)
for d in range(2, int(n**(1/2))):
if n % d == 0:
return d
# Algorithm Minecraft uses to convert world seeds to integers
# Shows why `creashaks organzine` yields seed 0, whereas by design manually inputting seed 0 uses random seed.
def seed(x: str) -> int:
if isinstance(x, int):
return x
s = 0
for c in x:
s = ((31*s + ord(c)) ^ 0x80000000) & 0xFFFFFFFF - 0x80000000
return s
import numpy as np
import pygame
from scipy.ndimage import convolve
def game_of_life(state):
kernel = np.array([[2,2,2],[2,1,2],[2,2,2]])
activation = np.vectorize(lambda x: int(5 <= x <= 7))
return activation(convolve(state, kernel, mode='wrap'))
pygame.init()