lib/euler.rb in Oompa-euler-1.0.2 vs lib/euler.rb in Oompa-euler-1.0.3

- old
+ new

@@ -1,15 +1,8 @@ -=begin rdoc -libeuler is a simple library you can simply +require+ in your code. -It's goal is to help reduce the repetativeness of some Project -Euler problems. - -Author:: Mike Skalnik(mailto:mike.skalnik@gmail.com) -Copyright:: 2008, Mike Skalnik; All Rights Reserved -License:: See LICENSE file -=end +# The module that contains all the methods. module Euler + VERSION = "1.0.3" extend self attr_reader :primes, :sieve, :fibonaccis # Several methods for the Integer class. module IntegerMethods # Returns +self+! @@ -23,14 +16,14 @@ # 10.prime_factors # => [2, 5] # 20.prime_factors # => [2, 2, 5] def prime_factors return [self] if self.prime? current, to_factor, factors, max = 2, self, [], 0 - # current is the value currently being tested - # to_factor is the value being factored (slowly moves to a prime value) - # factors is the array of prime factors - # max is the maximum value of any factor set after the next loop + # current is the value currently being tested to_factor is the value + # being factored (slowly moves to a prime value) factors is the array of + # prime factors max is the maximum value of any factor set after the + # next loop while to_factor % current == 0 factors << 2 to_factor /= current end current += 1 @@ -45,12 +38,11 @@ end factors << to_factor if to_factor > 1 return factors end - # Returns +true+ or +false+ depending on the primality of - # +self+ + # Returns a boolean which gives the primality of +self+ # 2.prime? # => true # 4.prime? # => false def prime? if self == 1 then return false elsif self < 4 then return true @@ -63,13 +55,12 @@ end end return true end - # Compares +self+ and +x+ and returns +true+ - # if the values are permutations, and +false+ - # otherwise. + # Compares +self+ and +x+ and returns +true+ if the values are + # permutations, and +false+ otherwise. # 123.is_permutation?(312) # => true # 312.is_permutation?(281) # => false def is_permutation?(x) return true if self == x # A number is a permutation of itself primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] @@ -86,24 +77,22 @@ end return s_total == x_total end - # Checks to see if +self+ is a pandigital with the range - # of x + # Checks to see if +self+ is a pandigital with the range of x # 0192837465.is_pandigital?(0..9) # => true # 0192837465.is_pandigital?(1..9) # => false def is_pandigital?(x) str = self.to_s x.each do |i| return false unless str.include? i.to_s end return true end - # Checks to see if +self+ is contained in the fibonacci - # sequence. + # Checks to see if +self+ is contained in the fibonacci sequence. # 1.is_fibonacci? # => true # 4.is_fibonacci? # => true def is_fibonacci? a, b = Math.sqrt((5*(self**2))+4), Math.sqrt((5*(self**2))-4) return true if a.to_i == a or b.to_i == b @@ -121,13 +110,12 @@ end return count end end - # A basic prime sieve. Sets Euler.sieve to an array of - # boolean values that indicate if that index if prime - # or not. + # A basic prime sieve. Sets Euler.sieve to an array of boolean values that + # indicate if that index if prime or not. # Euler.generate_sieve(50) # Euler::sieve[2] # => true # Euler::sieve[4] # => false def generate_sieve(max) @sieve = Array.new(max, false) @@ -138,12 +126,11 @@ (i**2).step(max, 2*i) { |x| @sieve[x] = false} end } end - # A more advanced prime sieve. Generates an n amount of - # prime values. + # A more advanced prime sieve. Generates an n amount of prime values. # Euler.get_primes(50) # Euler::primes[0] # => 2 # Euler::primes[2] # => 5 def get_primes(n) counter, current_value, @primes = 0, 2, [] @@ -174,11 +161,11 @@ # Euler.is_pythagorean_triplet?(1, 2, 3) # => false def is_pythagorean_triplet?(a, b, c) a**2 + b**2 == c**2 end - # Given two values of a pythagorean triplet, and nil for the missing - # value, it returns the missing value. + # Given two values of a pythagorean triplet, and nil for the missing value, + # it returns the missing value. # Euler.find_missing_pyth_value(nil, 4, 5) # => 3 def find_missing_pyth_value(a, b, c) return Math.sqrt(c**2 - b**2) if a.nil? return Math.sqrt(c**2 - a**2) if b.nil? return Math.sqrt(a**2 + b**2) if c.nil? \ No newline at end of file