I learned to program in Java and C# and in my free time I am using Ruby because it is fun. Unfortunately I have the feeling that I write Ruby code like I am making Java or C# code. I have learned to use regex instead of strings for comparison, using each instead of for-loops, keeping method names lowercase, and how to use code blocks (which are quite similar to lambda expressions in C#).
I would love to improve the Rubiness of my code and I hope you would be willing to give me one or more pointers on a piece of code I made. It answers Project Euler problem 27.
class Integer
def prime?
return false if self < 1
2.upto(Math.sqrt(self)) do |i|
return false if self % i == 0
end
true
end
end
def get_amount_of_primes_from_quadratic_formula(a,b)
primes = []
still_all_primes = true
n = 0
while still_all_primes
result = n**2 + a*n + b
if result.prime? then
primes << result
else
still_all_primes = false
end
n += 1
end
primes.size
end
def get_product_of_coefficients_that_produce_maximum_number_of_primes_for_consecutive_values()
max_product = 0
max_primes = 0
-999.upto(1000) do |a|
-999.upto(1000) do |b|
primes = get_amount_of_primes_from_quadratic_formula(a,b)
if primes > max_primes then
max_primes = primes
max_product = a*b
end
end
end
max_product
end
start = Time.now
answer = get_product_of_coefficients_that_produce_maximum_number_of_primes_for_consecutive_values()
puts "The answer is #{answer} and it took #{Time.now-start} seconds."
I think I can improve on the if-then statement and write it more concise, and also the two "upto-loops" where I first declare the variables max_primes
and max_product
can be written in a more Ruby-way I am sure.
I would be very grateful if you could let me know how to write more like Ruby!
Links that ask similar questions which I am reading in the meantime:
I learned to program in Java and C# and in my free time I am using Ruby because it is fun. Unfortunately I have the feeling that I write Ruby code like I am making Java or C# code. I have learned to use regex instead of strings for comparison, using each instead of for-loops, keeping method names lowercase, and how to use code blocks (which are quite similar to lambda expressions in C#).
I would love to improve the Rubiness of my code and I hope you would be willing to give me one or more pointers on a piece of code I made. It answers Project Euler problem 27.
class Integer
def prime?
return false if self < 1
2.upto(Math.sqrt(self)) do |i|
return false if self % i == 0
end
true
end
end
def get_amount_of_primes_from_quadratic_formula(a,b)
primes = []
still_all_primes = true
n = 0
while still_all_primes
result = n**2 + a*n + b
if result.prime? then
primes << result
else
still_all_primes = false
end
n += 1
end
primes.size
end
def get_product_of_coefficients_that_produce_maximum_number_of_primes_for_consecutive_values()
max_product = 0
max_primes = 0
-999.upto(1000) do |a|
-999.upto(1000) do |b|
primes = get_amount_of_primes_from_quadratic_formula(a,b)
if primes > max_primes then
max_primes = primes
max_product = a*b
end
end
end
max_product
end
start = Time.now
answer = get_product_of_coefficients_that_produce_maximum_number_of_primes_for_consecutive_values()
puts "The answer is #{answer} and it took #{Time.now-start} seconds."
I think I can improve on the if-then statement and write it more concise, and also the two "upto-loops" where I first declare the variables max_primes
and max_product
can be written in a more Ruby-way I am sure.
I would be very grateful if you could let me know how to write more like Ruby!
Links that ask similar questions which I am reading in the meantime:
I learned to program in Java and C# and in my free time I am using Ruby because it is fun. Unfortunately I have the feeling that I write Ruby code like I am making Java or C# code. I have learned to use regex instead of strings for comparison, using each instead of for-loops, keeping method names lowercase, and how to use code blocks (which are quite similar to lambda expressions in C#).
I would love to improve the Rubiness of my code and I hope you would be willing to give me one or more pointers on a piece of code I made. It answers Project Euler problem 27.
class Integer
def prime?
return false if self < 1
2.upto(Math.sqrt(self)) do |i|
return false if self % i == 0
end
true
end
end
def get_amount_of_primes_from_quadratic_formula(a,b)
primes = []
still_all_primes = true
n = 0
while still_all_primes
result = n**2 + a*n + b
if result.prime? then
primes << result
else
still_all_primes = false
end
n += 1
end
primes.size
end
def get_product_of_coefficients_that_produce_maximum_number_of_primes_for_consecutive_values()
max_product = 0
max_primes = 0
-999.upto(1000) do |a|
-999.upto(1000) do |b|
primes = get_amount_of_primes_from_quadratic_formula(a,b)
if primes > max_primes then
max_primes = primes
max_product = a*b
end
end
end
max_product
end
start = Time.now
answer = get_product_of_coefficients_that_produce_maximum_number_of_primes_for_consecutive_values()
puts "The answer is #{answer} and it took #{Time.now-start} seconds."
I think I can improve on the if-then statement and write it more concise, and also the two "upto-loops" where I first declare the variables max_primes
and max_product
can be written in a more Ruby-way I am sure.
I would be very grateful if you could let me know how to write more like Ruby!
Links that ask similar questions which I am reading in the meantime:
How to make my Ruby less like Java and more like Make Project Euler 27 solution idiomatic Ruby?
How to make my Ruby less like Java and more like Ruby?
I learned to program in Java and C# and in my free time I am using Ruby because it is fun. Unfortunately I have the feeling that I write Ruby code like I am making Java or C# code. I have learned to use regex instead of strings for comparison, using each instead of for-loops, keeping method names lowercase, and how to use code blocks (which are quite similar to lambda expressions in C#).
I would love to improve the Rubiness of my code and I hope you would be willing to give me one or more pointers on a piece of code I made. It answers Project Euler problem 27.
class Integer
def prime?
return false if self < 1
2.upto(Math.sqrt(self)) do |i|
return false if self % i == 0
end
true
end
end
def get_amount_of_primes_from_quadratic_formula(a,b)
primes = []
still_all_primes = true
n = 0
while still_all_primes
result = n**2 + a*n + b
if result.prime? then
primes << result
else
still_all_primes = false
end
n += 1
end
primes.size
end
def get_product_of_coefficients_that_produce_maximum_number_of_primes_for_consecutive_values()
max_product = 0
max_primes = 0
-999.upto(1000) do |a|
-999.upto(1000) do |b|
primes = get_amount_of_primes_from_quadratic_formula(a,b)
if primes > max_primes then
max_primes = primes
max_product = a*b
end
end
end
max_product
end
start = Time.now
answer = get_product_of_coefficients_that_produce_maximum_number_of_primes_for_consecutive_values()
puts "The answer is #{answer} and it took #{Time.now-start} seconds."
I think I can improve on the if-then statement and write it more concise, and also the two "upto-loops" where I first declare the variables max_primes
and max_product
can be written in a more Ruby-way I am sure.
I would be very grateful if you could let me know how to write more like Ruby!
Links that ask similar questions which I am reading in the meantime: