source/euler58.rb
# This is gnarly; there are prettier ways I could've written this, but they
# are much slower.
This is gnarly; there are prettier ways I could've written this, but they
are much slower.
This is gnarly ; there are prettier ways I could ' ve written this but they are much slower .
def primes_below(known_primes=[2])
    return [] if self < 3
    primes = known_primes
    n = known_primes.length > 1 ? known_primes[-1] : 3
    until n >= self
      prime = false
      primes.each do |p|
        break if n % p == 0
        if p * p > n
          prime = true
          break
        end
      end
      primes << n if prime
      n += 2
    end
    primes
  end
def primes_below ( known_primes = [ 2 ] ) return [ ] if self < 3 primes = known_primes n = known_primes . length > 1 ? known_primes [ - 1 ] : 3 until n >= self prime = false primes . each do | p | break if n % p == 0 if p * p > n prime = true break end end primes << n if prime n += 2 end primes end
This is gnarly; there are prettier ways I could've written this, but they
are much slower.
This is gnarly ; there are prettier ways I could ' ve written this but they are much slower .
known_primes