numb 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. data/.document +5 -0
  2. data/.gitignore +5 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +10 -0
  5. data/Rakefile +52 -0
  6. data/VERSION +1 -0
  7. data/lib/numb/abundant.rb +20 -0
  8. data/lib/numb/achilles.rb +15 -0
  9. data/lib/numb/automorphic.rb +24 -0
  10. data/lib/numb/carol.rb +21 -0
  11. data/lib/numb/deficient.rb +19 -0
  12. data/lib/numb/dihedral_prime.rb +25 -0
  13. data/lib/numb/dudeney.rb +15 -0
  14. data/lib/numb/economical.rb +16 -0
  15. data/lib/numb/emrip.rb +17 -0
  16. data/lib/numb/equidigital.rb +18 -0
  17. data/lib/numb/extravagant.rb +18 -0
  18. data/lib/numb/factorion.rb +15 -0
  19. data/lib/numb/frugal.rb +16 -0
  20. data/lib/numb/happy.rb +15 -0
  21. data/lib/numb/harshad.rb +8 -0
  22. data/lib/numb/hilbert.rb +7 -0
  23. data/lib/numb/hyperperfect.rb +7 -0
  24. data/lib/numb/impolite.rb +6 -0
  25. data/lib/numb/kaprekar.rb +14 -0
  26. data/lib/numb/keith.rb +15 -0
  27. data/lib/numb/kynea.rb +9 -0
  28. data/lib/numb/mms_pair.rb +11 -0
  29. data/lib/numb/mobius.rb +11 -0
  30. data/lib/numb/narcissistic.rb +8 -0
  31. data/lib/numb/nivenmorphic.rb +9 -0
  32. data/lib/numb/ordinal.rb +11 -0
  33. data/lib/numb/parasitic.rb +10 -0
  34. data/lib/numb/perfect.rb +10 -0
  35. data/lib/numb/perfect_power.rb +9 -0
  36. data/lib/numb/polite.rb +7 -0
  37. data/lib/numb/polydivisible.rb +10 -0
  38. data/lib/numb/powerful.rb +8 -0
  39. data/lib/numb/practical.rb +26 -0
  40. data/lib/numb/prime.rb +495 -0
  41. data/lib/numb/ruby1.8.rb +9 -0
  42. data/lib/numb/ruby1.9.rb +8 -0
  43. data/lib/numb/self.rb +13 -0
  44. data/lib/numb/self_descriptive.rb +9 -0
  45. data/lib/numb/semi_perfect.rb +15 -0
  46. data/lib/numb/semiprime.rb +6 -0
  47. data/lib/numb/smarandache_wellin.rb +11 -0
  48. data/lib/numb/smith.rb +7 -0
  49. data/lib/numb/sphenic.rb +8 -0
  50. data/lib/numb/square.rb +8 -0
  51. data/lib/numb/square_free.rb +6 -0
  52. data/lib/numb/triangular.rb +8 -0
  53. data/lib/numb/trimorphic.rb +6 -0
  54. data/lib/numb/undulating.rb +11 -0
  55. data/lib/numb/vampire.rb +11 -0
  56. data/lib/numb/weird.rb +8 -0
  57. data/lib/numb.rb +80 -0
  58. data/numb.gemspec +201 -0
  59. data/spec/abundant_spec.rb +24 -0
  60. data/spec/achilles_spec.rb +24 -0
  61. data/spec/automorphic_spec.rb +208 -0
  62. data/spec/carol_spec.rb +18 -0
  63. data/spec/deficient_spec.rb +23 -0
  64. data/spec/digital_sum_spec.rb +8 -0
  65. data/spec/dihedral_prime_spec.rb +38 -0
  66. data/spec/dudeney_spec.rb +14 -0
  67. data/spec/economical_spec.rb +28 -0
  68. data/spec/emrip_spec.rb +26 -0
  69. data/spec/equidigital_spec.rb +27 -0
  70. data/spec/extravagant_spec.rb +20 -0
  71. data/spec/factor_spec.rb +22 -0
  72. data/spec/factorion_spec.rb +13 -0
  73. data/spec/frugal_spec.rb +20 -0
  74. data/spec/happy_spec.rb +24 -0
  75. data/spec/harshad_spec.rb +25 -0
  76. data/spec/hilbert_spec.rb +24 -0
  77. data/spec/hyperperfect_spec.rb +59 -0
  78. data/spec/kaprekar_spec.rb +25 -0
  79. data/spec/keith_spec.rb +21 -0
  80. data/spec/kynea_spec.rb +19 -0
  81. data/spec/maris_mcgwire_sosa_pair_spec.rb +20 -0
  82. data/spec/mobius_spec.rb +52 -0
  83. data/spec/narcissistic_spec.rb +18 -0
  84. data/spec/nivenmorphic_spec.rb +24 -0
  85. data/spec/number_of_distinct_prime_factors_spec.rb +35 -0
  86. data/spec/number_of_prime_factors_spec.rb +32 -0
  87. data/spec/ordinal_spec.rb +64 -0
  88. data/spec/parasitic_spec.rb +29 -0
  89. data/spec/perfect_power_spec.rb +24 -0
  90. data/spec/perfect_spec.rb +103 -0
  91. data/spec/polite_spec.rb +14 -0
  92. data/spec/politeness_spec.rb +11 -0
  93. data/spec/polydivisible_spec.rb +19 -0
  94. data/spec/powerful_spec.rb +25 -0
  95. data/spec/practical_spec.rb +30 -0
  96. data/spec/self_descriptive_spec.rb +19 -0
  97. data/spec/self_spec.rb +20 -0
  98. data/spec/semi_perfect_spec.rb +24 -0
  99. data/spec/semiprime_spec.rb +21 -0
  100. data/spec/smarandache_wellin_spec.rb +26 -0
  101. data/spec/smith_spec.rb +19 -0
  102. data/spec/spec_helper.rb +9 -0
  103. data/spec/sphenic_spec.rb +20 -0
  104. data/spec/square_free_spec.rb +19 -0
  105. data/spec/square_spec.rb +24 -0
  106. data/spec/triangular_spec.rb +19 -0
  107. data/spec/trimorphic_spec.rb +25 -0
  108. data/spec/undulating_spec.rb +20 -0
  109. data/spec/vampire_spec.rb +19 -0
  110. data/spec/weird_spec.rb +24 -0
  111. metadata +216 -0
