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
#!/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: |
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
| 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. |
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
# | |
# 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 |
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
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. |
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
" | |
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 | |
} |
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
#!/usr/bin/python | |
# | |
# weasel.py | |
# | |
# A naive implementation of the genetic algorithm described at | |
# http://en.wikipedia.org/wiki/Weasel_program | |
# | |
import random |
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
# | |
# 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): |
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
| 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)]]. |
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
#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) { |
NewerOlder