From a14ea9474e4d190b6458a1b5f30947764f0675a5 Mon Sep 17 00:00:00 2001 From: awong05 Date: Tue, 18 Feb 2014 18:09:43 -0500 Subject: [PATCH 1/5] i "Problem #3 Solved" --- problem03.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problem03.rb b/problem03.rb index e69de29..928f181 100644 --- a/problem03.rb +++ b/problem03.rb @@ -0,0 +1,26 @@ +$factor_array = [] + +def prime(n) + for i in 2..n-1 + if n % i == 0 + return false + end + end + return true +end + +def factor(n) + counter = 2 + until n % counter == 0 && prime(counter) + counter += 1 + end + $factor_array << counter + if n != counter + factor(n/counter) + else + puts $factor_array.pop + Process.exit(0) + end +end + +factor(600851475143) \ No newline at end of file From 643135e0cb828a4a8a0022cf756b6627a6556e36 Mon Sep 17 00:00:00 2001 From: awong05 Date: Tue, 18 Feb 2014 19:43:25 -0500 Subject: [PATCH 2/5] "Problem #5 Solved" --- problem05.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/problem05.rb b/problem05.rb index e69de29..2280210 100644 --- a/problem05.rb +++ b/problem05.rb @@ -0,0 +1 @@ +puts (1..20).inject(:lcm) \ No newline at end of file From 84fcb5d054896390071ad6479f3ba1db937ce6b4 Mon Sep 17 00:00:00 2001 From: awong05 Date: Tue, 18 Feb 2014 20:26:51 -0500 Subject: [PATCH 3/5] Cleaned first method --- problem03.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/problem03.rb b/problem03.rb index 928f181..30fa2e9 100644 --- a/problem03.rb +++ b/problem03.rb @@ -1,11 +1,7 @@ $factor_array = [] def prime(n) - for i in 2..n-1 - if n % i == 0 - return false - end - end + (2..n-1).each {|x| return false if n % x == 0} return true end From 403cd3483925e8e33987b7ee8b51c955bd1f9e60 Mon Sep 17 00:00:00 2001 From: awong05 Date: Wed, 19 Feb 2014 00:40:48 -0500 Subject: [PATCH 4/5] Problem #8: Solved --- problem08.rb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/problem08.rb b/problem08.rb index e69de29..36a9b0c 100644 --- a/problem08.rb +++ b/problem08.rb @@ -0,0 +1,38 @@ +$final_product = 0 + +def multiply(array) + counter = 0 + x = 1 + while counter < array.length + x = x*array[counter].to_i + counter += 1 + end + return x +end + +n = 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450 +n_array = n.to_s.split('') +e = n_array.to_enum +product_array = [] +5.times do + product_array << e.next +end + +set_product = multiply(product_array) + +next_int = e.next + +begin + until next_int == nil + product_array.shift + product_array << next_int + set_product = multiply(product_array) + if set_product > $final_product + $final_product = set_product + end + next_int = e.next + end +rescue StopIteration + puts $final_product + Process.exit(0) +end \ No newline at end of file From 011e51588ac1d4b1244861f6fdc4927709e87626 Mon Sep 17 00:00:00 2001 From: awong05 Date: Thu, 20 Feb 2014 00:47:04 -0500 Subject: [PATCH 5/5] Cleaned code to Problem #8 --- problem08.rb | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/problem08.rb b/problem08.rb index 36a9b0c..991070f 100644 --- a/problem08.rb +++ b/problem08.rb @@ -1,38 +1,28 @@ $final_product = 0 def multiply(array) - counter = 0 - x = 1 - while counter < array.length - x = x*array[counter].to_i - counter += 1 - end - return x + i = 1 + array.each { |x| i = i*x.to_i } + return i end n = 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450 n_array = n.to_s.split('') e = n_array.to_enum product_array = [] -5.times do - product_array << e.next -end set_product = multiply(product_array) -next_int = e.next - -begin - until next_int == nil +n_array.each do |x| + if product_array.length < 5 + product_array << e.next + else product_array.shift - product_array << next_int + product_array << e.next set_product = multiply(product_array) if set_product > $final_product $final_product = set_product end - next_int = e.next - end -rescue StopIteration - puts $final_product - Process.exit(0) -end \ No newline at end of file + end +end +puts $final_product \ No newline at end of file