Module: Boqwij::IntegerExtensions
- Defined in:
- lib/boqwij/integer_extensions.rb
Overview
A set of extensions to the Integer class.
Instance Method Summary collapse
-
#delimit(delimiter = ",") ⇒ Object
Add a delimiter to every three digits of a long integer.
-
#even? ⇒ Boolean
Test if integer is even.
-
#factorial ⇒ Object
Get the factorial of given number.
-
#fibonacci ⇒ Object
Get the fibonacci number associated with a particular integer.
-
#is_divisible_by?(factor) ⇒ Boolean
Test if a number is divisible by a given factor.
-
#is_fibonacci? ⇒ Boolean
Checks to see if integer is contained in the fibonacci sequence.
-
#is_palindrome? ⇒ Boolean
Test to see if a given number is a palindrome.
-
#is_permutation_of?(x) ⇒ Boolean
Compares two integers to see if they are permutations of each other.
-
#is_prime? ⇒ Boolean
Test to see if a given number is prime.
-
#length ⇒ Object
Find the length of an integer.
-
#neg? ⇒ Boolean
Test if integer is negative.
-
#odd? ⇒ Boolean
Test if integer is odd.
-
#pos? ⇒ Boolean
Test if integer is positive.
-
#prime_factors ⇒ Object
Get an array of prime factors for any integer.
Instance Method Details
#delimit(delimiter = ",") ⇒ Object
Add a delimiter to every three digits of a long integer. The default delimiter is a comma.
35 36 37 38 39 |
# File 'lib/boqwij/integer_extensions.rb', line 35 def delimit(delimiter = ",") self.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}") rescue self.to_s end |
#even? ⇒ Boolean
Test if integer is even.
10 11 12 |
# File 'lib/boqwij/integer_extensions.rb', line 10 def even? !self.odd? end |
#factorial ⇒ Object
Get the factorial of given number. (e.g., 6.factorial # => 720)
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/boqwij/integer_extensions.rb', line 43 def factorial return nil if self < 1 n = self count = n fact = 1 count.times do fact = fact * n n -= 1 end fact end |
#fibonacci ⇒ Object
Get the fibonacci number associated with a particular integer. (e.g., 6.fibonacci # => 8; 10.fibonacci # => 55)
57 58 59 60 61 62 |
# File 'lib/boqwij/integer_extensions.rb', line 57 def fibonacci return nil if self < 1 current, successor = 0, 1 self.times { |i| current, successor = successor, current + successor} current end |
#is_divisible_by?(factor) ⇒ Boolean
Test if a number is divisible by a given factor. (e.g., 18.is_divisible_by?(3) #=> true)
115 116 117 |
# File 'lib/boqwij/integer_extensions.rb', line 115 def is_divisible_by?(factor) self%factor == 0 end |
#is_fibonacci? ⇒ Boolean
Checks to see if integer is contained in the fibonacci sequence. (e.g, 12.is_fibonacci? # => false; 55.is_fibonacci? # => true)
66 67 68 69 70 71 |
# File 'lib/boqwij/integer_extensions.rb', line 66 def is_fibonacci? return true if self == 1 a, b = Math.sqrt((5*(self**2))+4), Math.sqrt((5*(self**2))-4) return true if (a.to_i == a and b.to_i != b) or (a.to_i != a && b.to_i == b) return false end |
#is_palindrome? ⇒ Boolean
Test to see if a given number is a palindrome. (e.g., 1001.is_palindrome? # => true; 4654.is_palindrome? # => false)
121 122 123 124 125 126 |
# File 'lib/boqwij/integer_extensions.rb', line 121 def is_palindrome? return false if self < 0 @original = self.to_s @backwards = @original.reverse @original.to_i == @backwards.to_i end |
#is_permutation_of?(x) ⇒ Boolean
Compares two integers to see if they are permutations of each other. (e.g, 314159.is_permutation_of?(431951) # => true)
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/boqwij/integer_extensions.rb', line 130 def is_permutation_of?(x) return true if self == x 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 s_total == x_total end |
#is_prime? ⇒ Boolean
Test to see if a given number is prime. (e.g., 2.is_prime? # => true; 104743.is_prime? # => true; e.g., 4.is_prime? # => false; 104742.is_prime? # => false)
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/boqwij/integer_extensions.rb', line 76 def is_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 |
#length ⇒ Object
Find the length of an integer.
25 26 27 28 29 30 31 32 |
# File 'lib/boqwij/integer_extensions.rb', line 25 def length n, count = self, 1 while n > 9 n /= 10 count += 1 end count end |
#neg? ⇒ Boolean
Test if integer is negative.
15 16 17 |
# File 'lib/boqwij/integer_extensions.rb', line 15 def neg? self < 0 end |
#odd? ⇒ Boolean
Test if integer is odd.
5 6 7 |
# File 'lib/boqwij/integer_extensions.rb', line 5 def odd? self%2 != 0 end |
#pos? ⇒ Boolean
Test if integer is positive.
20 21 22 |
# File 'lib/boqwij/integer_extensions.rb', line 20 def pos? self > 0 end |
#prime_factors ⇒ Object
Get an array of prime factors for any integer. (e.g., 30.prime_factors # => [2,3,5])
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/boqwij/integer_extensions.rb', line 92 def prime_factors return [self] if self.is_prime? current, to_factor, factors, max = 2, self, [], 0 while to_factor % current == 0 factors << current 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 current += 2 end end factors << to_factor if to_factor > 1 factors end |