Module: Euler::IntegerMethods

Defined in:
lib/euler.rb

Overview

Several methods for the Integer class.

Instance Method Summary collapse

Instance Method Details

#factorialObject

Returns self!

5.factorial # => 120


10
11
12
13
# File 'lib/euler.rb', line 10

def factorial
  return 0 if self < 1
  (2..self).inject(1) { |value, n| value * n }
end

#is_fibonacci?Boolean

Checks to see if self is contained in the fibonacci sequence.

1.is_fibonacci? # => true
4.is_fibonacci? # => true

Returns:

  • (Boolean)


96
97
98
99
100
# File 'lib/euler.rb', line 96

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
  return false
end

#is_pandigital?(x) ⇒ Boolean

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

Returns:

  • (Boolean)


85
86
87
88
89
90
91
# File 'lib/euler.rb', line 85

def is_pandigital?(x)
  str = self.to_s
  x.each do |i|
    return false unless str.include? i.to_s
  end
  return true
end

#is_permutation?(x) ⇒ Boolean

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

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/euler.rb', line 64

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]
  s, s_total, x_total = self, 1, 1
  
  while s > 0
    s_total *= primes[s % 10]
    s /= 10
  end
  
  while x > 0
    x_total *= primes[x % 10]
    x /= 10
  end
  
  return s_total == x_total
end

#lengthObject

Returns how many digits self contains.

10.length   # => 2
4563.length # => 4


105
106
107
108
109
110
111
112
# File 'lib/euler.rb', line 105

def length
  n, count = self, 1
  while n > 9
    n /= 10
    count += 1
  end
  return count
end

#prime?Boolean

Returns a boolean which gives the primality of self

2.prime? # => true
4.prime? # => false

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/euler.rb', line 46

def prime?
  if    self == 1     then return false
  elsif self < 4      then return true
  elsif self % 2 == 0 then return false
  elsif self < 9      then return true
  elsif self % 3 == 0 then return false
  else
    5.step(Math.sqrt(self).to_i, 2) do |x|
      return false if self % x == 0
    end
  end
  return true
end

#prime_factorsObject

Returns an array of prime factors of self

10.prime_factors # => [2, 5]
20.prime_factors # => [2, 2, 5]


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/euler.rb', line 18

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
  while to_factor % current == 0
    factors << 2
    to_factor /= current
  end
  current += 1
  max = Math.sqrt(to_factor).to_i + 1
  while to_factor <= max
    if to_factor % current == 0
      factors << current
      to_factor /= current
    else
      i += 2
    end
  end
  factors << to_factor if to_factor > 1
  return factors
end