Last active
December 21, 2020 17:12
-
-
Save selfawaresoup/c1b58d847e90b41768e12993bcdd6578 to your computer and use it in GitHub Desktop.
Project Euler Problems
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
module Seventeen | |
WORDS = { | |
1000000 => "million", | |
1000 => "thousand", | |
100 => "hundred", | |
90 => "ninety", | |
80 => "eighty", | |
70 => "seventy", | |
60 => "sixty", | |
50 => "fifty", | |
40 => "forty", | |
30 => "thirty", | |
20 => "twenty", | |
19=>"nineteen", | |
18=>"eighteen", | |
17=>"seventeen", | |
16=>"sixteen", | |
15=>"fifteen", | |
14=>"fourteen", | |
13=>"thirteen", | |
12=>"twelve", | |
11 => "eleven", | |
10 => "ten", | |
9 => "nine", | |
8 => "eight", | |
7 => "seven", | |
6 => "six", | |
5 => "five", | |
4 => "four", | |
3 => "three", | |
2 => "two", | |
1 => "one" | |
} | |
def self.num_to_words(number, join: " ") | |
phrase = [] | |
remainder = number | |
WORDS.each do |value, word| | |
n = remainder / value | |
if n > 0 | |
if n > 20 | |
count = self.num_to_words(n) | |
else | |
count = WORDS[n] | |
end | |
if phrase.last == "hundred" | |
phrase.push("and") | |
end | |
unless n == 1 && value < 100 | |
phrase.push(count) | |
end | |
phrase.push(word) | |
end | |
remainder = remainder % value | |
end | |
phrase.join(join) | |
end | |
def self.range_sum(range) | |
range.map {|n| self.num_to_words(n, join: "") } | |
end | |
end | |
pp Seventeen::range_sum(1..1000).join('').length |
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
triangle_str = <<TRIANGLE | |
75 | |
95 64 | |
17 47 82 | |
18 35 87 10 | |
20 04 82 47 65 | |
19 01 23 75 03 34 | |
88 02 77 73 07 63 67 | |
99 65 04 28 06 16 70 92 | |
41 41 26 56 83 40 80 70 33 | |
41 48 72 33 47 32 37 16 94 29 | |
53 71 44 65 25 43 91 52 97 51 14 | |
70 11 33 28 77 73 17 78 39 68 17 57 | |
91 71 52 38 17 14 91 43 58 50 27 29 48 | |
63 66 04 68 89 53 67 30 73 16 69 87 40 31 | |
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 | |
TRIANGLE | |
triangle = triangle_str.split("\n").map do |l| | |
l.split(" ").map do |s| | |
s.to_i | |
end | |
end | |
(triangle.length - 2).downto(0).each do |l| | |
line = triangle[l] | |
line.each_with_index do |cell, c| | |
nextA = triangle[l + 1][c] | |
nextB = triangle[l + 1][c + 1] | |
triangle[l][c] = cell + [nextA, nextB].max | |
end | |
end | |
puts triangle[0][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
def leap_year(y) | |
if y % 400 == 0 | |
true | |
elsif y % 100 == 0 | |
false | |
elsif y % 4 == 0 | |
true | |
else | |
false | |
end | |
end | |
def month_length(m, y) | |
if m == 2 | |
if leap_year(y) | |
29 | |
else | |
28 | |
end | |
elsif [4,6,9,11].include?(m) | |
30 | |
else | |
31 | |
end | |
end | |
weekday = 0 | |
day = 1 | |
month = 1 | |
year = 1900 | |
ml = month_length(month, year) | |
sundays = 0 | |
loop do | |
puts "#{year}, #{month}, #{day}, #{weekday}" | |
if year >= 1901 && weekday == 6 && day == 1 | |
sundays += 1 | |
end | |
break if year == 2000 && month == 12 && day == 31 | |
if day == ml | |
day = 1 | |
if month == 12 | |
month = 1 | |
year +=1 | |
else | |
month +=1 | |
end | |
ml = month_length(month, year) | |
else | |
day += 1 | |
end | |
weekday = (weekday + 1) % 7 | |
end | |
pp sundays |
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
factorial = 1 | |
(2..100).each do |n| | |
factorial = factorial * n | |
end | |
digit_sum = 0 | |
factorial.to_s.split('').each do |c| | |
digit_sum += c.to_i | |
end | |
pp digit_sum |
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
# file from https://projecteuler.net/project/resources/p067_triangle.txt | |
triangle_str = File.open("p067_triangle.txt").read | |
triangle = triangle_str.split("\n").map do |l| | |
l.split(" ").map do |s| | |
s.to_i | |
end | |
end | |
(triangle.length - 2).downto(0).each do |l| | |
line = triangle[l] | |
line.each_with_index do |cell, c| | |
nextA = triangle[l + 1][c] | |
nextB = triangle[l + 1][c + 1] | |
triangle[l][c] = cell + [nextA, nextB].max | |
end | |
end | |
puts triangle[0][0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment