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
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 |
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
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 |
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
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| |
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
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| |
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
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| |
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
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 |
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
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] |
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
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 |
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
while true | |
print '>> ' | |
STDOUT.flush | |
isbn = gets.chomp | |
`lpr barcodes/#{isbn}.png` | |
end |
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
#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' |
OlderNewer