numb 0.152.0 → 0.170.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. data/VERSION +1 -1
  2. data/lib/numb/bell.rb +9 -12
  3. data/lib/numb/bernoulli.rb +7 -8
  4. data/lib/numb/catalan.rb +1 -5
  5. data/lib/numb/divisors/abundant.rb +58 -0
  6. data/lib/numb/divisors/aliquot.rb +31 -0
  7. data/lib/numb/divisors/amicable.rb +38 -0
  8. data/lib/numb/divisors/perfect.rb +73 -0
  9. data/lib/numb/divisors.rb +389 -0
  10. data/lib/numb/entringer.rb +2 -0
  11. data/lib/numb/figurate.rb +138 -0
  12. data/lib/numb/in_sequence.rb +11 -0
  13. data/lib/numb/jacobsthal_lucas.rb +5 -6
  14. data/lib/numb/knuth.rb +1 -4
  15. data/lib/numb/lucas.rb +5 -5
  16. data/lib/numb/lucas2.rb +11 -0
  17. data/lib/numb/lychrel.rb +0 -4
  18. data/lib/numb/palindrome.rb +8 -0
  19. data/lib/numb/pell_lucas.rb +9 -0
  20. data/lib/numb/primes.rb +164 -0
  21. data/lib/numb/pronic.rb +1 -0
  22. data/lib/numb/q.rb +13 -0
  23. data/lib/numb/schr/303/266der.rb +20 -0
  24. data/lib/numb/segmented.rb +29 -0
  25. data/lib/numb/stirling.rb +9 -0
  26. data/lib/numb/stirling2.rb +9 -0
  27. data/lib/numb/strictly_non_palindromic.rb +1 -5
  28. data/lib/numb/super_catalan.rb +17 -0
  29. data/lib/numb/super_d.rb +6 -0
  30. data/lib/numb/takeuchi.rb +18 -0
  31. data/lib/numb/unhappy.rb +2 -0
  32. data/lib/numb.rb +1 -53
  33. data/spec/numb/first_with_n_divisors_spec.rb +15 -0
  34. data/spec/numb/highly_composite_spec.rb +1 -1
  35. data/spec/numb/jacobsthal_lucas_spec.rb +19 -11
  36. data/spec/numb/lucas2_spec.rb +30 -0
  37. data/spec/numb/lucas_spec.rb +17 -9
  38. data/spec/numb/minimal_spec.rb +1 -1
  39. data/spec/numb/near_square_spec.rb +54 -0
  40. data/spec/numb/octahedral_spec.rb +29 -0
  41. data/spec/numb/palindrome_spec.rb +43 -0
  42. data/spec/numb/pell_lucas_spec.rb +28 -0
  43. data/spec/numb/pentatope_spec.rb +15 -0
  44. data/spec/numb/primitive_abundant_spec.rb +21 -0
  45. data/spec/numb/pyramidal_spec.rb +42 -0
  46. data/spec/numb/q_spec.rb +31 -0
  47. data/spec/numb/schr/303/266der_spec.rb +28 -0
  48. data/spec/numb/segmented_spec.rb +29 -0
  49. data/spec/numb/square_part_spec.rb +15 -0
  50. data/spec/numb/star_spec.rb +29 -0
  51. data/spec/numb/stella_octangula_spec.rb +29 -0
  52. data/spec/numb/stirling2_spec.rb +23 -0
  53. data/spec/numb/stirling_spec.rb +23 -0
  54. data/spec/numb/super_catalan_spec.rb +29 -0
  55. data/spec/numb/super_d_spec.rb +68 -0
  56. data/spec/numb/takeuchi_spec.rb +28 -0
  57. data/spec/numb/tetrahedral_spec.rb +15 -0
  58. metadata +63 -100
  59. data/lib/numb/abundancy.rb +0 -8
  60. data/lib/numb/abundant.rb +0 -20
  61. data/lib/numb/achilles.rb +0 -15
  62. data/lib/numb/aliquot_sequence.rb +0 -13
  63. data/lib/numb/almost_perfect.rb +0 -8
  64. data/lib/numb/almost_prime.rb +0 -6
  65. data/lib/numb/amicable.rb +0 -6
  66. data/lib/numb/aspiring.rb +0 -8
  67. data/lib/numb/augmented_amicable.rb +0 -7
  68. data/lib/numb/balanced_prime.rb +0 -10
  69. data/lib/numb/betrothed.rb +0 -8
  70. data/lib/numb/breeder.rb +0 -9
  71. data/lib/numb/brilliant.rb +0 -7
  72. data/lib/numb/centered_cube.rb +0 -7
  73. data/lib/numb/centered_hexagonal.rb +0 -6
  74. data/lib/numb/centered_n_gonal.rb +0 -9
  75. data/lib/numb/centered_pentagonal.rb +0 -6
  76. data/lib/numb/centered_square.rb +0 -5
  77. data/lib/numb/centered_triangular.rb +0 -5
  78. data/lib/numb/composite.rb +0 -5
  79. data/lib/numb/coprime.rb +0 -9
  80. data/lib/numb/cube.rb +0 -5
  81. data/lib/numb/d.rb +0 -6
  82. data/lib/numb/decagonal.rb +0 -5
  83. data/lib/numb/deficient.rb +0 -19
  84. data/lib/numb/dihedral_prime.rb +0 -26
  85. data/lib/numb/dodecagonal.rb +0 -5
  86. data/lib/numb/e_divisors.rb +0 -10
  87. data/lib/numb/e_perfect.rb +0 -7
  88. data/lib/numb/economical.rb +0 -16
  89. data/lib/numb/emrip.rb +0 -15
  90. data/lib/numb/equidigital.rb +0 -18
  91. data/lib/numb/extravagant.rb +0 -18
  92. data/lib/numb/fermat_pseudoprime.rb +0 -9
  93. data/lib/numb/friendly.rb +0 -9
  94. data/lib/numb/full_reptend_prime.rb +0 -5
  95. data/lib/numb/giuga.rb +0 -7
  96. data/lib/numb/harshad.rb +0 -9
  97. data/lib/numb/heptagonal.rb +0 -5
  98. data/lib/numb/hexagonal.rb +0 -6
  99. data/lib/numb/highly_abundant.rb +0 -7
  100. data/lib/numb/highly_composite.rb +0 -12
  101. data/lib/numb/hoax.rb +0 -7
  102. data/lib/numb/hyperperfect.rb +0 -7
  103. data/lib/numb/impolite.rb +0 -6
  104. data/lib/numb/infinitary_divisors.rb +0 -15
  105. data/lib/numb/infinitary_perfect.rb +0 -6
  106. data/lib/numb/interprime.rb +0 -6
  107. data/lib/numb/k_perfect.rb +0 -8
  108. data/lib/numb/mersenne_prime.rb +0 -5
  109. data/lib/numb/minimal.rb +0 -8
  110. data/lib/numb/multiamicable.rb +0 -7
  111. data/lib/numb/myriagonal.rb +0 -5
  112. data/lib/numb/n_gonal.rb +0 -7
  113. data/lib/numb/next_prev_prime.rb +0 -13
  114. data/lib/numb/nth_prime.rb +0 -17
  115. data/lib/numb/number_of_divisors.rb +0 -22
  116. data/lib/numb/octagonal.rb +0 -5
  117. data/lib/numb/ore.rb +0 -8
  118. data/lib/numb/pentagonal.rb +0 -5
  119. data/lib/numb/perfect.rb +0 -8
  120. data/lib/numb/perfect_power.rb +0 -9
  121. data/lib/numb/polite.rb +0 -7
  122. data/lib/numb/powerful.rb +0 -10
  123. data/lib/numb/practical.rb +0 -26
  124. data/lib/numb/prime_count.rb +0 -16
  125. data/lib/numb/prime_signature.rb +0 -5
  126. data/lib/numb/primitive_pseudoperfect.rb +0 -5
  127. data/lib/numb/rhonda.rb +0 -6
  128. data/lib/numb/rough.rb +0 -5
  129. data/lib/numb/safe_prime.rb +0 -5
  130. data/lib/numb/semiperfect.rb +0 -20
  131. data/lib/numb/semiprime.rb +0 -6
  132. data/lib/numb/smith.rb +0 -7
  133. data/lib/numb/smooth.rb +0 -5
  134. data/lib/numb/sociable.rb +0 -6
  135. data/lib/numb/sophie_germain_prime.rb +0 -5
  136. data/lib/numb/square.rb +0 -7
  137. data/lib/numb/square_free.rb +0 -6
  138. data/lib/numb/square_triangular.rb +0 -5
  139. data/lib/numb/squared_triangular.rb +0 -5
  140. data/lib/numb/sublime.rb +0 -6
  141. data/lib/numb/sum_of_e_divisors.rb +0 -10
  142. data/lib/numb/sum_of_infinitary_divisors.rb +0 -9
  143. data/lib/numb/sum_of_unitary_divisors.rb +0 -5
  144. data/lib/numb/super_poulet.rb +0 -5
  145. data/lib/numb/superabundant.rb +0 -18
  146. data/lib/numb/superperfect.rb +0 -6
  147. data/lib/numb/triangular.rb +0 -6
  148. data/lib/numb/twin_prime.rb +0 -5
  149. data/lib/numb/unitary_amicable.rb +0 -7
  150. data/lib/numb/unitary_divisor.rb +0 -5
  151. data/lib/numb/unitary_perfect.rb +0 -7
  152. data/lib/numb/unitary_sociable.rb +0 -7
  153. data/lib/numb/untouchable.rb +0 -14
  154. data/lib/numb/unusual.rb +0 -5
  155. data/lib/numb/weird.rb +0 -8
  156. data/lib/numb/wieferich.rb +0 -6
