From 147c267455b75ca62fee099736cebe7928672059 Mon Sep 17 00:00:00 2001 From: Sampson R Crowley Date: Fri, 7 Oct 2016 20:18:00 -0600 Subject: [PATCH 1/6] completed calisthenics --- counting_game.rb | 0 ruby_calisthenics.rb | 117 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 counting_game.rb create mode 100644 ruby_calisthenics.rb diff --git a/counting_game.rb b/counting_game.rb new file mode 100644 index 0000000..e69de29 diff --git a/ruby_calisthenics.rb b/ruby_calisthenics.rb new file mode 100644 index 0000000..c430d5c --- /dev/null +++ b/ruby_calisthenics.rb @@ -0,0 +1,117 @@ +def power(base,exponent) + @total = 1 + (1..exponent).each{ + @total *= base + } + return @total +end + +def factorial(number) + @total = 1 + (1..number).each{|current_number| + @total *= current_number + } + return @total +end + +def uniques(input) + unique_output = [] + input.each{|item| + if !unique_output.include? item + unique_output.push(item) + end + } + return unique_output +end + +def combinations(first,second) + combination = [] + first.each{|item| + second.each{|second_item| + combination.push(item+second_item) + } + } + return combination +end + +def is_prime?(number) + (2...number).each do |divisor| + is_divisible = (number % divisor == 0) + if is_divisible + # divisor found + return false + end + divisor += 1 + end + # no divisors found + return true +end + +def overlap(rectangle_one, rectangle_two) + #add additional section to array to easily compare top, right, bottom, left + rectangle = [[[0,0],[0,0]],[[0,0],[0,0]]] + box_limits = { + 'rectangle_one' => { + top: 0, + right: 0, + bottom: 0, + left: 0 + }, + 'rectangle_two' => { + top: 0, + right: 0, + bottom: 0, + left: 0 + } + } + + if rectangle_one[0][0] < rectangle_one[1][0] + box_limits['rectangle_one'][:left] = rectangle_one[0][0] + box_limits['rectangle_one'][:right] = rectangle_one[1][0] + else + box_limits['rectangle_one'][:left] = rectangle_one[1][0] + box_limits['rectangle_one'][:right] = rectangle_one[0][0] + end + if rectangle_one[0][1] < rectangle_one[1][1] + box_limits['rectangle_one'][:bottom] = rectangle_one[0][1] + box_limits['rectangle_one'][:top] = rectangle_one[1][1] + else + box_limits['rectangle_one'][:bottom] = rectangle_one[1][1] + box_limits['rectangle_one'][:top] = rectangle_one[0][1] + end + if rectangle_two[0][0] < rectangle_two[1][0] + box_limits['rectangle_two'][:left] = rectangle_two[0][0] + box_limits['rectangle_two'][:right] = rectangle_two[1][0] + else + box_limits['rectangle_two'][:left] = rectangle_two[1][0] + box_limits['rectangle_two'][:right] = rectangle_two[0][0] + end + if rectangle_two[0][1] < rectangle_two[1][1] + box_limits['rectangle_two'][:bottom] = rectangle_two[0][1] + box_limits['rectangle_two'][:top] = rectangle_two[1][1] + else + box_limits['rectangle_two'][:bottom] = rectangle_two[1][1] + box_limits['rectangle_two'][:top] = rectangle_two[0][1] + end + + if (box_limits["rectangle_one"][:top] <= box_limits["rectangle_two"][:bottom] || box_limits["rectangle_one"][:bottom] >= box_limits["rectangle_two"][:top] || box_limits["rectangle_one"][:right] <= box_limits["rectangle_two"][:left] || box_limits["rectangle_one"][:left] >= box_limits["rectangle_two"][:right]) + return false + else + return true + end +end + +p "power of (3,4) is #{power(3,4)}" +p "factorial of (1,5) is #{factorial(5)}" +p "uniques([1,5,\"frog\", 2,1,3,\"frog\"]): #{uniques([1,5,"frog", 2,1,3,"frog"])}" +p "combinations([\"to\"], [\"test\", \"it\"]): #{combinations(["to"], ["test", "it"])}" +p "17 is prime? #{is_prime?(17)}" +p "do these overlap:" +p "overlap([ [0,0],[3,3] ], [ [1,1],[4,5] ]): #{overlap([ [0,0],[3,3] ], [ [1,1],[4,5] ])}" +p "overlap([ [3,3],[0,0] ], [ [4,5],[1,1] ]): #{overlap([ [3,3],[0,0] ], [ [4,5],[1,1] ])}" +p "overlap([ [3,0],[0,3] ], [ [4,1],[1,5] ]): #{overlap([ [3,0],[0,3] ], [ [4,1],[1,5] ])}" +p "overlap([ [0,3],[3,0] ], [ [1,5],[4,1] ]): #{overlap([ [0,3],[3,0] ], [ [1,5],[4,1] ])}" +p "overlap( [ [0,0],[1,4] ], [ [1,1],[3,2] ] ): #{overlap( [ [0,0],[1,4] ], [ [1,1],[3,2] ] )}" +p "overlap( [ [1,4],[0,0] ], [ [3,2],[1,1] ] ): #{overlap( [ [1,4],[0,0] ], [ [3,2],[1,1] ] )}" +p "overlap([[0,4],[1,0]],[[3,1],[1,2]]): #{overlap([[0,4],[1,0]],[[3,1],[1,2]])}" +p "overlap([[1,0],[0,4]],[[1,2],[3,1]]): #{overlap([[1,0],[0,4]],[[1,2],[3,1]])}" \ No newline at end of file From 83d1ed8a1205f0c55c707b2c51ce6e21dd19a47b Mon Sep 17 00:00:00 2001 From: Sampson R Crowley Date: Fri, 7 Oct 2016 21:01:36 -0600 Subject: [PATCH 2/6] translated pseudo to ruby --- counting_game.rb | 50 ++++++++++++++++++++++++++++++++++ ruby_calisthenics.rb | 65 +++++++++++++++++++------------------------- 2 files changed, 78 insertions(+), 37 deletions(-) diff --git a/counting_game.rb b/counting_game.rb index e69de29..d8ad208 100644 --- a/counting_game.rb +++ b/counting_game.rb @@ -0,0 +1,50 @@ +=begin +PROGRAM CounterGame + numfriends = 10 + player = 1 + count = 0 + direction = 1 + WHILE count < 100 + count = count + 1 + IF count % 7 = 0 THEN + direction = -1 * direction + END + IF count % 11 = 0 THEN + change = 2 + ELSE + change = 1 + END + player = player + change * direction + IF player < 1 OR play > 10 THEN + player =|(|player|- 10)| + END + IF count = 100 THEN + winner = player + END + END + return winner +END +=end +def counting_game (players,max) + player = 1 + count = 0 + direction = 1 + (1..max).each{|count| + if count % 7 + direction *= -1 + end + if count % 11 + change = 2 + else + change = 1 + end + player = player + (change * direction) + if !(1..10).includes? player + player = (player.abs - 10).abs + end + if count == 100 + return player + end + } +end +puts counting_game(3,100) \ No newline at end of file diff --git a/ruby_calisthenics.rb b/ruby_calisthenics.rb index c430d5c..217ae24 100644 --- a/ruby_calisthenics.rb +++ b/ruby_calisthenics.rb @@ -52,49 +52,40 @@ def overlap(rectangle_one, rectangle_two) rectangle = [[[0,0],[0,0]],[[0,0],[0,0]]] box_limits = { 'rectangle_one' => { - top: 0, - right: 0, - bottom: 0, - left: 0 + top: rectangle_one[1][1], + right: rectangle_one[1][0], + bottom: rectangle_one[0][1], + left: rectangle_one[0][0] }, 'rectangle_two' => { - top: 0, - right: 0, - bottom: 0, - left: 0 + top: rectangle_two[1][1], + right: rectangle_two[1][0], + bottom: rectangle_two[0][1], + left: rectangle_two[0][0] } } - if rectangle_one[0][0] < rectangle_one[1][0] - box_limits['rectangle_one'][:left] = rectangle_one[0][0] - box_limits['rectangle_one'][:right] = rectangle_one[1][0] - else - box_limits['rectangle_one'][:left] = rectangle_one[1][0] - box_limits['rectangle_one'][:right] = rectangle_one[0][0] - end - if rectangle_one[0][1] < rectangle_one[1][1] - box_limits['rectangle_one'][:bottom] = rectangle_one[0][1] - box_limits['rectangle_one'][:top] = rectangle_one[1][1] - else - box_limits['rectangle_one'][:bottom] = rectangle_one[1][1] - box_limits['rectangle_one'][:top] = rectangle_one[0][1] - end - if rectangle_two[0][0] < rectangle_two[1][0] - box_limits['rectangle_two'][:left] = rectangle_two[0][0] - box_limits['rectangle_two'][:right] = rectangle_two[1][0] - else - box_limits['rectangle_two'][:left] = rectangle_two[1][0] - box_limits['rectangle_two'][:right] = rectangle_two[0][0] - end - if rectangle_two[0][1] < rectangle_two[1][1] - box_limits['rectangle_two'][:bottom] = rectangle_two[0][1] - box_limits['rectangle_two'][:top] = rectangle_two[1][1] - else - box_limits['rectangle_two'][:bottom] = rectangle_two[1][1] - box_limits['rectangle_two'][:top] = rectangle_two[0][1] - end + if rectangle_one[0][0] > rectangle_one[1][0] + box_limits['rectangle_one'][:left] = rectangle_one[1][0] + box_limits['rectangle_one'][:right] = rectangle_one[0][0] + end + if rectangle_one[0][1] > rectangle_one[1][1] + box_limits['rectangle_one'][:bottom] = rectangle_one[1][1] + box_limits['rectangle_one'][:top] = rectangle_one[0][1] + end + if rectangle_two[0][0] > rectangle_two[1][0] + box_limits['rectangle_two'][:left] = rectangle_two[1][0] + box_limits['rectangle_two'][:right] = rectangle_two[0][0] + end + if rectangle_two[0][1] > rectangle_two[1][1] + box_limits['rectangle_two'][:bottom] = rectangle_two[1][1] + box_limits['rectangle_two'][:top] = rectangle_two[0][1] + end - if (box_limits["rectangle_one"][:top] <= box_limits["rectangle_two"][:bottom] || box_limits["rectangle_one"][:bottom] >= box_limits["rectangle_two"][:top] || box_limits["rectangle_one"][:right] <= box_limits["rectangle_two"][:left] || box_limits["rectangle_one"][:left] >= box_limits["rectangle_two"][:right]) + if (box_limits["rectangle_one"][:top] <= box_limits["rectangle_two"][:bottom] || + box_limits["rectangle_one"][:bottom] >= box_limits["rectangle_two"][:top] || + box_limits["rectangle_one"][:right] <= box_limits["rectangle_two"][:left] || + box_limits["rectangle_one"][:left] >= box_limits["rectangle_two"][:right]) return false else return true From 7f55d7a72c0c70619f03c2fa340b1115ca4f1820 Mon Sep 17 00:00:00 2001 From: Sampson R Crowley Date: Fri, 7 Oct 2016 21:36:22 -0600 Subject: [PATCH 3/6] fixed and tested final --- counting_game.rb | 45 ++++++++++----------------------------------- 1 file changed, 10 insertions(+), 35 deletions(-) diff --git a/counting_game.rb b/counting_game.rb index d8ad208..fcbc4b8 100644 --- a/counting_game.rb +++ b/counting_game.rb @@ -1,50 +1,25 @@ -=begin -PROGRAM CounterGame - numfriends = 10 - player = 1 - count = 0 - direction = 1 - WHILE count < 100 - count = count + 1 - IF count % 7 = 0 THEN - direction = -1 * direction - END - IF count % 11 = 0 THEN - change = 2 - ELSE - change = 1 - END - player = player + change * direction - IF player < 1 OR play > 10 THEN - player =|(|player|- 10)| - END - IF count = 100 THEN - winner = player - END - END - return winner -END -=end + def counting_game (players,max) player = 1 count = 0 direction = 1 (1..max).each{|count| - if count % 7 + if (count % 7 == 0) direction *= -1 end - if count % 11 + if count % 11 == 0 change = 2 else change = 1 end player = player + (change * direction) - if !(1..10).includes? player - player = (player.abs - 10).abs - end - if count == 100 - return player + if !player.between?(1,players) + player = (player.abs - players).abs end + #puts "#{player} player, #{count} count, #{direction} direction, #{count % 7 == 0} 7, #{change} change, #{count % 11 == 0} 11" } + return player end -puts counting_game(3,100) \ No newline at end of file + +puts counting_game(3,100) +puts counting_game(60,100) From 2783e0c1059acf2f77e308c7c4827eddeb70fa4e Mon Sep 17 00:00:00 2001 From: Sampson R Crowley Date: Fri, 7 Oct 2016 21:39:26 -0600 Subject: [PATCH 4/6] add project euler file --- projecteuler_problems.rb | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 projecteuler_problems.rb diff --git a/projecteuler_problems.rb b/projecteuler_problems.rb new file mode 100644 index 0000000..369885a --- /dev/null +++ b/projecteuler_problems.rb @@ -0,0 +1,63 @@ + +def three_and_five(range) + @total = 0 + range.each do |number| + if (number % 3 == 0 && number % 5 == 0) + @total += number + end + end + return @total +end + +def fibonacci_sum(max) + @total = 0 + @first = 0 + @second = 1 + while @second < max + @next = @first + @second + @first = @second + @second = @next + @total += @next + end + return @total +end + +def largest_prime(number) + @largest = number + (2...@largest).each do |i| + is_divisible = ((@largest % i) == 0) + if is_divisible + # divisor found; stop and return new range! + return @largest = largest_prime(@largest / i) + end + i += 1 + end + # no divisors found + return @largest +end + +def largest_palindrome(number_of_digits,symetrical=true) + @largest_number = "9" + @smallest_number = "1" + if symetrical + (number_of_digits-1).times do + @largest_number += "9" + @smallest_number += "0" + end + end + @smallest_number = @smallest_number.to_i + @largest_number = @largest_number.to_i + @largest_number.downto(@smallest_number){|number| + number.downto(@smallest_number){|multiple| + product = number * multiple + if product.to_s == product.to_s.reverse + return product + end + } + } +end + +puts three_and_five(1..1000) +puts fibonacci_sum(4000000) +puts largest_prime(600851475143) +puts largest_palindrome(3) \ No newline at end of file From 69c8775341d08734d030ec9517c5cecae479c75e Mon Sep 17 00:00:00 2001 From: Sampson R Crowley Date: Fri, 7 Oct 2016 21:40:26 -0600 Subject: [PATCH 5/6] renaming of files --- projecteuler_problems.rb => project_euler_problems.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename projecteuler_problems.rb => project_euler_problems.rb (100%) diff --git a/projecteuler_problems.rb b/project_euler_problems.rb similarity index 100% rename from projecteuler_problems.rb rename to project_euler_problems.rb From e0cccc74ae67b5e2f4798100b21f5182a743f44d Mon Sep 17 00:00:00 2001 From: Sampson R Crowley Date: Thu, 13 Oct 2016 16:43:47 -0600 Subject: [PATCH 6/6] final commit --- counting_game.rb | 24 ++++--- project_euler_problems.rb | 128 +++++++++++++++++++++++++++++++++++++- 2 files changed, 141 insertions(+), 11 deletions(-) diff --git a/counting_game.rb b/counting_game.rb index fcbc4b8..4728467 100644 --- a/counting_game.rb +++ b/counting_game.rb @@ -1,25 +1,29 @@ def counting_game (players,max) - player = 1 - count = 0 + player = 0 direction = 1 (1..max).each{|count| - if (count % 7 == 0) - direction *= -1 - end - if count % 11 == 0 - change = 2 + if !(count == 1) + if ((count-1).abs % 7 == 0) + direction *= -1 + end + if (0 == (count-1) % 11 ) + change = 2 + else + change = 1 + end else - change = 1 + direction = 1 + change = 1 end player = player + (change * direction) if !player.between?(1,players) player = (player.abs - players).abs end - #puts "#{player} player, #{count} count, #{direction} direction, #{count % 7 == 0} 7, #{change} change, #{count % 11 == 0} 11" + #puts "player: #{player}, count: #{count}, direction: #{direction}, divisible by 7 #{(count-1) % 7 == 0}, change: #{change}, divisible by 11: #{(count-1) % 11 == 0}" } return player end -puts counting_game(3,100) +puts counting_game(10,100) puts counting_game(60,100) diff --git a/project_euler_problems.rb b/project_euler_problems.rb index 369885a..5b37b51 100644 --- a/project_euler_problems.rb +++ b/project_euler_problems.rb @@ -57,7 +57,133 @@ def largest_palindrome(number_of_digits,symetrical=true) } end +def smallest_multiple(low,high) + divisible = false + number = low * high + while !divisible + divisible = true + number = number + high + (low..high).each{|i| + if !(number % i == 0) + divisible = false + break + end + } + end + return number +end + +def sum_square_difference(range) + sum = 0 + square = 0 + (range).each{|number| + sum += number + square += number**2 + } + difference = (sum**2 - square).abs +end + + +def nth_prime(target, n = false) + if !n + n = target**2 + end + nums = [nil, nil, *2..n] + (2..Math.sqrt(n)).each do |i| + (i**2..n).step(i){|m| nums[m] = nil} if nums[i] + end + primes = nums.compact + if primes.length < target + return nth_prime(target, n**2) + end + return primes[target-1] +end + +def largest_product_in_a_series(number_string,step) + @record = 0 + @position = 0 + numbers = number_string.split("") + num_list = [] + numbers.each{|number| num_list.push(number.to_i)} + iterate = num_list.length-step + iterate.times do |i| + @sum = 1 + (i..i+step-1).step(1){|m| + @sum *= num_list[m] + } + if @sum > @record + @position = i + @record = @sum + end + end + return "position: #{@position}, total: #{@record}" +end + +def summation_of_primes(max) + n = max + nums = [nil, nil, *2..n] + (2..Math.sqrt(n)).each do |i| + (i**2..n).step(i){|m| nums[m] = nil} if nums[i] + end + primes = nums.compact + return primes.inject(:+) +end + +def reverse(input_array) + while input_array.length != 0 + second.push(input_array[-1]) + input_array.pop + end + return second +end + +def light_switches + @nums = [] + (1..100).each{ + @nums.push("off") + } + (1..100).each{|trip| + (trip-1..99).step(trip){|m| + if @nums[m] == "off" + @nums[m] = "on" + else + @nums[m] = "off" + end + } + } + return @nums +end + +def stock_picker(prices) + @largest = 0 + @pair = [0,0] + i=0 + while i < prices.length-1 + n = i+1 + while n < prices.length + difference = prices[n] - prices[i] + if difference > @largest + @largest = difference + @pair = [i,n] + end + n += 1 + end + i += 1 + end + return @pair +end + + + puts three_and_five(1..1000) puts fibonacci_sum(4000000) puts largest_prime(600851475143) -puts largest_palindrome(3) \ No newline at end of file +puts largest_palindrome(3) +puts smallest_multiple(1,10) +puts sum_square_difference(1..100) +puts nth_prime(6) +puts nth_prime(100) +puts largest_product_in_a_series("09458394323523452345234523452345234523452345234523459303948",13) +puts summation_of_primes(30) +p light_switches +p stock_picker([44,10,66,73,12,12,54,23,67,87,1,1000]) \ No newline at end of file