Goodbye ruby and path to a new language 2
Well, not in a forever way. Ruby is the language I use mostly on my own time. Since my free time is very limited, I need to shelf it for now. Making room for me to hack some lisp.
Like many people, my free time comes in small chunks. I have been using project euler as a way to learn new languages (also math). The code required to solve each problem is usually very short (30 lines or less). I can usually fit each problem into my small chunks of time very easily. It's a great way to get your hands dirty on the language you want to learn.
For those who follow my blog, you probably noticed that I have been posting project euler solutions in ruby. Since I'm trying to learn lisp now, I'll start to solve the problems using lisp. I'll post the rest of my solutions written in ruby in the following post.
Overall, I still enjoy using ruby. The lack of free time has forced me to make this hard decision.
Project Euler Solutions 6 - 10 2
6. What is the difference between the sum of the squares and the square of the sums?
start = Time.now
sum_of_squares = 0
square_of_sums = 0
(1..100).each do |n|
sum_of_squares = sum_of_squares + n ** 2
square_of_sums = square_of_sums + n
end
puts "Took: #{Time.now - start} seconds"
puts (square_of_sums ** 2) - sum_of_squaresTook: 0.000165 seconds
max = 150000
table = {}
prime = []
start = Time.now
(2..max).each do |n|
table[n.to_s] = n
end
(2..(max + 1 / 2)).each do |n|
unless table[n.to_s].nil? then prime << n; end
if prime.length >= 10001 then break; end
index = n + n
while index <= max
table.delete(index.to_s)
index = index + n
end
end
puts "Took: #{Time.now - start} seconds"
puts "#{prime.length} prime is #{prime.last}"Took: 2.576914
8. Discover the largest product of five consecutive digits in the 1000-digit number.
lines = DATA.readlines
greatest = 0
greatest_5 = []
start = Time.now
lines.each do |line|
line_chars = line.split(//)
while not line_chars.empty?
result = 1
next_5 = line_chars.slice(0,5)
next_5.each do |n|
result = n.to_i * result
end
if result > greatest
greatest = result
greatest_5.replace(next_5)
end
line_chars.slice!(0,1)
end
end
puts "Took: #{Time.now - start} seconds"
puts "#{greatest}"
__END__
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450Took: 0.0071 seconds
9. Find the only Pythagorean triplet, {a, b, c}, for which a + b + c = 1000.
start = Time.now
(1..1000).each do |a|
((a + 1)..1000).each do |b|
a_2 = a ** 2
b_2 = b ** 2
c = Math.sqrt(a_2 + b_2)
if c == c.to_i
c = c.to_i
c_2 = c ** 2
sum = Math.sqrt(a_2) + Math.sqrt(b_2) + Math.sqrt(c_2)
if sum == 1000
puts "Took: #{Time.now - start} seconds"
p "#{a} + #{b} + #{c} = #{a + b + c}"
p "Product is #{a * b * c}"
exit
end
end
end
endTook: 0.522663 seconds
10. Calculate the sum of all the primes below two million.
start = Time.now
max_prime = 2000000
table = {}
(2...max_prime).each do |n|
table[n.to_s] = n
end
(2...(max_prime / 2)).each do |n|
index = n + n
while index < max_prime
table.delete(index.to_s)
index = index + n
end
end
result = 0
table.each do |k,v|
result = result + v
end
puts "Took: #{Time.now - start} seconds"
puts "Result is: #{result}"Took: 48.149662 seconds
Project Euler Solutions 1 - 5 5
Click on each problem for a more detailed solution.
1. Add all the natural numbers below 1000 that are multiples of 3 or 5.
start = Time.now
total = 0
(1...1000).each do |n|
total += n if (n % 3).zero? or (n % 5).zero?
end
puts "Took: #{Time.now - start} seconds"
puts totalTook: 0.000977 seconds
start = Time.now
def fib(n1, n2, total)
return total if n2 > 1000000
total += n2 if (n2 % 2).zero?
fib(n2, n1 + n2, total)
end
puts "Took: #{Time.now - start} seconds"
puts fib(1, 2, 0)Took: 1.0e-05 seconds
3. Find the largest prime factor of 317584931803.
def next_prime(start_num, max_num)
is_prime = true
prime = 0
(start_num + 1..max_num).each do |n|
prime = n
(2..n - 1).each do |nn|
if (n % nn).zero? then is_prime = false; break; end
end
if is_prime then break; end
is_prime = true
end
prime
end
start = Time.now
n = 1
biggest_prime = 0
num = 317584931803
while(num != 1 or num > n)
n = next_prime(n, num)
if n > 0
result = num % n
if (result).zero?
biggest_prime = n
num = num / n
end
end
n += 1
end
puts "Took: #{Time.now - start} seconds"
puts biggest_primeTook: 0.477182 seconds
4. Find the largest palindrome made from the product of two 3-digit numbers.
start = Time.now
result = 0
left = 0
right = 0
(100...1000).to_a.reverse.each do |l|
(100...1000).to_a.reverse.each do |r|
temp = (l * r).to_s
if temp == temp.reverse and temp.to_i > result
result = temp.to_i
left = l
right = r
end
end
end
puts "Took: #{Time.now - start} seconds"
puts "#{left} * #{right} = #{result}"Took: 1.501993 seconds
5. What is the smallest number divisible by each of the numbers 1 to 20?
def is_prime(num)
if num < 2 then return false; end
(2..num - 1).each do |n|
if (num % n).zero?
return false
end
end
return true
end
def smallest_factor(num)
(2..num - 1).each do |n|
if (num % n).zero? then return n; end
end
return num
end
start = Time.now
result = 1
(1..20).each do |n|
if is_prime(n)
result = result * n
elsif not (result % n).zero?
result = result * smallest_factor(n)
end
end
puts "Took: #{Time.now - start} seconds"
puts resultTook: 0.00018 seconds
