numb 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +10 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/lib/numb/abundant.rb +20 -0
- data/lib/numb/achilles.rb +15 -0
- data/lib/numb/automorphic.rb +24 -0
- data/lib/numb/carol.rb +21 -0
- data/lib/numb/deficient.rb +19 -0
- data/lib/numb/dihedral_prime.rb +25 -0
- data/lib/numb/dudeney.rb +15 -0
- data/lib/numb/economical.rb +16 -0
- data/lib/numb/emrip.rb +17 -0
- data/lib/numb/equidigital.rb +18 -0
- data/lib/numb/extravagant.rb +18 -0
- data/lib/numb/factorion.rb +15 -0
- data/lib/numb/frugal.rb +16 -0
- data/lib/numb/happy.rb +15 -0
- data/lib/numb/harshad.rb +8 -0
- data/lib/numb/hilbert.rb +7 -0
- data/lib/numb/hyperperfect.rb +7 -0
- data/lib/numb/impolite.rb +6 -0
- data/lib/numb/kaprekar.rb +14 -0
- data/lib/numb/keith.rb +15 -0
- data/lib/numb/kynea.rb +9 -0
- data/lib/numb/mms_pair.rb +11 -0
- data/lib/numb/mobius.rb +11 -0
- data/lib/numb/narcissistic.rb +8 -0
- data/lib/numb/nivenmorphic.rb +9 -0
- data/lib/numb/ordinal.rb +11 -0
- data/lib/numb/parasitic.rb +10 -0
- data/lib/numb/perfect.rb +10 -0
- data/lib/numb/perfect_power.rb +9 -0
- data/lib/numb/polite.rb +7 -0
- data/lib/numb/polydivisible.rb +10 -0
- data/lib/numb/powerful.rb +8 -0
- data/lib/numb/practical.rb +26 -0
- data/lib/numb/prime.rb +495 -0
- data/lib/numb/ruby1.8.rb +9 -0
- data/lib/numb/ruby1.9.rb +8 -0
- data/lib/numb/self.rb +13 -0
- data/lib/numb/self_descriptive.rb +9 -0
- data/lib/numb/semi_perfect.rb +15 -0
- data/lib/numb/semiprime.rb +6 -0
- data/lib/numb/smarandache_wellin.rb +11 -0
- data/lib/numb/smith.rb +7 -0
- data/lib/numb/sphenic.rb +8 -0
- data/lib/numb/square.rb +8 -0
- data/lib/numb/square_free.rb +6 -0
- data/lib/numb/triangular.rb +8 -0
- data/lib/numb/trimorphic.rb +6 -0
- data/lib/numb/undulating.rb +11 -0
- data/lib/numb/vampire.rb +11 -0
- data/lib/numb/weird.rb +8 -0
- data/lib/numb.rb +80 -0
- data/numb.gemspec +201 -0
- data/spec/abundant_spec.rb +24 -0
- data/spec/achilles_spec.rb +24 -0
- data/spec/automorphic_spec.rb +208 -0
- data/spec/carol_spec.rb +18 -0
- data/spec/deficient_spec.rb +23 -0
- data/spec/digital_sum_spec.rb +8 -0
- data/spec/dihedral_prime_spec.rb +38 -0
- data/spec/dudeney_spec.rb +14 -0
- data/spec/economical_spec.rb +28 -0
- data/spec/emrip_spec.rb +26 -0
- data/spec/equidigital_spec.rb +27 -0
- data/spec/extravagant_spec.rb +20 -0
- data/spec/factor_spec.rb +22 -0
- data/spec/factorion_spec.rb +13 -0
- data/spec/frugal_spec.rb +20 -0
- data/spec/happy_spec.rb +24 -0
- data/spec/harshad_spec.rb +25 -0
- data/spec/hilbert_spec.rb +24 -0
- data/spec/hyperperfect_spec.rb +59 -0
- data/spec/kaprekar_spec.rb +25 -0
- data/spec/keith_spec.rb +21 -0
- data/spec/kynea_spec.rb +19 -0
- data/spec/maris_mcgwire_sosa_pair_spec.rb +20 -0
- data/spec/mobius_spec.rb +52 -0
- data/spec/narcissistic_spec.rb +18 -0
- data/spec/nivenmorphic_spec.rb +24 -0
- data/spec/number_of_distinct_prime_factors_spec.rb +35 -0
- data/spec/number_of_prime_factors_spec.rb +32 -0
- data/spec/ordinal_spec.rb +64 -0
- data/spec/parasitic_spec.rb +29 -0
- data/spec/perfect_power_spec.rb +24 -0
- data/spec/perfect_spec.rb +103 -0
- data/spec/polite_spec.rb +14 -0
- data/spec/politeness_spec.rb +11 -0
- data/spec/polydivisible_spec.rb +19 -0
- data/spec/powerful_spec.rb +25 -0
- data/spec/practical_spec.rb +30 -0
- data/spec/self_descriptive_spec.rb +19 -0
- data/spec/self_spec.rb +20 -0
- data/spec/semi_perfect_spec.rb +24 -0
- data/spec/semiprime_spec.rb +21 -0
- data/spec/smarandache_wellin_spec.rb +26 -0
- data/spec/smith_spec.rb +19 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/sphenic_spec.rb +20 -0
- data/spec/square_free_spec.rb +19 -0
- data/spec/square_spec.rb +24 -0
- data/spec/triangular_spec.rb +19 -0
- data/spec/trimorphic_spec.rb +25 -0
- data/spec/undulating_spec.rb +20 -0
- data/spec/vampire_spec.rb +19 -0
- data/spec/weird_spec.rb +24 -0
- metadata +216 -0
data/.document
ADDED
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
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
|
data/lib/numb/dudeney.rb
ADDED
@@ -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
|
data/lib/numb/frugal.rb
ADDED
@@ -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
|
data/lib/numb/harshad.rb
ADDED
data/lib/numb/hilbert.rb
ADDED
@@ -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,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
|
data/lib/numb/mobius.rb
ADDED
@@ -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
|
data/lib/numb/ordinal.rb
ADDED
@@ -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
|
data/lib/numb/perfect.rb
ADDED
@@ -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
|
data/lib/numb/polite.rb
ADDED
@@ -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
|