From b0ceef1c42c3f117e8e06feae4c9f5676589987d Mon Sep 17 00:00:00 2001 From: toppy42 Date: Sat, 28 Sep 2013 15:46:28 -0400 Subject: [PATCH 1/2] problem 3 solution --- problem03.rb | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/problem03.rb b/problem03.rb index e69de29..ed92186 100644 --- a/problem03.rb +++ b/problem03.rb @@ -0,0 +1,94 @@ +require 'benchmark' + +# generate primes up to a specific number +def prime_gen(num) + prime_array = [2] + 3.upto(num) do |x| + prime = true + prime_array.each do |member| + prime &= (0 != x % member) + if not prime + break + end + end + if prime == true + prime_array << x + end + end + prime_array +end + + +#puts prime_gen(100) + +# generate primes using a sieve +def prime_gen_reduct(num) + full_array = [] + primes = [] + 2.upto(num) {|x| full_array << x} + while not full_array.empty? + recent = full_array[0] + primes << recent + full_array.reject! {|x| x%recent==0} + end + primes +end + +# n = 100000 +# Benchmark.bm do |x| +# x.report {prime_gen(n)} +# x.report {prime_gen_reduct(n)} #faster +# end + +# take an array of primes and return list of additional up to num +# assumes it will only be passed a full array of primes +# also uses a sieve +def iter_prime_gen(primes_ary, num) + full_array=[] + new_primes = [] + if primes_ary[-1] >= num + return primes_ary + end + primes_ary[-1].upto(num) {|x| full_array << x} + until full_array.empty? + recent = full_array[0] + new_primes << recent + full_array.reject! {|x| x%recent==0} + end + new_primes +end + +global_prime = [2] + +#puts iter_prime_gen(global_prime,10) + + def largest_prime(num) + primes_array = [2] + array_copy = primes_array.dup + found = false + + while found==false + # add response from iter_prime_gen to primes arry and array_copy + new_primes = iter_prime_gen(primes_array, primes_array[-1] * 10) + primes_array += new_primes + array_copy += new_primes + + + until array_copy.empty? + first = array_copy[0] + if first == num + found = true + break + elsif num%first == 0 + num /= first + else + array_copy.shift + end + end + end + num + end + + puts largest_prime(12345) + puts largest_prime(600851475143) + From 62194527574482a75ff8b47ec933618cb2c89d86 Mon Sep 17 00:00:00 2001 From: toppy42 Date: Thu, 3 Oct 2013 17:56:04 -0400 Subject: [PATCH 2/2] Problem answers for 5 and 8 --- problem05.rb | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++ problem08.rb | 43 ++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/problem05.rb b/problem05.rb index e69de29..c0e21d5 100644 --- a/problem05.rb +++ b/problem05.rb @@ -0,0 +1,87 @@ +# take an array of primes and return list of additional up to num +# assumes it will only be passed a full array of primes +# also uses a sieve +def iter_prime_gen(primes_ary, num) + full_array=[] + new_primes = [] + if primes_ary[-1] >= num + return primes_ary + end + primes_ary[-1].upto(num) {|x| full_array << x} + until full_array.empty? + recent = full_array[0] + new_primes << recent + full_array.reject! {|x| x%recent==0} + end + new_primes +end + + +max_prime = 20 +$prime_list = iter_prime_gen([2],max_prime) + + +def get_factors(num) + fact_list = [] + temp_num = num + until temp_num == 1 + $prime_list.inject(temp_num) do |reduct, item| + if temp_num%item == 0 + fact_list << item + temp_num /= item + break + elsif item == $prime_list[-1] + $prime_list = iter_prime_gen($prime_list, temp_num) + end + end + end + fact_list +end + +#puts get_factors(30) +#puts get_factors(198) + +# Return all factors necessary to create each number +# Eg, [2,2,2,2] and [2,2,3,5] would return [2,2,2,2,3,5] +def reduce_factors(fact_list1, fact_list2) + if (not fact_list1.is_a?(Array) or not fact_list2.is_a?(Array)) + exit + end + ret_list = [] + count1 = 0 + count2 = 0 + + until fact_list1.empty? + length1 = fact_list1.length() + length2 = fact_list2.length() + reject1 = fact_list1[0] + count1 = length1 - fact_list1.reject! {|i| i == reject1}.length() + #fact_list2 = fact_list2.reject {|i| i == reject1} + #count2 = length2 - fact_list2.length() + count2 = length2 - fact_list2.reject! {|i| i == reject1}. length() + max = count1 > count2 ? count1:count2 + max.times do ret_list << reject1 end + end + if not fact_list2.empty? + ret_list.concat(fact_list2) + end + ret_list.sort() +end + +#puts reduce_factors(get_factors(20),get_factors(30)) +#puts reduce_factors(get_factors(20),get_factors(300)) + +# find the least common multiple in a range of numbers +def least_common_mult(range_start, range_end) + ret_list = [] + range_start.upto(range_end) do |num| + ret_list = reduce_factors(ret_list, get_factors(num)) + end + ret_list.inject(1) do | num, item | + num *= item + num + end + +end + +puts least_common_mult(1, 20) diff --git a/problem08.rb b/problem08.rb index e69de29..5680a81 100644 --- a/problem08.rb +++ b/problem08.rb @@ -0,0 +1,43 @@ +num_string = +"73167176531330624919225119674426574742355349194934 +96983520312774506326239578318016984801869478851843 +85861560789112949495459501737958331952853208805511 +12540698747158523863050715693290963295227443043557 +66896648950445244523161731856403098711121722383113 +62229893423380308135336276614282806444486645238749 +30358907296290491560440772390713810515859307960866 +70172427121883998797908792274921901699720888093776 +65727333001053367881220235421809751254540594752243 +52584907711670556013604839586446706324415722155397 +53697817977846174064955149290862569321978468622482 +83972241375657056057490261407972968652414535100474 +82166370484403199890008895243450658541227588666881 +16427171479924442928230863465674813919123162824586 +17866458359124566529476545682848912883142607690042 +24219022671055626321111109370544217506941658960408 +07198403850962455444362981230987879927244284909188 +84580156166097919133875499200524063689912560717606 +05886116467109405077541002256983155200055935729725 +71636269561882670428252483600823257530420752963450" + +num_string.gsub!(/(\n|\t|\r)/, ' ').gsub!(/(\s)/,"") + +num_array = num_string.chars.to_a.map {|num| num.to_i} + +def big_5(number_ary) + ret_list = [] + start_index = 0 + end_index=4 + product = 1 + + while end_index < number_ary.length + start_index.upto(end_index) {|i| product*=number_ary[i] } + ret_list << product + product = 1 + start_index+=1 + end_index+=1 + end + ret_list.max +end + +puts big_5(num_array)