@@ -0,0 +1,138 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def square?
4
+ return false unless zero? or (positive? and to_s(16)[-1] =~ /[0149]/)
5
+ (sq = sqrt).finite? ? sq.integer? : nil
6
+ end
7
+
8
+ def square_triangular?
9
+ square? and triangular?
10
+ end
11
+
12
+ def squared_triangular?
13
+ square? and sqrt.to_i.triangular?
14
+ end
15
+
16
+ def square_free?
17
+ not μ.zero?
18
+ end
19
+
20
+ def centered_n_gonal?(n)
21
+ raise ArgumentError unless n > 2
22
+ Rational(
23
+ -n + Math.sqrt( n**2 - 8 * (n - n * self) ),
24
+ 2 * n
25
+ ).denominator == 1
26
+ end
27
+
28
+ def centered_cube?
29
+ 1.upto(Math.cbrt(self)).any? do |n|
30
+ self == n**3 + (n - 1) ** 3
31
+ end
32
+ end
33
+
34
+ def centered_hexagonal?
35
+ n = self - 1
36
+ n.divides?(6) and (n/6).triangular?
37
+ end
38
+
39
+ def centered_pentagonal?
40
+ n = self - 1
41
+ n.divides?(5) and (n/5).triangular?
42
+ end
43
+
44
+ def centered_square?
45
+ centered_n_gonal? 4
46
+ end
47
+
48
+ def centered_triangular?
49
+ centered_n_gonal?(3)
50
+ end
51
+
52
+ def cube?
53
+ Math.cbrt(self).integer?
54
+ end
55
+
56
+ def decagonal?
57
+ n_gonal?(10)
58
+ end
59
+
60
+ def n_gonal?(n)
61
+ raise ArgumentError unless n.is_a?(Integer) and n >= 3
62
+ return true if zero?
63
+ ((Math.sqrt((8*n - 16)*self + (n-4)**2) + n - 4) / (2*n - 4)).integer?
64
+ end
65
+
66
+ def dodecagonal?
67
+ n_gonal?(12)
68
+ end
69
+
70
+ def heptagonal?
71
+ n_gonal?(7)
72
+ end
73
+
74
+ def hexagonal?
75
+ return true if zero?
76
+ ((Math.sqrt((8*self) + 1) + 1)/4).integer?
77
+ end
78
+
79
+ def myriagonal?
80
+ n_gonal?(10_000)
81
+ end
82
+
83
+ def near_square?(k)
84
+ (self - k).square?
85
+ end
86
+
87
+ def octagonal?
88
+ n_gonal?(8)
89
+ end
90
+
91
+ def pyramidal(r)
92
+ n = self
93
+ (Rational(1, 6) * n * n.succ * (((r - 2) * n) + (5 - r))).to_i
94
+ end
95
+
96
+ def octahedral
97
+ (Rational(self, 3) * (2 * self**2 + 1)).to_i
98
+ end
99
+
100
+ def octahedral?
101
+ in_sequence?(seq: :octahedral)
102
+ end
103
+
104
+ def triangular?
105
+ n_gonal?(3)
106
+ end
107
+
108
+ def pentagonal?
109
+ n_gonal?(5)
110
+ end
111
+
112
+ def pentatope
113
+ (Rational(tetrahedral, 4) * (self + 3)).to_i
114
+ end
115
+
116
+ def star
117
+ 6 * self * (self - 1) + 1
118
+ end
119
+
120
+ def star?
121
+ return true if self == 1
122
+ return false unless [1, 4].include?(digital_root) and
123
+ to_s.end_with?(*%w{01 13 21 33 37 41 53 61 73 81 93})
124
+ centered_n_gonal?(12)
125
+ end
126
+
127
+ def tetrahedral
128
+ pyramidal(3)
129
+ end
130
+
131
+ def stella_octangula
132
+ octahedral + 8 * (self - 1).tetrahedral
133
+ end
134
+
135
+ def stella_octangula?
136
+ in_sequence?(seq: :stella_octangula)
137
+ end
138
+ end
@@ -0,0 +1,11 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def in_sequence?(args)
4
+ args = {range: (1..self), cond: :<, initial: []}.merge(args)
5
+ return true if Array(args[:initial]).include?(self)
6
+ args[:range].each do |n|
7
+ next if (term = n.send(args[:seq])).send(args[:cond], self)
8
+ return term == self ? true : false
9
+ end
10
+ end
11
+ end
@@ -1,10 +1,9 @@
1
1
  class Integer
