numb 0.10.0 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.10.0
1
+ 0.20.0
@@ -0,0 +1,8 @@
1
+ class Integer
2
+ def carmichael?
3
+ return false unless odd? and composite? and square_free?
4
+ prime_factors.all? do |p|
5
+ (self - 1).remainder(p - 1) == 0
6
+ end
7
+ end
8
+ end
data/lib/numb/cube.rb ADDED
@@ -0,0 +1,8 @@
1
+ class Integer
2
+ def cube?
3
+ y = self < 0 ? (-self) : self
4
+ x = Math.cbrt(y)
5
+ # The hack below is seemingly needed for 1.8 compatibility
6
+ x.ceil.to_f.to_s == x.to_s
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ class Integer
2
+ def decagonal?
3
+ n_gonal?(10)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Integer
2
+ def dodecagonal?
3
+ n_gonal?(12)
4
+ end
5
+ end
data/lib/numb/dudeney.rb CHANGED
@@ -10,6 +10,8 @@ class Integer
10
10
  # 98.dudeney? #=> false
11
11
  #
12
12
  def dudeney?
13
- Math.cbrt(self) == self.digits.reduce(:+)
13
+ # The ugly hack below is seemingly needed for 1.8 compatibility. I ave
14
+ # yet to understand why.
15
+ Math.cbrt(self).to_s.sub(/\.0$/,'') == self.digits.reduce(:+).to_s
14
16
  end
15
17
  end
@@ -0,0 +1,5 @@
1
+ class Integer
2
+ def heptagonal?
3
+ n_gonal?(7)
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ class Integer
2
+ def hexagonal?
3
+ return true if zero?
4
+ n = ((Math.sqrt((8*self) + 1) + 1)/4)
5
+ n == n.to_i
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ class Integer
2
+ def myriagonal?
3
+ n_gonal?(10_000)
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ class Integer
2
+ def n_gonal?(n)
3
+ raise ArgumentError unless n.is_a?(Integer) and n >= 3
4
+ return true if zero?
5
+ x = (Math.sqrt((8*n - 16)*self + (n-4)**2) + n - 4) / (2*n - 4)
6
+ x == x.to_i
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class Integer
2
+ def octagonal?
3
+ n_gonal?(8)
4
+ #return true if zero?
5
+ #x = (Math.sqrt(48*self + 16) + 4)/12
6
+ #x.to_i == x
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ class Integer
2
+ def pentagonal?
3
+ n_gonal?(5)
4
+ end
5
+ end
data/lib/numb/ruby1.8.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Math
2
2
  def self.cbrt(x)
3
- (x.to_f**(1.0/3.0)).round.to_f
3
+ (x.to_f**(1.0/3.0))
4
4
  end unless Math.methods.any?{|m| m.to_s == 'cbrt'}
5
5
 
6
6
  def self.log2(x)
@@ -1,8 +1,6 @@
1
1
  # coding: utf-8
2
2
  class Integer
3
3
  def triangular?
4
- return false if self < 0
5
- root = Math.sqrt(8 * self + 1)
6
- root == root.floor
4
+ n_gonal?(3)
7
5
  end
8
6
  end
data/lib/numb.rb CHANGED
@@ -1,12 +1,14 @@
1
1
  # coding: utf-8
2
2
 
