Module: FiveLeaves
- Defined in:
- lib/five_leaves/maximize.rb,
lib/five_leaves/lazy_enum.rb,
lib/five_leaves/prime_util.rb,
lib/five_leaves/palindromic.rb
Class Method Summary collapse
- .factors(n) ⇒ Object
- .find(init = 0) ⇒ Object
-
.maximize(range = 1..1_000_000) ⇒ Object
find the input in the range which maximizes the result of the block.
- .palindromic?(n) ⇒ Boolean
- .prime_factors(n) ⇒ Object
- .proper_factors(n) ⇒ Object
- .series(init = 0) ⇒ Object
- .small_factors(n) ⇒ Object
Class Method Details
.factors(n) ⇒ Object
21 22 23 |
# File 'lib/five_leaves/prime_util.rb', line 21 def self.factors(n) [n] + proper_factors(n) end |
.find(init = 0) ⇒ Object
34 35 36 |
# 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) ⇒ Object
find the input in the range which maximizes the result of the block
5 6 7 |
# 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) ⇒ Boolean
4 5 6 7 |
# 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) ⇒ Object
25 26 27 28 |
# 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) ⇒ Object
16 17 18 19 |
# 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) ⇒ Object
30 31 32 |
# 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) ⇒ Object
12 13 14 |
# 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 |