Instantly share code, notes, and snippets.

# Michael Nestor mdnestor

Created May 9, 2024 09:25
Diffe-Hellman key exchange in Python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 # 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
Last active May 9, 2024 09:30
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 # 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
Last active May 9, 2024 09:26
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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()