3
- libs = %w{abundant achilles automorphic balanced_prime carol composite deficient
4
- dihedral_prime dudeney economical emrip equidigital
5
- extravagant factorion frugal happy harshad hilbert hyperperfect
6
- impolite kaprekar keith kynea lucas lucas_carmichael mms_pair mobius
7
- narcissistic nivenmorphic ordinal parasitic perfect perfect_power
8
- polite polydivisible powerful practical primitive_pseudoperfect
9
- pronic rhonda self self_descriptive semiperfect semiprime
3
+ libs = %w{abundant achilles automorphic balanced_prime carmichael carol
4
+ composite cube decagonal deficient dodecagonal dihedral_prime
5
+ dudeney economical emrip equidigital extravagant factorion
6
+ frugal happy harshad heptagonal hexagonal hilbert hyperperfect
7
+ impolite kaprekar keith kynea lucas lucas_carmichael mms_pair
8
+ mobius myriagonal narcissistic nivenmorphic n_gonal octagonal
9
+ ordinal parasitic pentagonal perfect perfect_power polite
10
+ polydivisible powerful practical primitive_pseudoperfect pronic
11
+ rhonda self self_descriptive semiperfect semiprime
10
12
  smarandache_wellin smith sophie_germain_prime sphenic square
11
13
  square_free triangular trimorphic undulating vampire weird
12
14
  }
@@ -0,0 +1,20 @@
1
+ describe Integer, "#carmichael?" do
2
+ # A002997
3
+ CARMICHAEL = [561,1105,1729,2465,2821,6601,8911,10585,15841,
4
+ 29341,41041,46657,52633,62745,63973,75361,101101,
5
+ 115921,126217,162401,172081,188461,252601,278545,
6
+ 294409,314821,334153,340561,399001,410041,449065,
7
+ 488881,512461]
8
+
9
+ CARMICHAEL.each do |n|
10
+ it "returns true for Carmichael number #{n}" do
11
+ n.should be_carmichael
12
+ end
13
+ end
14
+
15
+ ((1..CARMICHAEL.last).to_a - CARMICHAEL).shuffle.first(10).each do |n|
16
+ it "returns false for non-Carmichael number #{n}" do
17
+ n.should_not be_carmichael
18
+ end
19
+ end
20
+ end
data/spec/cube_spec.rb ADDED
@@ -0,0 +1,24 @@
1
+ describe Integer, "#cube?" do
2
+ # A000578
3
+ CUBES = [0,1,8,27,64,125,216,343,512,729,1000,1331,1728,
4
+ 2197,2744,3375,4096,4913,5832,6859,8000,9261,
5
+ 10648,12167,13824,15625,17576,19683,21952,24389,
6
+ 27000,29791,32768,35937,39304,42875,46656,50653,
7
+ 54872,59319,64000]
8
+
9
+ it "returns true for perfect cubes" do
10
+ CUBES.each{|n| n.should be_cube}
11
+ end
12
+
13
+ it "returns true for negative perfect cubes" do
14
+ CUBES.shuffle.first(10).each{|n| (-n).should be_cube}
15
+ end
16
+
17
+ it "returns false for non-perfect cubes" do
18
+ ((0..CUBES.last).to_a - CUBES).shuffle.first(10).each{|n| n.should_not be_cube}
19
+ end
20
+
21
+ it "returns false for negative non-perfect cubes" do
22
+ ((0..CUBES.last).to_a - CUBES).shuffle.first(10).each{|n| (-n).should_not be_cube}
23
+ end
24
+ end
@@ -0,0 +1,16 @@
1
+ describe Integer, "#decagonal?" do
2
+ # A001107
3
+ DECAGONAL = [0,1,10,27,52,85,126,175,232,297,370,451,540,637,
4
+ 742,855,976,1105,1242,1387,1540,1701,1870,2047,
5
+ 2232,2425,2626,2835,3052,3277,3510,3751,4000,4257,
6
+ 4522,4795,5076,5365,5662,5967,6280,6601,6930,7267,
7
+ 7612,7965,8326]
8
+
9
+ it "returns true for a decagonal number" do
10
+ DECAGONAL.each{|n| n.should be_decagonal}
11
+ end
12
+
13
+ it "returns false for a non-decagonal number" do
14
+ ((0..DECAGONAL.last).to_a - DECAGONAL).each{|n| n.should_not be_decagonal}
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ describe Integer, "#dodecagonal?" do
2
+
3
+ # A051624
4
+ DODECAGONAL = [0,1,12,33,64,105,156,217,288,369,460,561,672,793,
5
+ 924,1065,1216,1377,1548,1729,1920,2121,2332,2553,
6
+ 2784,3025,3276,3537,3808,4089,4380,4681,4992,5313,
7
+ 5644,5985,6336,6697,7068,7449,7840,8241,8652]
8
+
9
+ it "returns true for dodecagonal numbers" do
10
+ DODECAGONAL.each{|n| n.should be_dodecagonal}
11
+ end
12
+
13
+ it "returns false for non-dodecagonal numbers" do
14
+ ((0..DODECAGONAL.last).to_a - DODECAGONAL).each do |n|
15
+ n.should_not be_dodecagonal
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ describe Integer, "#heptagonal?" do
2
+ HEPTAGONAL = [0,1,7,18,34,55,81,112,148,189,235,286,342,403,
3
+ 469,540,616,697,783,874,970,1071,1177,1288,1404,
4
+ 1525,1651,1782,1918,2059,2205,2356,2512,2673,2839,
5
+ 3010,3186,3367,3553,3744,3940,4141,4347,4558,4774,
6
+ 4995,5221,5452,5688]
7
+
8
+ it "returns true for a heptagonal number" do
9
+ HEPTAGONAL.each{|n| n.should be_heptagonal}
10
+ end
11
+
12
+ it "returns false for a non-heptagonal number" do
13
+ ((0..HEPTAGONAL.last).to_a - HEPTAGONAL).each{|n| n.should_not be_heptagonal}
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ describe Integer, "#hexagonal?" do
2
+ # A000384
3
+ HEXAGONAL = [0,1,6,15,28,45,66,91,120,153,190,231,276,325,378,
4
+ 435,496,561,630,703,780,861,946,1035,1128,1225,
5
+ 1326,1431,1540,1653,1770,1891,2016,2145,2278,2415,
6
+ 2556,2701,2850,3003,3160,3321,3486,3655,3828,4005,
7
+ 4186,4371,4560]
8
+
9
+ it "returns true for a hexagonal number" do
10
+ HEXAGONAL.each{|n| n.should be_hexagonal}
11
+ end
12
+
13
+ it "returns false for a non-hexagonal number" do
14
+ ((0..HEXAGONAL.last).to_a - HEXAGONAL).each{|n| n.should_not be_hexagonal}
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ describe Integer, "#myriagonal?" do
2
+
3
+ MYRIAGONAL = [0, 1, 10000, 29997, 59992, 99985, 149976, 209965, 279952,
4
+ 359937, 449920, 549901, 659880, 779857, 909832, 1049805,
5
+ 1199776, 1359745, 1529712, 1709677, 1899640, 2099601]
6
+
7
+ it "should return true for myriagonal numbers" do
8
+ MYRIAGONAL.shuffle.first(2).each{|n| n.should be_myriagonal}
9
+ end
10
+
11
+ it "should return false for non-myriagonal numbers" do
12
+ ((0..MYRIAGONAL.last).to_a - MYRIAGONAL).shuffle.first(2).each do |n|
13
+ n.should_not be_myriagonal
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ describe Integer, "#octagonal?" do
2
+ #A000567
3
+ OCTAGONAL = [0,1,8,21,40,65,96,133,176,225,280,341,408,481,
4
+ 560,645,736,833,936,1045,1160,1281,1408,1541,1680,
5
+ 1825,1976,2133,2296,2465,2640,2821,3008,3201,3400,
6
+ 3605,3816,4033,4256,4485,4720,4961,5208,5461]
7
+
8
+ it "returns true for octagonal numbers" do
9
+ OCTAGONAL.each{|n| n.should be_octagonal}
10
+ end
11
+
12
+ it "returns false for non-octagonal numbers" do
13
+ ((0..OCTAGONAL.last).to_a - OCTAGONAL).each{|n| n.should_not be_octagonal}
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ describe Integer, "#pentagonal?" do
2
+ PENTAGONAL = [0,1,5,12,22,35,51,70,92,117,145,176,210,247,287,
3
+ 330,376,425,477,532,590,651,715,782,852,925,1001,
4
+ 1080,1162,1247,1335,1426,1520,1617,1717,1820,1926,
5
+ 2035,2147,2262,2380,2501,2625,2752,2882,3015,3151]
6
+
7
+ it "returns true for pentagonal numbers" do
8
+ PENTAGONAL.each{|n| n.should be_pentagonal}
9
+ end
10
+
11
+ it "returns false for non-pentagonal numbers" do
12
+ ((0..PENTAGONAL.last).to_a - PENTAGONAL).each{|n| n.should_not be_pentagonal}
13
+ end
14
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: numb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.20.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Run Paint Run Run
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-04 00:00:00 +00:00
12
+ date: 2010-01-05 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -43,10 +43,14 @@ files:
43
43
  - lib/numb/achilles.rb
44
44
  - lib/numb/automorphic.rb
45
45
  - lib/numb/balanced_prime.rb
46
+ - lib/numb/carmichael.rb
46
47
  - lib/numb/carol.rb
47
48
  - lib/numb/composite.rb
49
+ - lib/numb/cube.rb
50
+ - lib/numb/decagonal.rb
48
51
  - lib/numb/deficient.rb
49
52
  - lib/numb/dihedral_prime.rb
53
+ - lib/numb/dodecagonal.rb
50
54
  - lib/numb/dudeney.rb
51
55
  - lib/numb/economical.rb
52
56
  - lib/numb/emrip.rb
@@ -56,6 +60,8 @@ files:
56
60
  - lib/numb/frugal.rb
57
61
  - lib/numb/happy.rb
58
62
  - lib/numb/harshad.rb
63
+ - lib/numb/heptagonal.rb
64
+ - lib/numb/hexagonal.rb
59
65
  - lib/numb/hilbert.rb
60
66
  - lib/numb/hyperperfect.rb
61
67
  - lib/numb/impolite.rb
@@ -66,10 +72,14 @@ files:
66
72
  - lib/numb/lucas_carmichael.rb
67
73
  - lib/numb/mms_pair.rb
68
74
  - lib/numb/mobius.rb
75
+ - lib/numb/myriagonal.rb
76
+ - lib/numb/n_gonal.rb
69
77
  - lib/numb/narcissistic.rb
70
78
  - lib/numb/nivenmorphic.rb
79
+ - lib/numb/octagonal.rb
71
80
  - lib/numb/ordinal.rb
72
81
  - lib/numb/parasitic.rb
82
+ - lib/numb/pentagonal.rb
73
83
  - lib/numb/perfect.rb
74
84
  - lib/numb/perfect_power.rb
75
85
  - lib/numb/polite.rb
@@ -101,11 +111,15 @@ files:
101
111
  - spec/achilles_spec.rb
102
112
  - spec/automorphic_spec.rb
103
113
  - spec/balanced_prime_spec.rb
114
+ - spec/carmichael_spec.rb
104
115
  - spec/carol_spec.rb
105
116
  - spec/composite_spec.rb
117
+ - spec/cube_spec.rb
118
+ - spec/decagonal_spec.rb
106
119
  - spec/deficient_spec.rb
107
120
  - spec/digital_sum_spec.rb
108
121
  - spec/dihedral_prime_spec.rb
122
+ - spec/dodecagonal_spec.rb
109
123
  - spec/dudeney_spec.rb
110
124
  - spec/economical_spec.rb
111
125
  - spec/emrip_spec.rb
@@ -116,6 +130,8 @@ files:
116
130
  - spec/frugal_spec.rb
117
131
  - spec/happy_spec.rb
118
132
  - spec/harshad_spec.rb
133
+ - spec/heptagonal_spec.rb
134
+ - spec/hexagonal_spec.rb
119
135
  - spec/hilbert_spec.rb
120
136
  - spec/hyperperfect_spec.rb
121
137
  - spec/kaprekar_spec.rb
@@ -125,12 +141,15 @@ files:
125
141
  - spec/lucas_spec.rb
126
142
  - spec/maris_mcgwire_sosa_pair_spec.rb
127
143
  - spec/mobius_spec.rb
144
+ - spec/myriagonal_spec.rb
128
145
  - spec/narcissistic_spec.rb
129
146
  - spec/nivenmorphic_spec.rb
130
147
  - spec/number_of_distinct_prime_factors_spec.rb
131
148
  - spec/number_of_prime_factors_spec.rb
149
+ - spec/octagonal_spec.rb
132
150
  - spec/ordinal_spec.rb
133
151
  - spec/parasitic_spec.rb
152
+ - spec/pentagonal_spec.rb
134
153
  - spec/perfect_power_spec.rb
135
154
  - spec/perfect_spec.rb
136
155
  - spec/polite_spec.rb
@@ -200,15 +219,19 @@ test_files:
200
219
  - spec/ordinal_spec.rb
201
220
  - spec/perfect_spec.rb
202
221
  - spec/perfect_power_spec.rb
222
+ - spec/carmichael_spec.rb
203
223
  - spec/polite_spec.rb
204
224
  - spec/automorphic_spec.rb
205
225
  - spec/hyperperfect_spec.rb
206
226
  - spec/spec_helper.rb
227
+ - spec/heptagonal_spec.rb
207
228
  - spec/kynea_spec.rb
208
229
  - spec/factor_spec.rb
209
230
  - spec/emrip_spec.rb
210
231
  - spec/smith_spec.rb
232
+ - spec/octagonal_spec.rb
211
233
  - spec/vampire_spec.rb
234
+ - spec/dodecagonal_spec.rb
212
235
  - spec/dihedral_prime_spec.rb
213
236
  - spec/sphenic_spec.rb
214
237
  - spec/lucas_carmichael_spec.rb
@@ -222,13 +245,16 @@ test_files:
222
245
  - spec/semiprime_spec.rb
223
246
  - spec/deficient_spec.rb
224
247
  - spec/happy_spec.rb
248
+ - spec/hexagonal_spec.rb
225
249
  - spec/triangular_spec.rb
226
250
  - spec/semi_perfect_spec.rb
251
+ - spec/pentagonal_spec.rb
227
252
  - spec/smarandache_wellin_spec.rb
228
253
  - spec/maris_mcgwire_sosa_pair_spec.rb
229
254
  - spec/lucas_spec.rb
230
255
  - spec/trimorphic_spec.rb
231
256
  - spec/harshad_spec.rb
257
+ - spec/cube_spec.rb
232
258
  - spec/carol_spec.rb
233
259
  - spec/powerful_spec.rb
234
260
  - spec/weird_spec.rb
@@ -239,10 +265,12 @@ test_files:
239
265
  - spec/undulating_spec.rb
240
266
  - spec/extravagant_spec.rb
241
267
  - spec/self_descriptive_spec.rb
268
+ - spec/decagonal_spec.rb
242
269
  - spec/kaprekar_spec.rb
243
270
  - spec/hilbert_spec.rb
244
271
  - spec/keith_spec.rb
245
272
  - spec/narcissistic_spec.rb
273
+ - spec/myriagonal_spec.rb
246
274
  - spec/achilles_spec.rb
247
275
  - spec/pronic_spec.rb
248
276
  - spec/politeness_spec.rb