The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Project Euler 1-5 (Ruby)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jan Lelis

Posts: 136
Nickname: rbjl
Registered: Aug, 2009

Jan Lelis is an IT student from Dresden/Germany
Project Euler 1-5 (Ruby) Posted: Jan 18, 2010 7:56 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Jan Lelis.
Original Post: Project Euler 1-5 (Ruby)
Feed Title: rbJ*_*L.net
Feed URL: http://feeds.feedburner.com/rbJL
Feed Description: Hi, I am a fan of Ruby and like to explore it and the world around ;). So I started this blog, where I am publishing code snippets, tutorials for beginners as well as general thoughts about Ruby, the web or programming in general.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jan Lelis
Latest Posts From rbJ*_*L.net

Advertisement

projecteuler.net tries to get you thinking about how to solve mathematical problems by programming. Here are the first five problems, solved in Ruby, including comments.

Listing 1
/16/euler-001.rb ruby
# http://projecteuler.net/index.php?section=problems&id=1
#  look at the first 1000 numbers and add those to the sum,
#  which are multiples of 3 or 5

sum = 0 # initial sum

1000.times{ |n| # iteration index (0-999)
  sum += n if n%3==0 || # add to sum if divisible by
              n%5==0    #  3 or 5 without rest
}

puts sum

Listing 2
/16/euler-002.rb ruby 1.9
# http://projecteuler.net/index.php?section=problems&id=2
#  calculate fibonacci (non-recursive) and sum all even values

n=m = 1 # first two fib. numbers
sum = 0 # initial sum

while m < 4_000_000 # as long as fib. sequence is < 4 000 000
  n,m = m,n+m # calculate next step (sum of the prev. 2 numbers)
  sum += n  if n.even? # add to sum if even [ruby 1.9]
end

puts sum

Listing 3
/16/euler-003.rb ruby
# http://projecteuler.net/index.php?section=problems&id=3
#  find the largest prime factor of 600 851 475 143

a = 600_851_475_143
f = 2 # smallest possible prime factor

# Divide "evil big number" by lowest prime factors to get the
#  biggest one. Prime factors are those numbers, which do not
#  leave a rest. If a possible number is not a prime factor 
#  candidate anymore, check next one
a%f==0 ? a/=f : f+=1  while a>1

puts f

Listing 4
/16/euler-004.rb ruby
# http://projecteuler.net/index.php?section=problems&id=4
#  find the largest palindrome made from xxx*xxx

products = # get an array of ALL xxx*xxx
  (100..999).map{ |a|
    (100..999).map{ |b|
      a*b
    }
  }.flatten.select{ |p|      # select those, which are reversed
    p.to_s == p.to_s.reverse # the same as non-reversed
  }.sort.reverse # sort them and reverse result

puts products[0] # put out first (and largest) palindrome

Listing 5
/16/euler-005.rb ruby 1.87
# http://projecteuler.net/index.php?section=problems&id=5
#  find a number that you can divide by 1..20
#  using the method described at the solution sheet at euler

# needed input
till = 20
primes = [2,3,5,7,11,13,17,19]

# get factor for each prime
factors = primes.map{ |f|
  f > (limit ||= till**0.5) ? 1 : # only calcutlate if f < square root of till
  ( Math.log(20)/Math.log(f) ).to_i # calc using log (see euler sheet)
}

# now multiply all primes**factors
number = 1
p = primes.each  # init extern iterators
f = factors.each

loop{ number *= p.next**f.next } # loop breaks when StopIteration is raised

# output
puts number
CC-BY-SA (DE)

Read: Project Euler 1-5 (Ruby)

Topic: Build it from source Previous Topic   Next Topic Topic: rake_commit_tasks now supports git

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use