From 32c6efeb958ec617d18f9b3baeb99db80dead3f1 Mon Sep 17 00:00:00 2001 From: petahz Date: Mon, 17 Feb 2014 22:43:40 -0500 Subject: [PATCH 1/7] Update problem03.rb A simple solution. --- problem03.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problem03.rb b/problem03.rb index e69de29..09d2c12 100644 --- a/problem03.rb +++ b/problem03.rb @@ -0,0 +1,16 @@ +def larg_prim num + prime_factors = [] + testNum = 2 + while testNum <= num + if num % testNum == 0 + prime_factors << testNum + num /= testNum + else + testNum +=1 + end + end + prime_factors.sort! + return prime_factors.last +end + +puts larg_prim(600851475143) From 3bf4070d3bdda46c4014567d99af01d971400941 Mon Sep 17 00:00:00 2001 From: petahz Date: Wed, 19 Feb 2014 11:37:18 -0500 Subject: [PATCH 2/7] Update problem05.rb --- problem05.rb | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/problem05.rb b/problem05.rb index e69de29..35a6ae2 100644 --- a/problem05.rb +++ b/problem05.rb @@ -0,0 +1,48 @@ +def lcm max_num + (1..max_num).inject(:lcm) +end + +puts lcm(20) + + +# Code below where I tried to figure out the LCM without the Ruby lcm method. + +def lcm max_num + # Get a list of all prime factors. + prime_arr = [] + (2..max_num).each do |x| + if is_prime? x + prime_arr << x + end + end + + # Find the LCM + factors_arr = prime_arr + prime_arr.take_while{ |i| i <= Math.sqrt(max_num).each do |x| + counter = 2 + while x <= Math.sqrt(max_num) + if x ** counter <= max_num + factors_arr.push(x ** counter) + index = factors_arr.find_index(x ** (counter-1)) + factors_arr.delete_at(index) + else + break + end + counter += 1 + end + end + + return factors_arr.compact.inject(:*) +end + +def is_prime? num + testNum = 2 + while testNum < num + if num % testNum == 0 + return false + else + testNum += 1 + end + end + return true +end From bf6f65104acbe8084bdd52aaa3bc97bafab30e66 Mon Sep 17 00:00:00 2001 From: petahz Date: Wed, 19 Feb 2014 12:47:33 -0500 Subject: [PATCH 3/7] Update problem08.rb --- problem08.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/problem08.rb b/problem08.rb index e69de29..9df94c6 100644 --- a/problem08.rb +++ b/problem08.rb @@ -0,0 +1,13 @@ +def greatest5 num + digits = num.to_s.chars.map(&:to_i) + i = 0 + prods_array = [] + while (i+4) < digits.length + prods_array << (digits[i..i+4].inject(:*)) + i += 1 + end + + return prods_array.sort!.last +end + +puts greatest5 (7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450) From 92d1479cad33d0d509d5bc2a810a535305877d88 Mon Sep 17 00:00:00 2001 From: petahz Date: Thu, 20 Feb 2014 16:14:42 -0500 Subject: [PATCH 4/7] Update problem05.rb --- problem05.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problem05.rb b/problem05.rb index 35a6ae2..5576458 100644 --- a/problem05.rb +++ b/problem05.rb @@ -18,7 +18,7 @@ def lcm max_num # Find the LCM factors_arr = prime_arr - prime_arr.take_while{ |i| i <= Math.sqrt(max_num).each do |x| + prime_arr.each do |x| counter = 2 while x <= Math.sqrt(max_num) if x ** counter <= max_num From b6be3adf25cda71b4ae4afdcf146f7ab5f569cd7 Mon Sep 17 00:00:00 2001 From: petahz Date: Tue, 25 Feb 2014 08:29:50 -0500 Subject: [PATCH 5/7] Update problem03.rb --- problem03.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/problem03.rb b/problem03.rb index 09d2c12..9e3bc62 100644 --- a/problem03.rb +++ b/problem03.rb @@ -1,16 +1,15 @@ def larg_prim num - prime_factors = [] + prime_factor = 0 testNum = 2 while testNum <= num if num % testNum == 0 - prime_factors << testNum + prime_factor = testNum num /= testNum else testNum +=1 end end - prime_factors.sort! - return prime_factors.last + return prime_factor end puts larg_prim(600851475143) From b9c08f9623738e1c18537cd6c94d78efa5325a92 Mon Sep 17 00:00:00 2001 From: petahz Date: Tue, 25 Feb 2014 08:30:19 -0500 Subject: [PATCH 6/7] Update problem08.rb --- problem08.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/problem08.rb b/problem08.rb index 9df94c6..ac07d99 100644 --- a/problem08.rb +++ b/problem08.rb @@ -1,13 +1,16 @@ -def greatest5 num +def greatest5(num) digits = num.to_s.chars.map(&:to_i) i = 0 - prods_array = [] + greatest_prod = 0 while (i+4) < digits.length - prods_array << (digits[i..i+4].inject(:*)) + product = (digits[i..i+4].inject(:*)) + if product > greatest_prod + greatest_prod = product + end i += 1 end - return prods_array.sort!.last + return greatest_prod end puts greatest5 (7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450) From d729706adf0e52384555ecd783c6439796609f21 Mon Sep 17 00:00:00 2001 From: petahz Date: Tue, 4 Mar 2014 16:54:47 -0500 Subject: [PATCH 7/7] Update problem05.rb --- problem05.rb | 9 --------- 1 file changed, 9 deletions(-) diff --git a/problem05.rb b/problem05.rb index 5576458..ba4d113 100644 --- a/problem05.rb +++ b/problem05.rb @@ -1,12 +1,3 @@ -def lcm max_num - (1..max_num).inject(:lcm) -end - -puts lcm(20) - - -# Code below where I tried to figure out the LCM without the Ruby lcm method. - def lcm max_num # Get a list of all prime factors. prime_arr = []