2
2
  def jacobsthal_lucas?
3
- [self + 1, self - 1].any? do |n|
4
- exp = Math.log2(n)
5
- if exp.integer?
6
- self == 2**exp + (-1)**exp
7
- end
8
- end
3
+ in_sequence?(seq: :jacobsthal_lucas, initial: 2)
4
+ end
5
+
6
+ def jacobsthal_lucas
7
+ lucas2(1, -2)
9
8
  end
10
9
  end
data/lib/numb/knuth.rb CHANGED
@@ -7,9 +7,6 @@ class Integer
7
7
  end
8
8
 
9
9
  def knuth?
10
- downto(0) do |n|
11
- next if (k = n.knuth) > self
12
- return k == self ? true : false
13
- end
10
+ in_sequence?(range: downto(0), seq: :knuth, cond: :>)
14
11
  end
15
12
  end
data/lib/numb/lucas.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  class Integer
2
2
  def lucas?
3
- return true if (@@lucas ||= [2,1]).include?(self)
4
- while @@lucas.last < self
5
- @@lucas.push(@@lucas[-2] + @@lucas[-1])
6
- end
7
- @@lucas.last == self
3
+ in_sequence?(seq: :lucas, initial: [2])
4
+ end
5
+
6
+ def lucas
7
+ lucas2(1, -1)
8
8
  end
