From 5776e764b9745552f3bff3d572538150b624b27f Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Wed, 13 May 2015 09:26:27 -0400 Subject: [PATCH] Add code from group --- solutions/solution-1.rb | 8 ++++++++ solutions/solution-15.rb | 38 ++++++++++++++++++++++++++++++++++++++ solutions/solution-2.rb | 12 ++++++++++++ solutions/solution-3.rb | 4 ++++ solutions/solution-4.rb | 20 ++++++++++++++++++++ solutions/solution-5.rb | 3 +++ solutions/solution-8.rb | 33 +++++++++++++++++++++++++++++++++ spec/problem-1.rb | 12 ++++++++++++ 8 files changed, 130 insertions(+) create mode 100644 solutions/solution-1.rb create mode 100644 solutions/solution-15.rb create mode 100644 solutions/solution-2.rb create mode 100644 solutions/solution-3.rb create mode 100644 solutions/solution-4.rb create mode 100644 solutions/solution-5.rb create mode 100644 solutions/solution-8.rb create mode 100644 spec/problem-1.rb diff --git a/solutions/solution-1.rb b/solutions/solution-1.rb new file mode 100644 index 0000000..9c92e02 --- /dev/null +++ b/solutions/solution-1.rb @@ -0,0 +1,8 @@ +def solution_1(max) + sum = 0 + (1...max).each do |number| + sum += number if number % 3 == 0|| number % 5 == 0 + end + + sum +end diff --git a/solutions/solution-15.rb b/solutions/solution-15.rb new file mode 100644 index 0000000..6c93734 --- /dev/null +++ b/solutions/solution-15.rb @@ -0,0 +1,38 @@ +class Solution15 + attr_reader :solution + def initialize(size) + @size = size + @grid = Array.new(size + 1){ Array.new(size + 1, 0) } + + @solution = traverse(0,0) + end + + def traverse(x, y) + paths = 0 + + if @size - x == 0 || @size - y == 0 + @grid[x][y] = 1 + return 1 + end + + if @grid[x][y] == 0 + paths += traverse(x + 1, y) if x < @size + paths += traverse(x, y + 1) if y < @size + + @grid[x][y] = paths + @grid[y][x] = paths + end + + @grid[x][y] + end + + def traverse2(x, y) + paths = 0 + paths += traverse(x + 1, y) if x < @size + paths += traverse(x, y + 1) if y < @size + + return 1 if x == @size && y == @size + + return paths + end +end diff --git a/solutions/solution-2.rb b/solutions/solution-2.rb new file mode 100644 index 0000000..3165b6c --- /dev/null +++ b/solutions/solution-2.rb @@ -0,0 +1,12 @@ +def solution_2(max) + sum = 0 + + i = j = 1 + + while(j <= max) + i, j = j, i + j + sum += i if i.even? + end + + sum +end diff --git a/solutions/solution-3.rb b/solutions/solution-3.rb new file mode 100644 index 0000000..1fe9510 --- /dev/null +++ b/solutions/solution-3.rb @@ -0,0 +1,4 @@ +require 'prime' +def solution_3(number) + Prime.prime_division(number).last[0] +end diff --git a/solutions/solution-4.rb b/solutions/solution-4.rb new file mode 100644 index 0000000..873a47a --- /dev/null +++ b/solutions/solution-4.rb @@ -0,0 +1,20 @@ +def solution_4(digits) + min = min(digits) + max = max(digits) + + (min..max) + .to_a + .map { |i| (min..max).to_a.map { |j| i * j } } + .flatten(1) + .select { |i| i.to_s == i.to_s.reverse } + .sort + .last +end + +def min(digits) + ("1" + "0" * (digits - 1)).to_i +end + +def max(digits) + ("1" + "0" * (digits)).to_i - 1 +end diff --git a/solutions/solution-5.rb b/solutions/solution-5.rb new file mode 100644 index 0000000..9a0abfd --- /dev/null +++ b/solutions/solution-5.rb @@ -0,0 +1,3 @@ +def solution_5(max) + (1..max).inject(1) { |lcm, n| lcm.lcm(n) } +end diff --git a/solutions/solution-8.rb b/solutions/solution-8.rb new file mode 100644 index 0000000..d0bf571 --- /dev/null +++ b/solutions/solution-8.rb @@ -0,0 +1,33 @@ +def solution_8(length) + number = "73167176531330624919225119674426574742355349194934 +96983520312774506326239578318016984801869478851843 +85861560789112949495459501737958331952853208805511 +12540698747158523863050715693290963295227443043557 +66896648950445244523161731856403098711121722383113 +62229893423380308135336276614282806444486645238749 +30358907296290491560440772390713810515859307960866 +70172427121883998797908792274921901699720888093776 +65727333001053367881220235421809751254540594752243 +52584907711670556013604839586446706324415722155397 +53697817977846174064955149290862569321978468622482 +83972241375657056057490261407972968652414535100474 +82166370484403199890008895243450658541227588666881 +16427171479924442928230863465674813919123162824586 +17866458359124566529476545682848912883142607690042 +24219022671055626321111109370544217506941658960408 +07198403850962455444362981230987879927244284909188 +84580156166097919133875499200524063689912560717606 +05886116467109405077541002256983155200055935729725 +71636269561882670428252483600823257530420752963450".gsub(/\n/, '') + greatest = 0 + + number.split(//).each_with_index do |value, i| + if i >= length + current = ((i - length + 1)..i).map { |i| number[i].to_i }.reduce(1, :*) + + greatest = current if current > greatest + end + end + + return greatest +end diff --git a/spec/problem-1.rb b/spec/problem-1.rb new file mode 100644 index 0000000..6f26f19 --- /dev/null +++ b/spec/problem-1.rb @@ -0,0 +1,12 @@ +require_relative 'spec_helper' +require_relative '../solutions/solution-1' + +describe 'Multiples of 3 and 5' do + it 'works for the given example' do + expect(solution_1(10)).to eq(23) + end + + it 'works for the known answer' do + expect(solution_1(1000)).to eq(233168) + end +end