Skip to content

Instantly share code, notes, and snippets.

@mandeluna
mandeluna / frequency.py
Created August 3, 2013 05:01
I wanted to calculate the frequency of an LC circuit, but I kept getting hung up on conversions between component values and equivalents in scientific notation, so I made this gist
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import math
#
# Provide the Metric (SI) prefix for a measurement
#
def si_prefix(measure, format="%f"):
if measure > 1e24:
@mandeluna
mandeluna / GenSequence.st
Created November 27, 2012 17:52
Generate Excel style column headers in Smalltalk
| countDigits genSequence |
countDigits := [:n :base | ((n >= 0) and: [n < base])
ifTrue: [1] ifFalse: [1 + (countDigits value: (n - base) / base value: base)]].
genSequence := [:n :base :origin | | num_digits str intValue |
num_digits := countDigits value: n value: base.
intValue := (1 to: num_digits - 1)
inject: n
into: [:value :power | value - (base ** power)].
str := String new: num_digits.
@mandeluna
mandeluna / intersection.py
Created July 29, 2012 01:46
if you want straight lines connecting the points of a star
#
# intersection.py
#
# find the ratio of the inner radius to the outer radius if you want straight
# lines connecting the points of a 5 pointed-star
#
from math import *
tau = 2 * pi
r1 = 100
@mandeluna
mandeluna / checkEncoding.st
Created July 13, 2012 19:26
VW7 BOM in UTF-8 encoded XML fails to parse
checkEncoding
| encoding |
encoding := [stream encoding] on: Error do: [:ex | ex returnWith: #null].
encoding = #'UTF-8'
ifTrue:
[| c1 c2 c3 pos |
pos := stream position.
stream setBinary: true.
c1 := stream next.
@mandeluna
mandeluna / sierpinsky.ws
Created June 27, 2012 16:48
Sierpinsky Gasket in VisualWorks Smalltalk
"
The normal algorithm for solving for S is called the chaos game.
In pseudocode it is:
(x, y)= a random point in the bi-unit square
iterate {
i = a random integer from 0 to n - 1 inclusive
(x, y) = Fi (x, y)
plot(x, y) except during the first 20 iterations
}
@mandeluna
mandeluna / weasel.py
Created June 19, 2012 20:58
# A naive implementation of the genetic algorithm described at # http://en.wikipedia.org/wiki/Weasel_program
#!/usr/bin/python
#
# weasel.py
#
# A naive implementation of the genetic algorithm described at
# http://en.wikipedia.org/wiki/Weasel_program
#
import random
@mandeluna
mandeluna / logfact.py
Created June 15, 2012 20:51
ratio between n^n and n!
#
# log((n^n)/n!) approaches n as n gets large
#
# according to Stirling's formula the difference
# between log(n^n) and log(n!)
# approaches n - log(sqrt(2*pi*n))
#
import math
def logfact(n):
@mandeluna
mandeluna / CPS_Fib.st
Created May 11, 2012 21:56
CPS implementation of fib(n) in Smalltalk (with performance comparisions to memoized implementation)
| dict memoized fib_cps t |
dict := Dictionary new.
memoized := [:n | (n <= 2) ifTrue: [1] ifFalse: [
dict at: n ifAbsentPut: [(memoized value: (n-1)) + (memoized value: (n-2))]]].
t := Time millisecondsToRun: [1000 timesRepeat: [memoized value: 24]].
Transcript cr; print: (memoized value: 24).
Transcript cr; show: 'memoized: '; print: (t/1000.0); flush.
fib_cps := [:n :k | | cont |
cont := [:n1 | fib_cps value: (n - 2) value: [:n2 | k value: (n1 + n2)]].
@mandeluna
mandeluna / Fibonnaci.c
Created May 11, 2012 20:52
fibonnaci timing for Linux. Not well tested, stupid command-line parsing
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>
unsigned long long *cache;
unsigned long long fib_memoize(int n) {
if (n <= 2) {