9
9
  end
@@ -0,0 +1,11 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def lucas2(p, q)
4
+ d = (p**2) - (4*q)
5
+ raise ArgumentError unless d > 0
6
+ d_root = d.sqrt
7
+ (Rational(p + d_root, 2)**self + Rational(p - d_root, 2)**self).round
8
+ end
9
+
10
+ memoize :lucas2
11
+ end
data/lib/numb/lychrel.rb CHANGED
@@ -1,8 +1,4 @@
1
1
  class Integer
2
- def palindrome?
3
- reverse == self
4
- end
5
-
6
2
  def lychrel?
7
3
  n = self
8
4
  # This limit is as arbitrary as it looks
@@ -0,0 +1,8 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def palindrome?(base=10)
4
+ base(base) == base(base).reverse
5
+ end
6
+
7
+ alias :palindromic? :palindrome?
8
+ end
@@ -0,0 +1,9 @@
1
+ class Integer
2
+ def pell_lucas?
3
+ in_sequence?(seq: :pell_lucas, initial: [2])
4
+ end
5
+
6
+ def pell_lucas
7
+ lucas2(2, -1)
8
+ end
9
+ end
@@ -0,0 +1,164 @@
1
+ # coding: utf-8
2
+ require 'prime'
3
+
4
+ class Integer
5
+ def almost_prime?(k)
6
+ Ω == k
7
+ end
8
+
9
+ def balanced_prime?
10
+ return false unless prime? and self >= 5
11
+ primes, before = Prime.each, 2
12
+ primes.each do |prime|
13
+ return ((before + primes.next) / 2) == self if prime == self
14
+ before = prime
15
+ end
16
+ end
17
+
18
+ # A dihedral prime is a prime number that appears as itself or another prime
19
+ # when rendered on a seven-segment display of a calculator and...
20
+ #
21
+ # * Rotated 180°.
22
+ # * Mirrored.
23
+ # * Rotated 180° and mirrored.
24
+ #
25
+ # For example, 120121 is a dihedral prime. It is 121021 when rotated,
26
+ # 151051 (another prime) when mirrored, and 150151 when rotated and
27
+ # mirrored.
28
+ #
29
+ # Returns true if self is a dihedral prime; false otherwise.
30
+ #
31
+ # 101.dihedral_prime? #=> true
32
+ # 181.dihedral_prime? #=> true
33
+ # 7.dihedral_prime? #=> false
34
+ #
35
+ def dihedral_prime?
36
+ return false unless prime? and to_s.match(/^[01825]+$/)
37
+ mirror = ->(n){ n.to_s.gsub(/([25])/){|orig| orig == '2' ? '5' : '2'}.to_i }
38
+ [reverse, mirror[self], mirror[reverse]].all?(&:prime?)
39
+ end
40
+
41
+ # An emrip is a prime whose reversed digits give a different prime. For
42
+ # example, 17 is an emrip because 71 is also prime.
43
+ #
44
+ # Returns true if self is an emrip; false otherwise.
45
+ #
46
+ # 1009.emrip? #=> true
47
+ # 1193.emrip? #=> true
48
+ # 7.emrip? #=> false
49
+ #
50
+ def emrip?
51
+ prime? and reverse != self and reverse.prime?
52
+ end
53
+
54
+ def full_reptend_prime?
55
+ prime? and primitive_root?(10)
56
+ end
57
+
58
+ def interprime?
59
+ return false if prime?
60
+ self == (next_prime + prev_prime)/2
61
+ end
62
+
63
+ def mersenne_prime?
64
+ mersenne? and prime?
65
+ end
66
+
67
+ def next_prime
68
+ p = succ
69
+ p += 1 until p.prime?
70
+ p
71
+ end
72
+
73
+ def prev_prime
74
+ p = self - 1
75
+ p -= 1 until p.prime?
76
+ p
77
+ end
78
+
79
+ def fermat_pseudoprime?(a=10)
80
+ return false unless composite?
81
+ q = self
82
+ raise ArgumentError unless a >= 2
83
+ raise ArgumentError unless (q - 2) >= a
84
+ (a**(q-1)).modulo(q) == 1
85
+ end
86
+
87
+ # Algorithm derived from Formulas for pi(n) and the n-th prime by Sebastian
88
+ # Martin Ruiz and Jonathan Sondow [arXiv:math/0210312v2 [math.NT]]
89
+
90
+ # Returns the number of primes equal to or less than self
91
+ def π
92
+ x = self
93
+ return 0 if x == 1
94
+ @prime_count ||= ([2] + (3..x).select(&:odd?)).map do |j|
95
+ 1 + ( ((2 - j.τ)/j).floor ).floor
96
+ end.reduce(:+)
97
+ end
98
+ alias :prime_pi :π
99
+ alias :prime_count :π
100
+
101
+ def prime_signature
102
+ prime_division.map{|base, exponent| exponent}.sort.reverse
103
+ end
104
+
105
+ # Algorithm derived from Formulas for pi(n) and the n-th prime by Sebastian
106
+ # Martin Ruiz and Jonathan Sondow [arXiv:math/0210312v2 [math.NT]]
107
+
108
+ # Returns, after many eons, the nth prime, where n = self
109
+ def nth_prime
110
+ n = self
111
+ return 2 if n == 1
112
+ raise ArgumentError if n < 1
113
+ (2..( 2*n * Math.log(n) + 2).floor).map do |k|
114
+ 1 - (k.π.fdiv(n)).floor
115
+ end.reduce(:+) + 2
116
+ end
117
+
118
+ alias :prime :nth_prime
119
+
120
+ def sophie_germain_prime?
121
+ prime? and (2*self).succ.prime?
122
+ end
123
+
124
+ def safe_prime?
125
+ prime? and odd? and ((self - 1) / 2).prime?
126
+ end
127
+
128
+ def semiprime?
129
+ Ω == 2
130
+ end
131
+
132
+ def composite?
133
+ self > 1 and not prime?
134
+ end
135
+
136
+ def twin_prime?(p)
137
+ [p, self].all?(&:prime?) and p == self + 2
138
+ end
139
+
140
+
141
+ alias :primaries :prime_division
142
+
143
+ def number_of_distinct_prime_factors
144
+ prime_factors.uniq.size
145
+ end
146
+ alias :omega :number_of_distinct_prime_factors
147
+ alias :ω :number_of_distinct_prime_factors
148
+
149
+ def number_of_prime_factors
150
+ prime_factors.size
151
+ end
152
+ alias :bigomega :number_of_prime_factors
153
+ alias :Ω :number_of_prime_factors
154
+
155
+ def prime_factors
156
+ return [] if zero?
157
+ prime_division.map{|pair| [pair.first] * pair.last}.flatten
158
+ end
159
+
160
+ def wieferich_prime?
161
+ return false unless prime?
162
+ (2**(self - 1)).modulo(self ** 2) == 1
163
+ end
164
+ end
data/lib/numb/pronic.rb CHANGED
@@ -6,4 +6,5 @@ class Integer
6
6
 
