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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Run Paint Run Run
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,10 @@
1
+ = numb
2
+
3
+ Experiments in number theory by extending the Integer class with predicate
4
+ methods such as #perfect? and #weird?.
5
+
6
+ Requires Ruby 1.9.
7
+
8
+ == Copyright
9
+
10
+ Copyright (c) 2009 Run Paint Run Run. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ # coding: utf-8
2
+ require 'rubygems'
3
+ require 'rake'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "numb"
9
+ gem.summary = %Q{Experiments in number theory with new predicate methods for Integer.}
10
+ gem.email = "runrun@runpaint.org"
11
+ gem.homepage = "http://github.com/runpaint/numb"
12
+ gem.authors = ["Run Paint Run Run"]
13
+ gem.required_ruby_version = '>= 1.9.1'
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+
34
+ task :default => :spec
35
+
36
+ require 'rake/rdoctask'
37
+ Rake::RDocTask.new do |rdoc|
38
+ if File.exist?('VERSION.yml')
39
+ config = YAML.load(File.read('VERSION.yml'))
40
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
41
+ else
42
+ version = ""
43
+ end
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "numb #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ rdoc.options << '--line-numbers' << '--inline-source'
50
+ rdoc.options << '--charset' << 'utf-8'
51
+ end
52
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ # An abundant number is a number n for which σ(n) > 2n. That is, the sum of
4
+ # its divisors exceeds 2n. (See Integer#σ to compute the sum of the divisors
5
+ # of an arbitrary integer).
6
+ #
7
+ # Returns true if the number is abundant; false otherwise. Aliased to
8
+ # Integer#excessive?.
9
+ #
10
+ # 96.abundant? #=> true
11
+ # 100.abundant? #=> true
12
+ # 345.abundant? #=> false
13
+ #
14
+ def abundant?
15
+ return false unless self > 0
16
+ sum_of_divisors > (2 * self)
17
+ end
18
+
19
+ alias :excessive? :abundant?
20
+ end
@@ -0,0 +1,15 @@
1
+ # coding: utf-8
2
+ class Integer
3
+
4
+ # An Achilles number is powerful but not a perfect power.
5
+ #
6
+ # Returns true if self is an Achilles number; false otherwise.
7
+ #
8
+ # 1152.achilles? #=> true
9
+ # 4563.achilles? #=> true
10
+ # 100.achilles? #=> false
11
+ #
12
+ def achilles?
13
+ powerful? and not perfect_power?
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ class Integer
3
+
4
+ # An automorphic number is a number whose square terminates in the number
5
+ # itself. That is, k is automorphic if the final digits of k<sup>2</sup> are
6
+ # the digits of k.
7
+ #
8
+ # More generally, an <i>n</i>-automorphic number is one of the form
9
+ # <i>nk<sup>2</sup></i> which has its last digits equal to <i>k</i>.
10
+ # <i>n</i> may be supplied as an argument to this method; otherwise it
11
+ # defaults to 1.
12
+ #
13
+ # Returns true if the number is automorphic; false otherwise. Aliased to
14
+ # Integer#curious?.
15
+ #
16
+ # 25.automorphic? #=> true
17
+ # 9376.automorphic? #=> true
18
+ # 600.automorphic? #=> true
19
+ #
20
+ def automorphic?(n=1)
21
+ (n * self ** 2).to_s.end_with? self.to_s
22
+ end
23
+ alias :curious? :automorphic?
24
+ end
data/lib/numb/carol.rb ADDED
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+
3
+ class Integer
4
+
5
+ # A Carol number is an integer of the form <i>4<sup>n</sup> − 2<sup>n +
6
+ # 1</sup> − 1</i>.
7
+ #
8
+ # This method returns true if self is a Carol number; false otherwise.
9
+ #
10
+ # −1.carol? #=> true
11
+ # 959.carol? #=> true
12
+ # 32.carol? #=> false
13
+ #
14
+ def carol?
15
+ return true if self == 7
16
+ return true if self == -1
17
+ a, b = to_s(2).match(/^(1+)0(1+)$/).to_a[1..-1]
18
+ return false if (a.nil? or b.nil?)
19
+ b.length == (a.length + 3)
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ # A deficient number is a number n for which σ(n) < 2n. That is, the sum of
4
+ # its divisors are less than the number. (To calculate the sum of divisors
5
+ # for an arbitrary integer see Integer#σ).
6
+ #
7
+ # Returns true if the number is deficient; false otherwise.
8
+ #
9
+ # 8.deficient? #=> true
10
+ # 27.deficient? #=> true
11
+ # 6.deficient? #=> false
12
+ #
13
+ def deficient?
14
+ return false unless self > 0
15
+ sum_of_divisors < (2 * self)
16
+ end
17
+
18
+ alias :defective? :deficient?
19
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ class Integer
3
+
4
+ # A dihedral prime is a prime number that appears as itself or another prime
5
+ # when rendered on a seven-segment display of a calculator and...
6
+ #
7
+ # * Rotated 180°.
8
+ # * Mirrored.
9
+ # * Rotated 180° and mirrored.
10
+ #
11
+ # For example, 120121 is a dihedral prime. It is 121021 when rotated,
12
+ # 151051 (another prime) when mirrored, and 150151 when rotated and
13
+ # mirrored.
14
+ #
15
+ # Returns true if self is a dihedral prime; false otherwise.
16
+ #
17
+ # 101.dihedral_prime? #=> true
18
+ # 181.dihedral_prime? #=> true
19
+ # 7.dihedral_prime? #=> false
20
+ #
21
+ def dihedral_prime?
22
+ return false unless prime?
23
+ !!to_s.match(/^[01825][018253]*$/)
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ # A Dudeney number is a positive integer that is a perfect cube such that
4
+ # the sum of its decimal digits is the cube root of the number.
5
+ #
6
+ # Returns true if self is a Dudeney number; false otherwise.
7
+ #
8
+ # 4913.dudeney? #=> true
9
+ # 5832.dudeney? #=> true
10
+ # 98.dudeney? #=> false
11
+ #
12
+ def dudeney?
13
+ Math.cbrt(self) == self.digits.reduce(:+)
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ # A number which is either frugal or equidigital.
4
+ #
5
+ # Returns true if self is economical; false otherwise.
6
+ #
7
+ # See also Integer#equidigital? and Integer#frugal?.
8
+ #
9
+ # 243.economical? #=> true
10
+ # 7.economical? #=> true
11
+ # 989.economical? #=> false
12
+ #
13
+ def economical?
14
+ equidigital? or frugal?
15
+ end
16
+ end
data/lib/numb/emrip.rb ADDED
@@ -0,0 +1,17 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ # An emrip is a prime whose reversed digits give a different prime. For
4
+ # example, 17 is an emrip because 71 is also prime.
5
+ #
6
+ # Returns true if self is an emrip; false otherwise.
7
+ #
8
+ # 1009.emrip? #=> true
9
+ # 1193.emrip? #=> true
10
+ # 7.emrip? #=> false
11
+ #
12
+ def emrip?
13
+ return false unless prime?
14
+ reversed = to_s.reverse.to_i
15
+ reversed != self and reversed.prime?
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ # An equidigital number has the same number of digits as
4
+ # the number of digits in its prime factorization (including exponents).
5
+ #
6
+ # For example, 35 is equidigital because it has two digits and two 1-digit
7
+ # prime factors (5 and 7).
8
+ #
9
+ # Returns true if self is equidigital; false otherwise.
10
+ #
11
+ # 81.equidigital? #=> true
12
+ # 49.equidigital? #=> true
13
+ # 1287.equidigital? #=> false
14
+ #
15
+ def equidigital?
16
+ digits.size == prime_division.flatten.reject{|d|d==1}.join.to_i.digits.size
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ # An extravagant number has fewer digits than the number of digits in its
4
+ # prime factorization (including exponents).
5
+ #
6
+ # Returns true if self is extravagant; false otherwise. Aliased to
7
+ # Integer#wasteful?.
8
+ #
9
+ # 234.extravagant? #=> true
10
+ # 87.extravagant? #=> true
11
+ # 81.extravagant? #=> false
12
+ #
13
+ def extravagant?
14
+ digits.size < prime_division.flatten.reject{|d|d==1}.join.to_i.digits.size
15
+ end
16
+
17
+ alias :wasteful? :extravagant?
18
+ end
@@ -0,0 +1,15 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ # A factorion is a number equal to the sum of the factorials of its decimal
4
+ # digits.
5
+ #
6
+ # Returns true if self is a factorion; false otherwise.
7
+ #
8
+ # 145.factorion? #=> true
9
+ # 40585.factorion? #=> true
10
+ # 200.factorion? #=> false
11
+ #
12
+ def factorion?
13
+ [1, 2, 145, 40585].include? self
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ # coding: utf-8
2
+ class Integer
3
+
4
+ # A frugal number has more digits than the number of digits in its prime
5
+ # factorization (including exponents).
6
+ #
7
+ # Returns true if self is frugal; false otherwise.
8
+ #
9
+ # 128.frugal? #=> true
10
+ # 256.frugal? #=> true
11
+ # 300.frugal? #=> false
12
+ #
13
+ def frugal?
14
+ digits.size > prime_division.flatten.reject{|d|d==1}.join.to_i.digits.size
15
+ end
16
+ end
data/lib/numb/happy.rb ADDED
@@ -0,0 +1,15 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def happy?
4
+ return false unless self > 0
5
+ n = self
6
+ sad = '4 16 37 58 89 145 42 20'
7
+ seq = ""
8
+ loop do
9
+ n = n.digits.map{|d| d ** 2}.reduce(:+)
10
+ seq << n.to_s << ' '
11
+ return true if n == 1
12
+ return false if seq.include? sad
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,8 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def harshad?
4
+ return false unless self >= 0
5
+ (self % digital_sum) == 0
6
+ end
7
+ alias :niven? :harshad?
8
+ end
@@ -0,0 +1,7 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def hilbert?
4
+ return false unless self > 0
5
+ ((self - 1) % 4) == 0
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def hyperperfect?(k=1)
4
+ raise ArgumentError unless k >= 1
5
+ (1 + (k * (sum_of_divisors - self - 1))) == self
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def impolite?
4
+ not polite?
5
+ end
6
+ end
@@ -0,0 +1,14 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def kaprekar?
4
+ return true if self == 1
5
+ sdigits = (self ** 2).digits
6
+ (1..sdigits.size-1).each do |first|
7
+ a = sdigits.first(first).join.to_i
8
+ b = sdigits.last(sdigits.size-first).join.to_i
9
+ next if [a,b].any?{|n| n.zero?}
10
+ return true if (a + b) == self
11
+ end
12
+ false
13
+ end
14
+ end
data/lib/numb/keith.rb ADDED
@@ -0,0 +1,15 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def keith?
4
+ return false unless (n = self) > 9
5
+ terms = n.to_s.split(//).map{|d| d.to_i}
6
+ loop do
7
+ return true if (n = terms.reduce(:+)) == self
8
+ return false if n > self
9
+ terms.shift
10
+ terms << n
11
+ end
12
+ false
13
+ end
14
+ end
15
+
data/lib/numb/kynea.rb ADDED
@@ -0,0 +1,9 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def kynea?
4
+ return true if self == 7
5
+ a, b = to_s(2).match(/^(10+)(1+)$/).to_a[1..-1]
6
+ return false if (a.nil? or b.nil?)
7
+ a.length == (b.length - 1)
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def mms_pair?(other)
4
+ return false unless (self == other.succ) || (other == self.succ)
5
+ sum = [self,other].map do |n|
6
+ (n.digits + n.prime_factors.map{|p| p.digits}.flatten).reduce(:+)
7
+ end
8
+ sum.first == sum.last
9
+ end
10
+ alias :maris_mcgwire_sosa_pair? :mms_pair?
11
+ end
@@ -0,0 +1,11 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def mobius
4
+ case
5
+ when (self < 1) then nil
6
+ when (square_free? and number_of_distinct_prime_factors.even?) then 1
7
+ when (square_free? and number_of_distinct_prime_factors.odd?) then -1
8
+ when (not square_free?) then 0
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def narcissistic?
4
+ self == digits.map{|d| d ** digits.size}.reduce(:+)
5
+ end
6
+ alias :armstrong? :narcissistic?
7
+ alias :plus_perfect? :narcissistic?
8
+ end
@@ -0,0 +1,9 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def nivenmorphic?
4
+ return true if self == 0
5
+ return false unless self > 0
6
+ niven? && self.to_s.end_with?(digital_sum.to_s)
7
+ end
8
+ alias :harshadmorphic? :nivenmorphic?
9
+ end
@@ -0,0 +1,11 @@
1
+ class Integer
2
+ def ordinal
3
+ case to_s
4
+ when /^1\d$/ then 'th'
5
+ when /1$/ then 'st'
6
+ when /2$/ then 'nd'
7
+ when /3$/ then 'rd'
8
+ else 'th'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def parasitic?(n=nil)
4
+ return (1..9).any?{|x| parasitic?(x)} if n.nil?
5
+ return true if (n == 1 && self == 1)
6
+ return false unless self > 9
7
+ raise ArgumentError unless (n > 0 && n < 10)
8
+ (n*self) == [digits.last, digits[0..-2]].join.to_i
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def perfect?
4
+ return false if self < 6 or self.odd? or self.to_s !~ /(6|8)$/
5
+ return false if self != 6 and digital_root != 1
6
+ divisors = proper_positive_divisors
7
+ return false unless (divisors.size + 1).even?
8
+ divisors.reduce(:+) == self
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def perfect_power?
4
+ return false unless self > 0
5
+ return true if self == 1
6
+ divisors = positive_divisors
7
+ (2..Math.log2(self)).any? { |pow| divisors.any? {|div| (div ** pow) == self} }
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def polite?
4
+ return true if self == 1
5
+ politeness > 0
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def polydivisible?
4
+ return true if self == 1
5
+ return false if digits.first == 0
6
+ 1.upto(digits.size).all? do |first|
7
+ (digits.first(first).join.to_i % first) == 0
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ def powerful?
4
+ return false unless self > 0
5
+ divisors = positive_divisors
6
+ divisors.select {|d| d.prime? }.all?{|prime| divisors.include? (prime ** 2)}
7
+ end
8
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ class Integer
3
+ # Implementation of Stewart, B. M. (1954), "Sums of distinct divisors",
4
+ # American Journal of Mathematics 76: 779–785, doi:10.2307/2372651,
5
+ # MR0064800
6
+ def practical?
7
+ sum = 1
8
+ k = 2
9
+ n = self
10
+ while (n >= k)
11
+ s = 1
12
+ u = 0
13
+ while (n % k == 0)
14
+ n = n/k
15
+ s = s * k + 1
16
+ u += 1
17
+ end
18
+ unless (u == 0)
19
+ return false if (k > sum + 1)
20
+ sum *= s
21
+ end
22
+ k += (k == 2) ? 1 : 2
23
+ end
24
+ true
25
+ end
26
+ end