module FiveLeaves

Public Class Methods

factors(n) click to toggle source
# File lib/five_leaves/prime_util.rb, line 21
def self.factors(n)
  [n] + proper_factors(n)
end
find(init=0) { |x| ... } click to toggle source
# File lib/five_leaves/lazy_enum.rb, line 34
def self.find(init=0)
  (init..(1.0/0.0)).find { |x| yield x }
end
maximize(range = 1..1_000_000) { |x| ... } click to toggle source

find the input in the range which maximizes the result of the block

# File lib/five_leaves/maximize.rb, line 5
def self.maximize(range = 1..1_000_000)
  (range).lazy_map { |x| [yield(x), x] }.max[1]
end
palindromic?(n) click to toggle source
# File lib/five_leaves/palindromic.rb, line 4
def self.palindromic?(n)
  string = n.to_s
  (0..string.size/2).all_match? { |i| string[i] == string[-1-i] }
end
prime_factors(n) click to toggle source
# File lib/five_leaves/prime_util.rb, line 25
def self.prime_factors(n)
  small = small_factors(n)
  (small.map { |f| n / f } + small).uniq.select { |x| x.prime? }
end
proper_factors(n) click to toggle source
# File lib/five_leaves/prime_util.rb, line 16
def self.proper_factors(n)
  sf = small_factors(n)
  (sf.map { |x| n / x } + sf).uniq - [n]
end
series(init=0) { |x| ... } click to toggle source
# File lib/five_leaves/lazy_enum.rb, line 30
def self.series(init=0)
  (init..(1.0/0.0)).lazy_map { |x| yield x }
end
small_factors(n) click to toggle source
# File lib/five_leaves/prime_util.rb, line 12
def self.small_factors(n)
  (Math.sqrt(n).truncate.downto 1).select { |x| x.divides? n }
end