7
7
  alias :heteromecic? :pronic?
8
8
  alias :oblong? :pronic?
9
+ alias :promic? :pronic?
9
10
  end
data/lib/numb/q.rb ADDED
@@ -0,0 +1,13 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def q
4
+ return 1 if (n = self) <= 2
5
+ (n - (n - 1).q).q + (n - (n - 2).q).q
6
+ end
7
+
8
+ memoize :q
9
+
10
+ def q?
11
+ (1..(self ** 2)).any?{|n| n.q == self} or nil
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def schröder
4
+ return 1 if zero?
5
+ n = self
6
+ (n-1).schröder + (0...n).map do |k|
7
+ k.schröder * (n - 1 - k).schröder
8
+ end.reduce(:+)
9
+ end
10
+
11
+ memoize :schröder
12
+
13
+ alias :schroder :schröder
14
+
15
+ def schröder?
16
+ in_sequence?(seq: :schröder, initial: 1)
17
+ end
18
+
19
+ alias :schroder? :schröder?
20
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def segmented
4
+ return self if self <= 2
5
+ this = ((n = self)-1).segmented.succ
6
+
7
+ sum = Hash.new{|h, k| h[k] = k.map(&:segmented).reduce(:+) }
8
+
9
+ priors_sum = ->(target) do
10
+ (n - 2).downto(1).any? do |start|
11
+ (start.succ..(n-1)).any? do |stop|
12
+ break if (s = sum[ start..stop ]) > target
13
+ s == target
14
+ end
15
+ end
16
+ end
17
+
18
+ this += 1 while priors_sum[ this ]
19
+ this
20
+ end
21
+
22
+ memoize :segmented
23
+
24
+ def segmented?
25
+ in_sequence?(seq: :segmented)
26
+ end
27
+
28
+ alias :prime_number_of_measurement? :segmented?
29
+ end
@@ -0,0 +1,9 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def stirling(m)
4
+ n = self
5
+ return 1 if (n.zero? and m.zero?) or m == n
6
+ return 0 if m > n or m.zero? or n.zero?
7
+ (n-1).stirling(m-1) - (n-1)*(n-1).stirling(m)
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def stirling2(m)
4
+ n = self
5
+ return 1 if (n.zero? and m.zero?) or m == n
6
+ return 0 if m > n or m.zero? or n.zero?
7
+ (n-1).stirling2(m-1) + m * (n-1).stirling2(m)
8
+ end
9
+ end
@@ -1,10 +1,6 @@
1
- class String
2
- def palindrome?; self == reverse; end
3
- end
4
-
5
1
  class Integer
