Skip to content

Instantly share code, notes, and snippets.

@rinfz
rinfz / unit vector
Created February 12, 2013 22:31
Attempted implementation of generating a unit vector from a given offset.
double *randomVector(double v[2], int x, int y){
/* generates two coordinates on a unit circle which are used
* for the 4 vectors from each grid point.
*/
srand(time(NULL));
double r;
int cont = 1;
/* http://burtleburtle.net/bob/rand/unitvec.html */
do {
double m = (double)rand()/(double)(RAND_MAX-1);
@rinfz
rinfz / unit_vector_2
Created February 13, 2013 01:30
Working implementation of a random unit vector. Not sure why this took me so long.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
double fRand(double min, double max){
double f = (double)rand()/RAND_MAX;
return min+f*(max-min);
}
import itertools
import os, sys
import numpy
def get_data_range(filename):
start = ''.join([n for n in itertools.takewhile(lambda x: x != ",", open(filename).read())])
end = ''.join(list(reversed([n for n in itertools.takewhile(lambda x: x != ",", reversed(open(filename).read()))])))
ret = [start, end]
return eval(min(ret)), eval(max(ret)[:-1])
def sieve(limit):
"""
Performs the Sieve of Eratosthenes
:return: A list of prime numbers less than a given limit
"""
numbers = [True] * (limit + 1)
for i in range(2, int(math.sqrt(limit))):
if numbers[i]:
j_increment = lambda j: [int(math.pow(j, 2) + (n * j)) for n in
itertools.takewhile(lambda x: int(math.pow(j, 2) + (x * j)) <= limit,
import nltk
def noun_with_adjectives(sentence):
""" selects the first occurrence of a noun in a sentence
with any accompanying adjectives """
tags = nltk.pos_tag(sentence.split(" "))
phrase = []
for tag in tags:
if tag[1] in ["JJ", "JJS", "JJR"]:
phrase.append(tag[0])
while self.counter <= self.limit:
for k in range(self.delta):
if self.counter in data:
self.output.append([self.current_x, self.current_y, self.counter])
self.counter += 1
self.next_point()
if self.side % 2 == 0:
self.delta += 1
self.direction = (self.direction + 1) % 4
self.side += 1
(defn square-root
([c] (square-root c 1e-15))
([c epsilon]
(loop [t (double c)]
(if (float= t (/ c t) epsilon)
t
(recur (double (/ (+ t (/ c t)) 2)))) )))
(defn ^:static is-prime? [^long n]
(if (<= n 3)
(define (is-prime n)
(if (<= n 3)
(>= n 2)
(if (or (= (modulo n 2) 0) (= (modulo n 3) 0))
#f
(do ((i 5 (+ i 6))) ((> i (+ (sqrt n) 1)))
(if (or (= (modulo n i) 0) (= (modulo n (+ i 2)) 0))
#f))) #t))
(define (main args)
@rinfz
rinfz / gist:e1dadb81095d459e30c3
Last active August 29, 2015 14:16
sexy primes
(import srfi-1)
(require-extension srfi-1)
(define (is-prime? n)
(cond
((<= n 3) (>= n 2))
((or (= (modulo n 2) 0) (= (modulo n 3) 0)) #f)
(else (do ((i 5 (+ i 6))) ((> i (+ (sqrt n) 1)))
(if (or (= (modulo n i) 0) (= (modulo n (+ i 2)) 0))
#f)) #t)))
@rinfz
rinfz / three.py
Last active December 3, 2015 22:29
three
with open('three.txt', 'r') as h:
data = h.read()
print(math.ceil(1.25 * (abs(data.count('^') - data.count('v') * data.count('<') - data.count('>')) >> 11) - 1))