Skip to content

Instantly share code, notes, and snippets.

View md2perpe's full-sized avatar

Per Persson md2perpe

View GitHub Profile
@md2perpe
md2perpe / freefall.md
Created February 9, 2026 20:55
Two electrons in freefall

Two electrons in freefall, one says to the other: “Relax, I’ve got a positive feeling about this.”

The other electron groans. “Great. Last time you had a positive feeling, we got attracted to a proton with commitment issues.”

As they tumble past a passing photon, the photon zips by and shouts, “Heads up! Relativity says you’re falling, but from my frame, you’re just having an energetic lifestyle change!”

One electron spins faster. “Do you ever feel like we’re just waves pretending to be particles so we can fit into society?”

The other sighs. “All the time. I tried to settle down once, but my position and momentum just couldn’t agree.”

@md2perpe
md2perpe / main.hs
Created January 3, 2025 20:49
Ten thousand factorial (10000!)
module Main where
fac_ :: Integer -> Integer -> Integer
fac_ 0 p = p
fac_ n p = fac_ (n-1) (n*p)
fac :: Integer -> Integer
fac n = fac_ n 1
main = do
@md2perpe
md2perpe / prime_iterator.py
Last active April 17, 2022 18:27
Prime iterator
import itertools
from typing import Iterator
def sift(ns: Iterator[int]) -> Iterator[int]:
p = next(ns)
yield p
yield from sift(filter(lambda n: n%p != 0, ns))
def primes():
yield from sift(itertools.count(2))
@md2perpe
md2perpe / sum_big_and_small_numbers.py
Created February 28, 2022 11:23
Sum big and small numbers
BIG = 1_000_000.0
SMALL = 1e-12
COUNT = 1_000_000
def big_first():
yield BIG
for _ in range(COUNT):
yield SMALL
words = ['peace','piece','geese','tease','spoon']
def ham_dis(word1, word2):
return sum(c1 != c2 for c1, c2 in zip(word1, word2))
output = [
[ 'Ham-Dis', *words ],
*(
[ w1, *(ham_dis(w1, w2) for w2 in words) ]
for w1 in words
' ** PYTHON CODE CHALLENGE - SCHOOL ADMISSIONS ** '
# write code to handle admissions for a school - print results
# rules: pass test and interview. If applicant is legacy
# passing either test or interview is enough to be admitted
# your output should be sorted
# output = 'Accepted students: An, Bo, Mo, My, Xi'
appl =['Jay','Sam','Vi','Li','My','Xi','On','Mo','An','Bo']
test_ok =['xi','my','sam','an','mo','on']
#include <stdio.h>
typedef int square;
typedef int row;
typedef int col;
int is_valid_row(row n)
{
return (
n == 0b0011 ||
@md2perpe
md2perpe / pi_sequence.hs
Created March 20, 2021 22:40
Pi sequence
t :: Integer -> Float
t 0 = 0
t n = sqrt(2 + t(n-1))
p :: Integer -> Float
p n = 2**(n+1) * sqrt(2 - t(n))
@md2perpe
md2perpe / monads.ts
Created February 21, 2021 22:26
Monads
interface Thenable<a> {
then<b>(cont: (_: a) => Thenable<b>): Thenable<b>;
}
/*
run_slow_task.then((result) => {
do_something(result).then((new_result) => {
do_something_else(new_result)
})
})