6
2
  def strictly_non_palindromic?
7
3
  return true if (0..4).include?(self) or self == 6
8
- prime? and (2..(isqrt)).none?{|base| base(base).palindrome?}
4
+ prime? and (2..isqrt).none?{|base| palindrome?(base)}
9
5
  end
10
6
  end
@@ -0,0 +1,17 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def super_catalan
4
+ n = self
5
+ return 1 if n <= 2
6
+ Rational(
7
+ 3 * ((2 * n) - 3) * (n - 1).super_catalan - (n - 3) * (n - 2).super_catalan,
8
+ n
9
+ ).to_i
10
+ end
11
+
12
+ memoize :super_catalan
13
+
14
+ def super_catalan?
15
+ in_sequence?(seq: :super_catalan)
16
+ end
17
+ end
@@ -0,0 +1,6 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def super_d?(d)
4
+ (d * self**d).to_s.include?(d.to_s * d)
5
+ end
6
+ end
@@ -0,0 +1,18 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def takeuchi
4
+ n = self
5
+ return n if n <= 1
6
+ (0..n-2).map do |k|
7
+ (
8
+ (2 * (n + k - 1).choose(k)) - (n + k).choose(k)
9
+ ) * (n - k - 1).takeuchi
10
+ end.reduce(:+) + (1..n).map(&:catalan).reduce(:+)
11
+ end
12
+
13
+ memoize :takeuchi
14
+
15
+ def takeuchi?
16
+ in_sequence?(seq: :takeuchi)
17
+ end
18
+ end
data/lib/numb/unhappy.rb CHANGED
@@ -2,4 +2,6 @@ class Integer
2
2
  def unhappy?