data/lib/numb.rb ADDED
@@ -0,0 +1,80 @@
1
+ # coding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'backports'
5
+
6
+ libs = %w{abundant achilles automorphic carol deficient dihedral_prime dudeney
7
+ economical emrip equidigital extravagant factorion frugal happy
8
+ harshad hilbert hyperperfect impolite kaprekar keith kynea mms_pair mobius
9
+ narcissistic nivenmorphic ordinal parasitic perfect perfect_power polite
10
+ polydivisible powerful practical self self_descriptive semi_perfect semiprime
11
+ smarandache_wellin smith sphenic square square_free triangular trimorphic
12
+ undulating vampire weird
13
+ }
14
+
15
+ class Integer
16
+
17
+ def number_of_distinct_prime_factors
18
+ prime_factors.uniq.size
19
+ end
20
+ alias :omega :number_of_distinct_prime_factors
21
+
22
+ def number_of_prime_factors
23
+ prime_factors.size
24
+ end
25
+ alias :bigomega :number_of_prime_factors
26
+
27
+ def prime_factors
28
+ return [] if zero?
29
+ prime_division.map{|pair| [pair.first] * pair.last}.flatten
30
+ end
31
+
32
+ def politeness
33
+ positive_divisors.select{|d| d > 1}.select{|d| d.odd?}.size
34
+ end
35
+
36
+ def proper_positive_divisors
37
+ positive_divisors.reject {|d| d == self }
38
+ end
39
+
40
+ def positive_divisors
41
+ return [] unless self >= 0
42
+ (1..Math.sqrt(self).floor).select { |n| (self % n).zero? }.
43
+ map {|n| [n, self/n]}.flatten.uniq
44
+ end
45
+
46
+ def sum_of_divisors
47
+ positive_divisors.reduce(:+)
48
+ end
49
+
50
+ def digital_root
51
+ self == 0 ? 0 : 1 + ((self - 1) % 9)
52
+ end
53
+
54
+ def digital_sum
55
+ digits.reduce(:+)
56
+ end
57
+ alias :sum_of_digits :digital_sum
58
+ alias :sod :digital_sum
59
+
60
+ def digits
61
+ self.to_s.split(//).map{|d| d.to_i}
62
+ end
63
+
64
+ def factor?(n)
65
+ return false if n == 0
66
+ (self % n) == 0
67
+ end
68
+ end
69
+
70
+ begin
71
+ require 'prime'
72
+ libs << 'ruby1.9'
73
+ rescue LoadError
74
+ libs.unshift('ruby1.8','prime')
75
+ end
76
+
77
+ libs.each do |predicate|
78
+ require_relative "numb/#{predicate}"
79
+ end
80
+
data/numb.gemspec ADDED
@@ -0,0 +1,201 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{numb}
8
+ s.version = "0.3.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Run Paint Run Run"]
12
+ s.date = %q{2009-12-31}
13
+ s.email = %q{runrun@runpaint.org}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/numb.rb",
26
+ "lib/numb/abundant.rb",
27
+ "lib/numb/achilles.rb",
28
+ "lib/numb/automorphic.rb",
29
+ "lib/numb/carol.rb",
30
+ "lib/numb/deficient.rb",
31
+ "lib/numb/dihedral_prime.rb",
32
+ "lib/numb/dudeney.rb",
33
+ "lib/numb/economical.rb",
34
+ "lib/numb/emrip.rb",
35
+ "lib/numb/equidigital.rb",
36
+ "lib/numb/extravagant.rb",
37
+ "lib/numb/factorion.rb",
38
+ "lib/numb/frugal.rb",
39
+ "lib/numb/happy.rb",
40
+ "lib/numb/harshad.rb",
41
+ "lib/numb/hilbert.rb",
42
+ "lib/numb/hyperperfect.rb",
43
+ "lib/numb/impolite.rb",
44
+ "lib/numb/kaprekar.rb",
45
+ "lib/numb/keith.rb",
46
+ "lib/numb/kynea.rb",
47
+ "lib/numb/mms_pair.rb",
48
+ "lib/numb/mobius.rb",
49
+ "lib/numb/narcissistic.rb",
50
+ "lib/numb/nivenmorphic.rb",
51
+ "lib/numb/ordinal.rb",
52
+ "lib/numb/parasitic.rb",
53
+ "lib/numb/perfect.rb",
54
+ "lib/numb/perfect_power.rb",
55
+ "lib/numb/polite.rb",
56
+ "lib/numb/polydivisible.rb",
57
+ "lib/numb/powerful.rb",
58
+ "lib/numb/practical.rb",
59
+ "lib/numb/prime.rb",
60
+ "lib/numb/ruby1.8.rb",
61
+ "lib/numb/ruby1.9.rb",
62
+ "lib/numb/self.rb",
63
+ "lib/numb/self_descriptive.rb",
64
+ "lib/numb/semi_perfect.rb",
65
+ "lib/numb/semiprime.rb",
66
+ "lib/numb/smarandache_wellin.rb",
67
+ "lib/numb/smith.rb",
68
+ "lib/numb/sphenic.rb",
69
+ "lib/numb/square.rb",
70
+ "lib/numb/square_free.rb",
71
+ "lib/numb/triangular.rb",
72
+ "lib/numb/trimorphic.rb",
73
+ "lib/numb/undulating.rb",
74
+ "lib/numb/vampire.rb",
75
+ "lib/numb/weird.rb",
76
+ "numb.gemspec",
77
+ "spec/abundant_spec.rb",
78
+ "spec/achilles_spec.rb",
79
+ "spec/automorphic_spec.rb",
80
+ "spec/carol_spec.rb",
81
+ "spec/deficient_spec.rb",
82
+ "spec/digital_sum_spec.rb",
83
+ "spec/dihedral_prime_spec.rb",
84
+ "spec/dudeney_spec.rb",
85
+ "spec/economical_spec.rb",
86
+ "spec/emrip_spec.rb",
87
+ "spec/equidigital_spec.rb",
88
+ "spec/extravagant_spec.rb",
89
+ "spec/factor_spec.rb",
90
+ "spec/factorion_spec.rb",
91
+ "spec/frugal_spec.rb",
92
+ "spec/happy_spec.rb",
93
+ "spec/harshad_spec.rb",
94
+ "spec/hilbert_spec.rb",
95
+ "spec/hyperperfect_spec.rb",
96
+ "spec/kaprekar_spec.rb",
97
+ "spec/keith_spec.rb",
98
+ "spec/kynea_spec.rb",
99
+ "spec/maris_mcgwire_sosa_pair_spec.rb",
100
+ "spec/mobius_spec.rb",
101
+ "spec/narcissistic_spec.rb",
102
+ "spec/nivenmorphic_spec.rb",
103
+ "spec/number_of_distinct_prime_factors_spec.rb",
104
+ "spec/number_of_prime_factors_spec.rb",
105
+ "spec/ordinal_spec.rb",
106
+ "spec/parasitic_spec.rb",
107
+ "spec/perfect_power_spec.rb",
108
+ "spec/perfect_spec.rb",
109
+ "spec/polite_spec.rb",
110
+ "spec/politeness_spec.rb",
111
+ "spec/polydivisible_spec.rb",
112
+ "spec/powerful_spec.rb",
113
+ "spec/practical_spec.rb",
114
+ "spec/self_descriptive_spec.rb",
115
+ "spec/self_spec.rb",
116
+ "spec/semi_perfect_spec.rb",
117
+ "spec/semiprime_spec.rb",
118
+ "spec/smarandache_wellin_spec.rb",
119
+ "spec/smith_spec.rb",
120
+ "spec/spec_helper.rb",
121
+ "spec/sphenic_spec.rb",
122
+ "spec/square_free_spec.rb",
123
+ "spec/square_spec.rb",
124
+ "spec/triangular_spec.rb",
125
+ "spec/trimorphic_spec.rb",
126
+ "spec/undulating_spec.rb",
127
+ "spec/vampire_spec.rb",
128
+ "spec/weird_spec.rb"
129
+ ]
130
+ s.homepage = %q{http://github.com/runpaint/numb}
131
+ s.rdoc_options = ["--charset=UTF-8"]
132
+ s.require_paths = ["lib"]
133
+ s.required_ruby_version = Gem::Requirement.new(">= 1.9.1")
134
+ s.rubygems_version = %q{1.3.5}
135
+ s.summary = %q{Experiments in number theory with new predicate methods for Integer.}
136
+ s.test_files = [
137
+ "spec/economical_spec.rb",
138
+ "spec/polydivisible_spec.rb",
139
+ "spec/equidigital_spec.rb",
140
+ "spec/nivenmorphic_spec.rb",
141
+ "spec/dudeney_spec.rb",
142
+ "spec/square_free_spec.rb",
143
+ "spec/abundant_spec.rb",
144
+ "spec/self_spec.rb",
145
+ "spec/square_spec.rb",
146
+ "spec/ordinal_spec.rb",
147
+ "spec/perfect_spec.rb",
148
+ "spec/perfect_power_spec.rb",
149
+ "spec/polite_spec.rb",
150
+ "spec/automorphic_spec.rb",
151
+ "spec/hyperperfect_spec.rb",
152
+ "spec/spec_helper.rb",
153
+ "spec/kynea_spec.rb",
154
+ "spec/factor_spec.rb",
155
+ "spec/emrip_spec.rb",
156
+ "spec/smith_spec.rb",
157
+ "spec/vampire_spec.rb",
158
+ "spec/dihedral_prime_spec.rb",
159
+ "spec/sphenic_spec.rb",
160
+ "spec/practical_spec.rb",
161
+ "spec/parasitic_spec.rb",
162
+ "spec/number_of_prime_factors_spec.rb",
163
+ "spec/frugal_spec.rb",
164
+ "spec/semiprime_spec.rb",
165
+ "spec/deficient_spec.rb",
166
+ "spec/happy_spec.rb",
167
+ "spec/triangular_spec.rb",
168
+ "spec/semi_perfect_spec.rb",
169
+ "spec/smarandache_wellin_spec.rb",
170
+ "spec/maris_mcgwire_sosa_pair_spec.rb",
171
+ "spec/trimorphic_spec.rb",
172
+ "spec/harshad_spec.rb",
173
+ "spec/carol_spec.rb",
174
+ "spec/powerful_spec.rb",
175
+ "spec/weird_spec.rb",
176
+ "spec/number_of_distinct_prime_factors_spec.rb",
177
+ "spec/digital_sum_spec.rb",
178
+ "spec/factorion_spec.rb",
179
+ "spec/undulating_spec.rb",
180
+ "spec/extravagant_spec.rb",
181
+ "spec/self_descriptive_spec.rb",
182
+ "spec/kaprekar_spec.rb",
183
+ "spec/hilbert_spec.rb",
184
+ "spec/keith_spec.rb",
185
+ "spec/narcissistic_spec.rb",
186
+ "spec/achilles_spec.rb",
187
+ "spec/politeness_spec.rb",
188
+ "spec/mobius_spec.rb"
189
+ ]
190
+
191
+ if s.respond_to? :specification_version then
192
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
193
+ s.specification_version = 3
194
+
195
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
196
+ else
197
+ end
198
+ else
199
+ end
200
+ end
201
+
@@ -0,0 +1,24 @@
1
+ describe Integer, "#abundant?" do
2
+ ABUNDANT = [12,18,20,24,30,36,40,42,48,54,56,60,66,70,72,78,
3
+ 80,84,88,90,96,100,102,104,108,112,114,120,126,
4
+ 132,138,140,144,150,156,160,162,168,174,176,180,
5
+ 186,192,196,198,200,204,208,210,216,220,222,224,
6
+ 228,234,240,246,252,258,260,264,270]
7
+ it "returns true if the number is abundant" do
8
+ ABUNDANT.each do |number|
9
+ number.should be_abundant
10
+ end
11
+ end
12
+
13
+ it "returns false if the number is not abundant" do
14
+ ((1..270).to_a - ABUNDANT).each do |number|
15
+ number.should_not be_abundant
16
+ end
17
+ end
18
+
19
+ it "returns false if the number is negative" do
20
+ (1..270).each do |number|
21
+ (-number).should_not be_abundant
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ describe Integer, "achilles?" do
2
+ ACHILLES = [72,108,200,288,392,432,500,648,675,800,864,968,
3
+ 972,1125,1152,1323,1352,1372,1568,1800,1944,2000,
4
+ 2312,2592,2700,2888,3087,3200,3267,3456,3528,3872,
5
+ 3888,4000,4232,4500,4563,4608,5000,5292,5324,5400,
6
+ 5408,5488]
7
+ it "returns true for Achilles numbers" do
8
+ ACHILLES.each do |number|
9
+ number.should be_achilles
10
+ end
11
+ end
12
+
13
+ it "returns false for non-Achilles numbers" do
14
+ ((1..5487).to_a - ACHILLES).sample(10).each do |number|
15
+ number.should_not be_achilles
16
+ end
17
+ end
18
+
19
+ it "returns false for negative numbers" do
20
+ (1..5487).each do |number|
21
+ (-number).should_not be_achilles
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,208 @@
1
+ describe Integer, "#automorphic?" do
2
+
3
+ AUTOMORPHIC = (
4
+ # http://www.research.att.com/~njas/sequences/A016090
5
+ [6,76,376,9376,9376,109376,7109376,87109376,
6
+ 787109376,1787109376,81787109376,81787109376,
7
+ 81787109376,40081787109376,740081787109376,
8
+ 3740081787109376,43740081787109376,
9
+ 743740081787109376] +
10
+ # http://www.research.att.com/~njas/sequences/A007185
11
+ [5,25,625,625,90625,890625,2890625,12890625,
12
+ 212890625,8212890625,18212890625,918212890625,
13
+ 9918212890625,59918212890625,259918212890625,
14
+ 6259918212890625,56259918212890625,
15
+ 256259918212890625] +
16
+ # http://www.research.att.com/~njas/sequences/A003226
17
+ [0,1,5,6,25,76,376,625,9376,90625,109376,890625,
18
+ 2890625,7109376,12890625,87109376,212890625,
19
+ 787109376,1787109376,8212890625,18212890625,
20
+ 81787109376,918212890625,9918212890625,
21
+ 40081787109376,59918212890625]
22
+ ).uniq
23
+
24
+ it "returns true for automorphic numbers" do
25
+ AUTOMORPHIC.each do |n|
26
+ n.should be_automorphic
27
+ end
28
+ end
29
+
30
+ it "returns false for negative automorphic numbers" do
31
+ (AUTOMORPHIC - [0]).each do |n|
32
+ (-n).should_not be_automorphic
33
+ end
34
+ end
35
+
36
+ it "returns false for non-automorphic numbers" do
37
+ [2,4,626, 100228].each do |n|
38
+ n.should_not be_automorphic
39
+ end
40
+ end
41
+
42
+ it "returns false for negative numbers" do
43
+ [2,4,626, 100228].each do |n|
44
+ (-n).should_not be_automorphic
45
+ end
46
+ end
47
+
48
+ it "accepts an optional argument for n" do
49
+ lambda { 3.automorphic?(2) }.should_not raise_error(ArgumentError)
50
+ end
51
+
52
+ it "returns true if the number is 2-automorphic and 2 is given as an argument" do
53
+ # http://www.research.att.com/~njas/sequences/A030984
54
+ [8,88,688,4688,54688,554688,3554688,93554688,
55
+ 893554688,893554688,40893554688,40893554688,
56
+ 40893554688,70040893554688,870040893554688,
57
+ 1870040893554688,71870040893554688,
58
+ 871870040893554688].each do |number|
59
+ number.automorphic?(2).should be_true
60
+ end
61
+ end
62
+
63
+ it "returns false if the number is not 2-automorphic and 2 is given as an argument" do
64
+ [6, 13, 93, 552, 5003, 2827220126666].each do |number|
65
+ number.automorphic?(2).should be_false
66
+ end
67
+ end
68
+
69
+ it "returns true if the number is 3-automorphic and 3 is given as an argument" do
70
+ (
71
+ # http://www.research.att.com/~njas/sequences/A030986
72
+ [5,75,875,6875,96875,296875,4296875,4296875,
73
+ 404296875,9404296875,39404296875,639404296875,
74
+ 6639404296875,86639404296875,86639404296875,
75
+ 2086639404296875,52086639404296875] +
76
+ # http://www.research.att.com/~njas/sequences/A030985
77
+ [2,92,792,9792,69792,369792,2369792,62369792,
78
+ 262369792,7262369792,27262369792,27262369792,
79
+ 27262369792,80027262369792,580027262369792,
80
+ 4580027262369792,14580027262369792,
81
+ 914580027262369792]
82
+ ).each do |number|
83
+ number.automorphic?(3).should be_true
84
+ end
85
+ end
86
+
87
+ it "returns false if the number is not 3-automorphic and 3 is given as an argument" do
88
+ [6, 13, 93, 55, 5003, 2827220126666].each do |number|
89
+ number.automorphic?(3).should be_false
90
+ end
91
+ end
92
+
93
+ it "returns true if the number is 4-automorphic and 4 is given as an argument" do
94
+ # http://www.research.att.com/~njas/sequences/A030987
95
+ [4,44,344,7344,77344,777344,6777344,46777344,
96
+ 446777344,446777344,20446777344,20446777344,
97
+ 5020446777344,35020446777344,935020446777344,
98
+ 5935020446777344,35935020446777344].each do |number|
99
+ number.automorphic?(4).should be_true
100
+ end
101
+ end
102
+
103
+ it "returns false if the number is not 4-automorphic and 4 is given as an argument" do
104
+ [6, 14, 97, 55, 5000, 2827220126666].each do |number|
105
+ number.automorphic?(4).should be_false
106
+ end
107
+ end
108
+
109
+ it "returns true if the number is 5-automorphic and 5 is given as an argument" do
110
+ # http://www.research.att.com/~njas/sequences/A030988
111
+ [5,25,125,8125,78125,578125,2578125,42578125,
112
+ 642578125,3642578125,83642578125,983642578125,
113
+ 1983642578125,51983642578125,251983642578125,
114
+ 1251983642578125,51251983642578125].each do |number|
115
+ number.automorphic?(5).should be_true
116
+ end
117
+ end
118
+
119
+ it "returns false if the number is not 5-automorphic and 5 is given as an argument" do
120
+ [6, 97, 55, 5000, 2827220126666].each do |number|
121
+ number.automorphic?(5).should be_false
122
+ end
123
+ end
124
+
125
+ it "returns true if the number is 6-automorphic and 6 is given as an argument" do
126
+ # http://www.research.att.com/~njas/sequences/A030989
127
+ [6,96,896,4896,84896,184896,1184896,31184896,
128
+ 631184896,3631184896,13631184896,13631184896,
129
+ 13631184896,90013631184896,290013631184896,
130
+ 7290013631184896,57290013631184896,
131
+ 957290013631184896].each do |number|
132
+ number.automorphic?(6).should be_true
133
+ end
134
+ end
135
+
136
+ it "returns false if the number is not 6-automorphic and 6 is given as an argument" do
137
+ [5, 97, 5000, 2827220126666].each do |number|
138
+ number.automorphic?(6).should be_false
139
+ end
140
+ end
141
+
142
+ it "returns true if the number is 7-automorphic and 7 is given as an argument" do
143
+ (
144
+ # http://www.research.att.com/~njas/sequences/A030993
145
+ [3,43,143,7143,57143,857143,2857143,42857143,
146
+ 142857143,7142857143,57142857143,857142857143,
147
+ 2857142857143,42857142857143,142857142857143,
148
+ 7142857142857143,57142857142857143] +
149
+ # http://www.research.att.com/~njas/sequences/A030991
150
+ [5,75,375,4375,84375,984375,8984375,58984375,
151
+ 458984375,5458984375,45458984375,845458984375,
152
+ 2845458984375,22845458984375,322845458984375,
153
+ 2322845458984375,22322845458984375] +
154
+ # http://www.research.att.com/~njas/sequences/A030992
155
+ [8,68,768,2768,72768,872768,3872768,83872768,
156
+ 683872768,1683872768,11683872768,11683872768,
157
+ 11683872768,20011683872768,820011683872768,
158
+ 4820011683872768,34820011683872768,
159
+ 534820011683872768]
160
+ ).each do |number|
161
+ number.automorphic?(7).should be_true
162
+ end
163
+ end
164
+
165
+ it "returns false if the number is not 7-automorphic and 7 is given as an argument" do
166
+ [2,71,3263,71526675754123263].each do |number|
167
+ number.automorphic?(7).should be_false
168
+ end
169
+ end
170
+
171
+ it "returns true if the number is 8-automorphic and 8 is given as an argument" do
172
+ # http://www.research.att.com/~njas/sequences/A030993
173
+ [2,72,672,8672,88672,388672,3388672,23388672,
174
+ 223388672,223388672,10223388672,510223388672,
175
+ 7510223388672,67510223388672,967510223388672,
176
+ 7967510223388672,67967510223388672].each do |number|
177
+ number.automorphic?(8).should be_true
178
+ end
179
+ end
180
+
181
+ it "returns false if the number is not 8-automorphic and 8 is given as an argument" do
182
+ [3,71,3263,71526675754123263].each do |number|
183
+ number.automorphic?(8).should be_false
184
+ end
185
+ end
186
+
187
+ it "returns true if the number is 9-automorphic and 9 is given as an argument" do
188
+ # http://www.research.att.com/~njas/sequences/A030995
189
+ ([5,25,625,5625,65625,765625,4765625,34765625,
190
+ 134765625,3134765625,13134765625,213134765625,
191
+ 2213134765625,62213134765625,362213134765625,
192
+ 7362213134765625,17362213134765625] +
193
+ # http://www.research.att.com/~njas/sequences/A030994
194
+ [4,64,264,3264,23264,123264,4123264,54123264,
195
+ 754123264,5754123264,75754123264,675754123264,
196
+ 6675754123264,26675754123264,526675754123264,
197
+ 1526675754123264,71526675754123264]).each do |number|
198
+ number.automorphic?(9).should be_true
199
+ end
200
+ end
201
+
202
+ it "returns false if the number is not 9-automorphic and 9 is given as an argument" do
203
+ [6,65,200,3263,71526675754123263].each do |number|
204
+ number.automorphic?(9).should be_false
205
+ end
206
+ end
207
+ end
208
+
@@ -0,0 +1,18 @@
1
+ describe Integer, "#carol?" do
2
+ CAROL = [-1,7,47,223,959,3967,16127,65023,261119,1046527,
3
+ 4190207,16769023,67092479,268402687,1073676287,
4
+ 4294836223,17179607039,68718952447,274876858367,
5
+ 1099509530623,4398042316799,17592177655807]
6
+
7
+ it "returns true for Carol numbers" do
8
+ CAROL.each do |number|
9
+ number.should be_carol
10
+ end
11
+ end
12
+
13
+ it "returns false for non-Carol numbers" do
14
+ ((1..300).to_a - CAROL).each do |number|
15
+ number.should_not be_carol
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ describe Integer, "#deficient?" do
2
+ DEFICIENT = [1,2,3,4,5,7,8,9,10,11,13,14,15,16,17,19,21,22,23,
3
+ 25,26,27,29,31,32,33,34,35,37,38,39,41,43,44,45,
4
+ 46,47,49,50,51,52,53,55,57,58,59,61,62,63,64,65,
5
+ 67,68,69,71,73,74,75,76,77,79,81,82,83,85,86]
6
+ it "returns true for deficient numbers" do
7
+ DEFICIENT.each do |number|
8
+ number.should be_deficient
9
+ end
10
+ end
11
+
12
+ it "returns false for non-deficient numbers" do
13
+ ((1..86).to_a - DEFICIENT).each do |number|
14
+ number.should_not be_deficient
15
+ end
16
+ end
17
+
18
+ it "returns false for negative numbers" do
19
+ (1..86).each do |number|
20
+ (-number).should_not be_deficient
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,8 @@
1
+ describe Integer, "#digital_sum" do
2
+ it "returns the sum of the digits of self" do
3
+ 1.digital_sum.should == 1
4
+ 10.digital_sum.should == 1
5
+ 22.digital_sum.should == 4
6
+ 282761.digital_sum.should == 26
7
+ end
8
+ end
@@ -0,0 +1,38 @@
1
+ describe Integer, "#dihedral_prime?" do
2
+ # http://www.research.att.com/~njas/sequences/A038136
3
+ DIHEDRAL_PRIME = ([2,11,101,181,1181,1811,18181,108881,110881,
4
+ 118081,120121,121021,121151,150151,151051,151121,
5
+ 180181,180811,181081,188011,188801,1008001,
6
+ 1022201,1028011,1055501,1058011,1082801,1085801,
7
+ 1088081] +
8
+ # http://www.research.att.com/~njas/sequences/A134996
9
+ [2,5,11,101,181,1181,1811,18181,108881,110881,
10
+ 118081,120121,121021,121151,150151,151051,151121,
11
+ 180181,180811,181081,188011,188801,1008001,
12
+ 1022201,1028011,1055501,1058011,1082801,1085801,
13
+ 1088081]).uniq
14
+
15
+ it "returns true for Dihedral primes" do
16
+ DIHEDRAL_PRIME.each do |number|
17
+ number.should be_dihedral_prime
18
+ end
19
+ end
20
+
21
+ it "returns false for non-Dihedral primes" do
22
+ [6,12,4827290].each do |number|
23
+ number.should_not be_dihedral_prime
24
+ end
25
+ end
26
+
27
+ it "returns false for primes which aren't Dihedral" do
28
+ [3,17,19].each do |number|
29
+ number.should_not be_dihedral_prime
30
+ end
31
+ end
32
+
33
+ it "returns false for non-primes which are Dihedral" do
34
+ [8, 808, 1101].each do |number|
35
+ number.should_not be_dihedral_prime
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,14 @@
1
+ describe Integer, "#dudeney?" do
2
+ it "returns true for Dudeney numbers" do
3
+ # http://www.research.att.com/~njas/sequences/A061209
4
+ [1, 512, 4913, 5832, 17576, 19683].each do |number|
5
+ number.should be_dudeney
6
+ end
7
+ end
8
+
9
+ it "returns false for non-Dudeney numbers" do
10
+ [2, 511, 444444, 196833, 373627919182666355532718191].each do |number|
11
+ number.should_not be_dudeney
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ describe Integer, "#economical?" do
2
+ # http://www.research.att.com/~njas/sequences/A046759
3
+ FRUGAL = [125,128,243,256,343,512,625,729,1024,1029,1215,
4
+ 1250,1280,1331,1369,1458,1536,1681,1701,1715,1792,
5
+ 1849,1875,2048,2187,2197,2209,2401,2560,2809,3125,
6
+ 3481,3584,3645,3721,4096,4374,4375,4489,4802,
7
+ 4913]
8
+ # http://www.research.att.com/~njas/sequences/A046758
9
+ EQUIDIGITAL = [1,2,3,5,7,10,11,13,14,15,16,17,19,21,23,25,27,29,
10
+ 31,32,35,37,41,43,47,49,53,59,61,64,67,71,73,79,
11
+ 81,83,89,97,101,103,105,106,107,109,111,112,113,
12
+ 115,118,119,121,122,123,127,129,131,133,134,135,
13
+ 137,139]
14
+
15
+ ECONOMICAL = EQUIDIGITAL + FRUGAL
16
+
17
+ it "returns true for economical numbers" do
18
+ ECONOMICAL.each do |number|
19
+ number.should be_economical
20
+ end
21
+ end
22
+
23
+ it "returns false for non-economical numbers" do
24
+ (((1..139).to_a - EQUIDIGITAL - FRUGAL)).each do |number|
25
+ number.should_not be_economical
26
+ end
27
+ end
28
+ end