Skip to content

Instantly share code, notes, and snippets.

@elreimundo
elreimundo / triangle_validity
Last active December 19, 2015 13:18
Am I cheating? When I taught Geometry, I used to teach my kids that this was the only requirement for a (planar) triangle. I feel like I should be teaching the machine to check for isosceles, equilateral, etc., but if all we want is to determine if a triangle is valid...
def valid_triangle?(a, b, c)
(a + b > c) && (a + c > b) && (b + c > a)
end
@elreimundo
elreimundo / times_table
Last active December 19, 2015 14:09
So I thought this would show a (not-very-well-spaced) times table with individual values separated by a space and each row on a new line. What am I missing?
def times_table(rows)
bigarray = []
1.upto(rows) do |x|
newarrayrow=[]
1.upto(rows) do |y|
newarrayrow << x*y
end
bigarray << newarrayrow.join(' ')
end
@elreimundo
elreimundo / englishnumber.rb
Created July 17, 2013 15:04
Not sure how elegant this is, but here is my number-to-its-English-word-counterpart translator
def numwords any_number
bignumwords any_number, []
end
def bignumwords number, big_array
place_names = {1 => 'thousand',
2 => 'million',
3 => 'billion',
4 => 'trillion'}
comma_count = Math.log(number,10).floor / 3
@elreimundo
elreimundo / fiftysixpuzzle.rb
Created July 17, 2013 15:08
A friend posted a math puzzle: "Find the smallest integer that ends in 56, is divisible by 56, and whose digits add to 56." Here's the Ruby script I wrote to solve the puzzle.
def digitsum num
currentsum = 0
(Math.log(num,10).ceil).downto(0) do |digit|
currentsum = currentsum + num/(10**digit)
num = num%(10**digit)
end
currentsum
end
@elreimundo
elreimundo / countofmultiples.rb
Created July 17, 2013 15:19
Project Euler problem #1 : "Find the sum of all of the whole numbers less than 1000 that are multiples of either 3 or 5." I thought that problem was too quick, so I generalized it to "Find the sum of all of the whole numbers less than n that are multiples of either x or z," then ran n=1000,x=3,z=5.
def countofmultiples (num,firstfactor,secondfactor)
count = 0
1.upto(num-1) do |check|
if check % firstfactor == 0 || check % secondfactor == 0
count += check
end
end
count
end
@elreimundo
elreimundo / sumofprimes.rb
Last active December 20, 2015 00:59
Here's a pretty simple prime number generator I used for a problem in ProjectEuler (specifically, calculate the sum of every prime below 2,000,000, although this can be generalized to sum every prime below any integer)
def sumofprimesbelow number
prime_array = [3,5]
current_num = 7
while current_num < number
prime_array.each do |factor|
if current_num % factor == 0
break
elsif factor > Math.sqrt(current_num)
prime_array << current_num
break
@elreimundo
elreimundo / largestproduct.rb
Created July 20, 2013 20:34
This is the main method for a ProjectEuler problem that provides a grid of numbers (copied and pasted as a string) and wants to find the maximum product of any four consecutive elements horizontally, vertically, or diagonally. I probably could have written each of the if/then statements in a single line, but executing two lines of code actually …
def largestproduct(nums_as_a_string)
currentmax = 0
arry = nums_as_a_string.split("\n")
arry.map! { |row_as_a_string| row_as_a_string.split.map! { |digit| digit.to_i } }
0.upto( (arry.length - 1).to_i ) do |row|
0.upto( (arry[row].length - 1).to_i ) do |col|
if col <= (arry[row].length - 4)
rowprod = arry[row][col]*arry[row][col+1]*arry[row][col+2]*arry[row][col+3]
currentmax = [currentmax,rowprod].max
end
@elreimundo
elreimundo / fibonaccicheck.rb
Last active December 20, 2015 01:08
Determines if a number is a Fibonacci number by a) determining element in the Fibonacci sequence it would be, based on its size, and b) comparing it to that actual element in the Fibonacci sequence. Couldn't have done this without a recursive formula courtesy of the late Edsgar Dijkstra, http://www.cs.utexas.edu/users/EWD/ewd06xx/EWD654.PDF foun…
def is_fibonacci?(num)
phi = (1 + 5.0**0.5)/2
which_fib = ((Math.log(num)+Math.log(5.0**0.5)) / Math.log(phi)).round
num == fibonacci(which_fib)
end
def fibonacci(n)
if n==0
0
@elreimundo
elreimundo / trianglenumdivisors.rb
Created July 20, 2013 21:22
I wrote this to solve another Project Euler problem (the number of divisors for a given triangular number). To find the number of divisors that a number has, the quickest way is to take the prime factorization of the number. Next, add one to each of the exponents of the prime factors, and find the product of these new exponents. There's your num…
whichtrinum = 1
while true
trinum = (whichtrinum * (whichtrinum + 1)) / 2
numtofactor = trinum
factors = []
factor = 2
while numtofactor != 1
if numtofactor % factor == 0
factors << factor
numtofactor = numtofactor / factor
@elreimundo
elreimundo / roman_to_integer.rb
Last active December 20, 2015 01:49
Here's code that will convert any (properly ordered) roman numeral to a number. It uses a hash, which is something I'm pretty excited about since I don't feel like I totally understand hashes.
def roman_to_integer string
string.upcase!
raise ArgumentError if /[MDCLXVI]*/.match(string).to_s != string
letter_values = {'M' => 1000,
'D' => 500,
'C' => 100,
'L' => 50,
'X' => 10,
'V' => 5,
'I' => 1}