3
3
  not happy?
4
4
  end
5
+
6
+ alias :sad? :unhappy?
5
7
  end
data/lib/numb.rb CHANGED
@@ -17,27 +17,6 @@ end
17
17
  end
18
18
 
19
19
  class Integer
20
- def number_of_distinct_prime_factors
21
- prime_factors.uniq.size
22
- end
23
- alias :omega :number_of_distinct_prime_factors
24
- alias :ω :number_of_distinct_prime_factors
25
-
26
- def number_of_prime_factors
27
- prime_factors.size
28
- end
29
- alias :bigomega :number_of_prime_factors
30
- alias :Ω :number_of_prime_factors
31
-
32
- def prime_factors
33
- return [] if zero?
34
- prime_division.map{|pair| [pair.first] * pair.last}.flatten
35
- end
36
-
37
- def politeness
38
- divisors.select{|d| d > 1}.select{|d| d.odd?}.size
39
- end
40
-
41
20
  def sqrt
42
21
  Math.sqrt(self)
43
22
  end
@@ -46,28 +25,6 @@ class Integer
46
25
  sqrt.floor
47
26
  end
48
27
 
49
- def proper_divisors
50
- divisors.reject {|d| d == self }
51
- end
52
-
53
- def divisors
54
- return [] unless positive?
55
- return [1, self] if prime?
56
- (1..isqrt).select { |n| (self % n).zero? }.
57
- map {|n| [n, self/n]}.flatten.uniq
58
- end
59
- memoize :divisors
60
-
61
- def sum_of_divisors
62
- @sod ||= divisors.reduce(:+)
63
- end
64
- alias :σ :sum_of_divisors
65
-
66
- def aliquot_sum
67
- return 0 if zero?
68
- σ - self
69
- end
70
-
71
28
  def digital_root
72
29
  self == 0 ? 0 : 1 + ((self - 1) % 9)
73
30
  end
@@ -81,16 +38,7 @@ class Integer
81
38
  def digits
82
39
  self.to_s.split(//).map{|d| d.to_i}
83
40
  end
84
-
85
- def divides?(n)
86
- not n.zero? and (self % n).zero?
87
- end
88
41
  end
89
42
 
90
- require 'prime'
91
-
92
- class Integer
93
- alias :primaries :prime_division
94
- end
95
43
 
96
- Dir.glob(File.dirname(__FILE__) + '/numb/*.rb').each {|file| require file}
44
+ Dir.glob(File.dirname(__FILE__) + '/numb/**/*.rb').each {|file| require file}
@@ -0,0 +1,15 @@
1
+ # coding: utf-8
2
+ describe Integer, "#first_with_n_divisors" do
3
+ # A005179
4
+ @seq = [1,2,4,6,16,12,64,24,36,48,1024,60,4096,192,144,
5
+ 120,65536,180,262144,240,576,3072,4194304,360,
6
+ 1296,12288,900,960,268435456,720,1073741824,840,
7
+ 9216,196608,5184,1260,68719476736,786432,36864,
8
+ 1680,1099511627776,2880]
9
+
10
+ @seq.to_enum.with_index(1).each do |d, n|
11
+ it "returns #{d} as the first number with exactly #{n} divisors" do
12
+ n.first_with_n_divisors.should == d
13
+ end
14
+ end
15
+ end
@@ -6,7 +6,7 @@ describe Integer, "#highly_composite?" do
6
6
  221760,277200,332640,498960,554400,665280,720720,
7
7
  1081080,1441440,2162160].to_seq
8
8
 
9
- @seq.first(20).sample(5).each do |n|
9
+ @seq.first(18).sample(10).each do |n|
10
10
  it "should return true for highly composite integer #{n}" do
11
11
  n.should be_highly_composite
12
12
  end