Skip to content

Instantly share code, notes, and snippets.

View johana-star's full-sized avatar
🕶️

Johana Star johana-star

🕶️
View GitHub Profile
@johana-star
johana-star / euler15.rb
Created May 29, 2011 07:48
Euler Project Problem #015
# Solution to Project Euler's fifteenth problem
# http://projecteuler.net/index.php?section=problems&id=15
# How many routes are there through a 2020 grid?
def generate_Pascals_triangle(depth)
triangle = [[1], [1,1]]
(2..depth).each do |d|
# generate row #
triangle[d] = []
next_row = triangle[d-1]
next_row.push 0; next_row.unshift 0
@johana-star
johana-star / euler17.rb
Created May 29, 2011 09:03
Euler Project Problem #017
# Solution to Project Euler's seventeenth problem
# http://projecteuler.net/index.php?section=problems&id=17
# If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
ninety_nine = 3+3+5+4+4+3+5+5+4 + 3+6+6+8+8+7+7+9+8+8 + 6*10+(3+3+5+4+4+3+5+5+4) + 6*10+(3+3+5+4+4+3+5+5+4) + 5*10+(3+3+5+4+4+3+5+5+4) + 5*10+(3+3+5+4+4+3+5+5+4) + 5*10+(3+3+5+4+4+3+5+5+4) + 7*10+(3+3+5+4+4+3+5+5+4) + 6*10+(3+3+5+4+4+3+5+5+4) + 6*10+(3+3+5+4+4+3+5+5+4)
one_thousand = ninety_nine + 100*10 + ninety_nine + 100*10 + ninety_nine + 100*12 + ninety_nine + 100*11 + ninety_nine + 100*11 + ninety_nine + 100*10 + ninety_nine + 100*12 + ninety_nine + 100*12 + ninety_nine + 100*11 + ninety_nine + 11 + 99*9*3 # this last product is the ands.
p one_thousand
#3 = one
#3 = two
#5 = three
#4 = four
@johana-star
johana-star / euler36.rb
Created May 29, 2011 18:04
Euler Project Problem #036
# Solution to Project Euler's thirty-sixth problem
# http://projecteuler.net/index.php?section=problems&id=17
# Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
def test_decimal_palindromicity(number)
bool = number.to_s == number.to_s.reverse
return bool
end
def test_binary_palindromicity(number)
@johana-star
johana-star / euler40.rb
Created May 29, 2011 18:36
Euler Project Problem #040
# Solution to Project Euler's fortieth problem
# http://projecteuler.net/index.php?section=problems&id=40
# Find the product of the first, tenth, hundredth, thousandth, ten-thousandth, hundred-thousandth, and millionth digit of the irrational fraction created by concatenating the positive integers.
count = 0
ceiling = 1000000
number = ""
until number.length > ceiling do
count += 1
number.concat count.to_s
@johana-star
johana-star / euler35.rb
Created May 29, 2011 22:44
Euler Project Problem #035
# Solution to Project Euler's thirty-fifth problem
# http://projecteuler.net/index.php?section=problems&id=35
# How many circular primes are there below one million?
# This program needs a substantial rewrite. All Euler Project problems should take less than one minute to run, this one runs in 39 minutes.
def generate_primes(ceiling)
primes, count = [1, 2], 1
until primes.last > ceiling do
numbers = (primes[count-1]**2+1..primes[count]**2).to_a
@johana-star
johana-star / euler34.rb
Created May 30, 2011 18:15
Euler Project Problem #034
# Solution to Project Euler's thirty-fourth problem
# http://projecteuler.net/index.php?section=problems&id=34
# Find the sum of all numbers which are equal to the sum of the factorial of their digits.
def fact(n)
(1..n).reduce(1, :*)
end
def seperate_digits(numbers)
array = []
numbers.each_with_index do |n, i|
array[i] = []
@johana-star
johana-star / euler11.rb
Created June 1, 2011 22:45
Euler Project Problem #011
# Solution to Project Euler's eleventh problem
# http://projecteuler.net/index.php?section=problems&id=11
# FWhat is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the grid?
grid = [[8, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 8], [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00], [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65], [52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91], [22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80], [24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50], [32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70], [67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21], [24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72], [21, 36, 23, 9, 75, 00, 76, 44, 20, 45, 35, 14, 0
@johana-star
johana-star / euler21.rb
Created June 2, 2011 23:06
Euler Project Problem #021
# Solution to Project Euler's twenty-first problem
# http://projecteuler.net/index.php?section=problems&id=21
# Find the sum of the amicable pairs below 10000.
def generate_sum_of_divisors(ceiling)
sums = Array.new(ceiling+1, 0)
(0..ceiling).each do |number|
(1..Math.sqrt(number)).each do |num|
if number % num == 0 then
sums[number] += num
if number/num != num && num != 1 then
@johana-star
johana-star / euler19.rb
Created June 4, 2011 03:50
Euler Project Problem #019
# Solution to Project Euler's nineteenth problem
# http://projecteuler.net/index.php?section=problems&id=19
# find number of Sundays on the first of the month between Jan 1901 and Dec 2000.
year = 1901
day_of_week = 2 # 0: Sunday, 6: Saturday, 1 Jan 1901 was a Tuesday
month = 0 # 0: January, # 11: December
months = [3, 0, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3]
number_of_sundays = 0
until year == 2000 and month == 0 do
day_of_week += months[month]
@johana-star
johana-star / euler45.rb
Created June 4, 2011 16:34
Euler Project Problem #045
# Solution to Project Euler's forty-fifth problem
# http://projecteuler.net/index.php?section=problems&id=45
# Find the second number which is a triangle number, pentagonal number, and hexagonal number.
def create_pentagonal_number(number)
pentagonal = (number*(3*number-1))/2
return pentagonal
end
def create_hexagonal_number(number)
hexagonal = number*(2*number-1)
return hexagonal