Skip to content

Instantly share code, notes, and snippets.

View kristenmills's full-sized avatar

Kristen Mills kristenmills

View GitHub Profile
@kristenmills
kristenmills / problem69.rb
Created May 29, 2012 22:36
Project Euler 69
def find_max limit=1000000
lst = prime_sieve 1000
n = 1
while n < limit
if lst[0]*n > limit
return n
end
n *= lst[0]
lst = lst.drop 1
end
@kristenmills
kristenmills / problem243.rb
Created May 29, 2012 22:37
Project Euler 243
def resiliance limit = Rational(15499,94744)
list = prime_sieve(23)
sum = list.map{|x| 1-1.0/x}.reduce(:*)
val = list.reduce(:*)
phi = val * sum/(val-1)
while phi > limit
list << 2
val = list.reduce(:*)
phi = val * sum/(val-1)
end
@kristenmills
kristenmills / problem76.rb
Created May 29, 2012 22:37
Project Euler 76
def solve
nums = 1.upto(100).to_a
goal = 100
ways = Array.new
ways << 1
goal.times do
ways << 0
end
nums.each do |num|
num.upto(goal) do |x|
@kristenmills
kristenmills / problem70.rb
Created May 30, 2012 00:39
Project Euler 70
def phi p1, p2
(p1-1)*(p2-1)
end
def find
min = [0,2]
lst = prime_sieve 5000
lst.keep_if{|x| x > 2000}
lst.reverse_each do |p1|
lst.reverse_each do |p2|
@kristenmills
kristenmills / problem31.rb
Created June 8, 2012 00:20
Project Euler 31
def solve
coins = [1, 2, 5, 10, 20, 50, 100, 200]
goal = 200
ways = Array.new
ways << 1
200.times do
ways << 0
end
coins.each do |coin|
coin.upto(goal) do |x|
@kristenmills
kristenmills / problem102.rb
Created September 3, 2012 00:42
Project Euler 102
def same_side p1, p2, p3, p4
cp1 = cross_product p4-p3, p1-p3
cp2 = cross_product p4-p3, p2-p3
dot_product(cp1, cp2) >=0
end
# is the point p in the triangle
def point_in_triangle p=Vector[0,0,0], a, b, c
same_side(p, a, b, c) && same_side(p, b, a, c) && same_side(p, c, a, b)
end
@kristenmills
kristenmills / split.rb
Created November 28, 2012 17:10
Splits a string into lines so that the number characters in a line is less than or equal to a specified length. Individual words will only be split if the word itself has more characters than can fit on a line.
def split string, length
split = Array.new
if string.length > length #if the string needs to be split
string_words = string.split(" ")
line = ""
string_words.each do |x|
if x.length > length #if the word needs to be split
#add the start of the word onto the first line (even if it has already started)
while line.length < length
line += x[0]
LOG1 = Math.log(GOLDEN_RATIO, 10)
LOG2 = Math.log(Math.sqrt(5), 10)
def problem104
n = 2
fn1 =1
fn2 = 1
while true
n += 1
fn = (fn1 + fn2) % 10**9
@kristenmills
kristenmills / isbn.rb
Created September 8, 2013 01:19
Ruby script for making the barcode labels
while true
print '>> '
STDOUT.flush
isbn = gets.chomp
`lpr barcodes/#{isbn}.png`
end
@kristenmills
kristenmills / isbn2lcc.rb
Last active December 22, 2015 20:09 — forked from kurtraschke/parsecallno.py
Some scripts to work with library of congress call numbers. Eventually to be used in some fashion in Alexandria.
#Connects to the library of congress database.
#Given a ISBN, it fetches the LCC.
require 'zoom'
require 'marc'
require 'stringio'
ZOOM::Connection.open('z3950.loc.gov', 7090) do |conn|
conn.database_name = 'VOYAGER'
conn.preferred_record_syntax = 'USMARC'