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/spec/emrip_spec.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
describe Integer, "emrip?" do
|
2
|
+
# http://www.research.att.com/~njas/sequences/A006567
|
3
|
+
EMRIP = [13,17,31,37,71,73,79,97,107,113,149,157,167,179,
|
4
|
+
199,311,337,347,359,389,701,709,733,739,743,751,
|
5
|
+
761,769,907,937,941,953,967,971,983,991,1009,1021,
|
6
|
+
1031,1033,1061,1069,1091,1097,1103,1109,1151,1153,
|
7
|
+
1181,1193]
|
8
|
+
|
9
|
+
it "returns true for primes that give a different prime when reversed" do
|
10
|
+
EMRIP.each do |number|
|
11
|
+
number.should be_emrip
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns false for non-emrips" do
|
16
|
+
((1..1193).to_a - EMRIP).each do |number|
|
17
|
+
number.should_not be_emrip
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns false for palindromic primes" do
|
22
|
+
[11,151].each do |number|
|
23
|
+
number.should_not be_emrip
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
describe Integer, "#equidigital?" do
|
2
|
+
# http://www.research.att.com/~njas/sequences/A046758
|
3
|
+
EQUIDIGITAL = [1,2,3,5,7,10,11,13,14,15,16,17,19,21,23,25,27,29,
|
4
|
+
31,32,35,37,41,43,47,49,53,59,61,64,67,71,73,79,
|
5
|
+
81,83,89,97,101,103,105,106,107,109,111,112,113,
|
6
|
+
115,118,119,121,122,123,127,129,131,133,134,135,
|
7
|
+
137,139]
|
8
|
+
|
9
|
+
it "returns true for equidigital numbers" do
|
10
|
+
EQUIDIGITAL.each do |number|
|
11
|
+
number.should be_equidigital
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns true for prime numbers" do
|
16
|
+
[193, 197, 199].each do |number|
|
17
|
+
number.should be_equidigital
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns false for non-equidigital numbers" do
|
22
|
+
((1..139).to_a - EQUIDIGITAL).each do |number|
|
23
|
+
number.should_not be_equidigital
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
describe Integer, "#extravagant?" do
|
2
|
+
# http://www.research.att.com/~njas/sequences/A046760
|
3
|
+
EXTRAVAGANT = [4,6,8,9,12,18,20,22,24,26,28,30,33,34,36,38,39,
|
4
|
+
40,42,44,45,46,48,50,51,52,54,55,56,57,58,60,62,
|
5
|
+
63,65,66,68,69,70,72,74,75,76,77,78,80,82,84,85,
|
6
|
+
86,87,88,90,91,92,93,94,95,96,98,99,100,102,104,
|
7
|
+
108,110,114]
|
8
|
+
|
9
|
+
it "returns true for extravagant numbers" do
|
10
|
+
EXTRAVAGANT.each do |number|
|
11
|
+
number.should be_extravagant
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns false for non-extravagant numbers" do
|
16
|
+
((1..114).to_a - EXTRAVAGANT).each do |number|
|
17
|
+
number.should_not be_extravagant
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/factor_spec.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
describe Integer, "#factor?" do
|
2
|
+
it "returns true if self divided by other leaves no remainder" do
|
3
|
+
100.factor?(10).should be_true
|
4
|
+
9.factor?(3).should be_true
|
5
|
+
end
|
6
|
+
|
7
|
+
it "returns false if other is bigger than self" do
|
8
|
+
50.factor?(500).should be_false
|
9
|
+
end
|
10
|
+
|
11
|
+
it "returns false if self divided by other leaves a remainder" do
|
12
|
+
10.factor?(3).should be_false
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns false if other is 0" do
|
16
|
+
44.factor?(0).should be_false
|
17
|
+
end
|
18
|
+
|
19
|
+
it "works with negative numbers" do
|
20
|
+
20.factor?(-5).should be_true
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
describe Integer, "#factorion?" do
|
2
|
+
it "returns true for factorions" do
|
3
|
+
[1, 2, 145, 40585].each do |number|
|
4
|
+
number.should be_factorion
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
it "returns false for non-factorions" do
|
9
|
+
[3, 146, 4828291001].each do |number|
|
10
|
+
number.should_not be_factorion
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/spec/frugal_spec.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
describe Integer, "#frugal?" 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
|
+
|
9
|
+
it "returns true for frugal numbers" do
|
10
|
+
FRUGAL.each do |number|
|
11
|
+
number.should be_frugal
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns false for non-frugal numbers" do
|
16
|
+
((126..4912).to_a - FRUGAL).each do |number|
|
17
|
+
number.should_not be_frugal
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/happy_spec.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
describe Integer, "#happy?" do
|
2
|
+
HAPPY = [1,7,10,13,19,23,28,31,32,44,49,68,70,79,82,86,91,
|
3
|
+
94,97,100,103,109,129,130,133,139,167,176,188,190,
|
4
|
+
192,193,203,208,219,226,230,236,239,262,263,280,
|
5
|
+
291,293,301,302,310,313,319,320,326,329,331,338]
|
6
|
+
it "returns true for happy numbers" do
|
7
|
+
HAPPY.each do |number|
|
8
|
+
number.should be_happy
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns false for unhappy numbers" do
|
13
|
+
((1..338).to_a - HAPPY).each do |number|
|
14
|
+
number.should_not be_happy
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it "returns false for negative numbers" do
|
19
|
+
(1..338).each do |number|
|
20
|
+
(-number).should_not be_happy
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
describe Integer, "#harshad?" do
|
2
|
+
# http://www.research.att.com/~njas/sequences/A005349
|
3
|
+
HARSHAD = [1,2,3,4,5,6,7,8,9,10,12,18,20,21,24,27,30,36,40,
|
4
|
+
42,45,48,50,54,60,63,70,72,80,81,84,90,100,102,
|
5
|
+
108,110,111,112,114,117,120,126,132,133,135,140,
|
6
|
+
144,150,152,153,156,162,171,180,190,192,195,198,
|
7
|
+
200,201,204]
|
8
|
+
it "returns true for Harshad (Niven) numbers" do
|
9
|
+
HARSHAD.each do |number|
|
10
|
+
number.should be_harshad
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns false for non-Harshad/Niven numbers" do
|
15
|
+
((1..204).to_a - HARSHAD).each do |number|
|
16
|
+
number.should_not be_harshad
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns false for negative numbers" do
|
21
|
+
(1..204).each do |number|
|
22
|
+
(-number).should_not be_harshad
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
describe Integer, "#hilbert?" do
|
2
|
+
HILBERT = [1,5,9,13,17,21,25,29,33,37,41,45,49,53,57,61,65,
|
3
|
+
69,73,77,81,85,89,93,97,101,105,109,113,117,121,
|
4
|
+
125,129,133,137,141,145,149,153,157,161,165,169,
|
5
|
+
173,177,181,185,189]
|
6
|
+
it "returns true for a Hilbert number" do
|
7
|
+
HILBERT.each do |number|
|
8
|
+
number.should be_hilbert
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns false for a non-Hilbert number" do
|
13
|
+
((1..188).to_a - HILBERT).each do |number|
|
14
|
+
number.should_not be_hilbert
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it "returns false for a negative number" do
|
19
|
+
(1..189).each do |number|
|
20
|
+
(-number).should_not be_hilbert
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,59 @@
|
|
1
|
+
describe Integer, "#hyperperfect?" do
|
2
|
+
# http://www.research.att.com/~njas/sequences/A000396
|
3
|
+
PERFECT = [6,28,496,8128,33550336,8589869056,137438691328,
|
4
|
+
2305843008139952128,
|
5
|
+
2658455991569831744654692615953842176,
|
6
|
+
191561942608236107294793378084303638130997321548169216]
|
7
|
+
|
8
|
+
|
9
|
+
it "returns true for a 1-hyperperfect (perfect) number" do
|
10
|
+
PERFECT.first(5).each do |number|
|
11
|
+
number.should be_hyperperfect
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns false for a non-1-hyperperfect (non-perfect) number" do
|
16
|
+
((1..2000).to_a - PERFECT).sample(5).each do |number|
|
17
|
+
number.should_not be_hyperperfect
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns true for 6-hyperperfect numbers with an argument of 6" do
|
22
|
+
# http://www.research.att.com/~njas/sequences/A028499
|
23
|
+
[301,16513,60110701,1977225901].each do |number|
|
24
|
+
number.hyperperfect?(6).should be_true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it "returns false for non-6-hyperperfect numbers with an argument of 6" do
|
29
|
+
[302,6].each do |number|
|
30
|
+
number.hyperperfect?(6).should be_false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "returns true for 18-hyperperfect numbers with an argument of 18" do
|
35
|
+
# http://www.research.att.com/~njas/sequences/A028501
|
36
|
+
[1333,1909,2469601,893748277].each do |number|
|
37
|
+
number.hyperperfect?(18).should be_true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it "returns false for non-18-hyperperfect numbers with an argument of 18" do
|
42
|
+
[6,1334].each do |number|
|
43
|
+
number.hyperperfect?(18).should be_false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it "returns true for 2772-hyperperfect numbers with an argument of 2772" do
|
48
|
+
# http://www.research.att.com/~njas/sequences/A028502
|
49
|
+
[95295817,124035913,749931337,4275383113].each do |number|
|
50
|
+
number.hyperperfect?(2772).should be_true
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
it "returns false for non-2722-hyperperfect numbers with an argument of 2722" do
|
55
|
+
[6,95295816].each do |number|
|
56
|
+
number.hyperperfect?(2772).should be_false
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
describe Integer, "#kaprekar?" do
|
2
|
+
KAPREKAR = [1,9,45,55,99,297,703,999,2223,2728,4879,4950,
|
3
|
+
5050,5292,7272,7777,9999,17344,22222,38962,77778,
|
4
|
+
82656,95121,99999,142857,148149,181819,187110,
|
5
|
+
208495,318682,329967,351352,356643,390313,461539,
|
6
|
+
466830,499500,500500,533170]
|
7
|
+
|
8
|
+
it "returns true for Kaprekar numbers" do
|
9
|
+
KAPREKAR.each do |number|
|
10
|
+
number.should be_kaprekar
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns false for non-Kaprekar numbers" do
|
15
|
+
((1..300).to_a - KAPREKAR).each do |number|
|
16
|
+
number.should_not be_kaprekar
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns false for negative numbers" do
|
21
|
+
[-5, -9, -297, -345].each do |number|
|
22
|
+
number.should_not be_kaprekar
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/spec/keith_spec.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
describe Integer, "#keith?" do
|
2
|
+
# http://www.research.att.com/~njas/sequences/A007629
|
3
|
+
KEITH = [14,19,28,47,61,75,197,742,1104,1537,2208,2580,
|
4
|
+
3684,4788,7385,7647,7909,31331,34285,34348,55604,
|
5
|
+
62662,86935,93993,120284,129106,147640,156146,
|
6
|
+
174680,183186,298320,355419,694280,925993,1084051,
|
7
|
+
7913837,11436171,33445755,44121607]
|
8
|
+
|
9
|
+
it "returns true for Keith numbers" do
|
10
|
+
KEITH.each do |number|
|
11
|
+
number.should be_keith
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns false for non-Keith numbers" do
|
16
|
+
((1..200).to_a - KEITH).each do |number|
|
17
|
+
number.should_not be_keith
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
data/spec/kynea_spec.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
describe Integer, "#kynea?" do
|
2
|
+
# http://www.research.att.com/~njas/sequences/A093069
|
3
|
+
KYNEA = [7,23,79,287,1087,4223,16639,66047,263167,1050623,
|
4
|
+
4198399,16785407,67125247,268468223,1073807359,
|
5
|
+
4295098367,17180131327,68720001023,274878955519,
|
6
|
+
1099513724927,4398050705407,17592194433023]
|
7
|
+
|
8
|
+
it "returns true for Kynea numbers" do
|
9
|
+
KYNEA.each do |number|
|
10
|
+
number.should be_kynea
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns false for non-Kynea numbers" do
|
15
|
+
((1..300).to_a - KYNEA).each do |number|
|
16
|
+
number.should_not be_kynea
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
describe Integer, "#maris_mcgwire_sosa_pair?" do
|
2
|
+
|
3
|
+
MMS = [7,14,43,50,61,63,67,80,84,118,122,134,137,163,
|
4
|
+
196,212,213,224,241,273,274,277,279,283,351,352,
|
5
|
+
373,375,390,398,421,457,462,474,475,489,495,510,
|
6
|
+
516,523,526,537,547,555,558,577,584,590,592,616,
|
7
|
+
638,644,660,673,687,691]
|
8
|
+
|
9
|
+
it "returns true for Maris-McGwire-Sosa pairs" do
|
10
|
+
MMS.each do |number|
|
11
|
+
number.maris_mcgwire_sosa_pair?(number+1).should be_true
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns false for non-Maris-McGwire-Sosa pairs" do
|
16
|
+
((1..200).to_a - MMS).each do |number|
|
17
|
+
number.maris_mcgwire_sosa_pair?(number+1).should be_false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/mobius_spec.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
describe Integer, "#mobius" do
|
3
|
+
# http://www.research.att.com/~njas/sequences/A008683
|
4
|
+
MOBIUS = [1,-1,-1,0,-1,1,-1,0,0,1,-1,0,-1,1,1,0,-1,0,-1,0,
|
5
|
+
1,1,-1,0,0,1,0,0,-1,-1,-1,0,1,1,1,0,-1,1,1,0,-1,
|
6
|
+
-1,-1,0,0,1,-1,0,0,0,1,0,-1,0,1,0,1,1,-1,0,-1,1,0,
|
7
|
+
0,1,-1,-1,0,1,-1,-1,0,-1,1,0,0,1]
|
8
|
+
|
9
|
+
it "returns 1 if n is a square-free positive integer with an even number of distinct prime factors" do
|
10
|
+
MOBIUS.each_with_index do |value, idx|
|
11
|
+
next unless value == 1
|
12
|
+
(idx + 1).mobius.should == 1
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "returns −1 if n is a square-free positive integer with an odd number of distinct prime factors" do
|
17
|
+
MOBIUS.each_with_index do |value, idx|
|
18
|
+
next unless value == -1
|
19
|
+
(idx + 1).mobius.should == -1
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "returns −1 if n is prime" do
|
24
|
+
Prime.each(200).each do |prime|
|
25
|
+
next if MOBIUS.size < prime
|
26
|
+
prime.mobius.should == -1
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "returns 0 iff n is not square-free." do
|
31
|
+
MOBIUS.each_with_index do |value, idx|
|
32
|
+
next unless value == 0
|
33
|
+
(idx + 1).mobius.should == 0
|
34
|
+
end
|
35
|
+
|
36
|
+
# http://www.research.att.com/~njas/sequences/A013929
|
37
|
+
[4,8,9,12,16,18,20,24,25,27,28,32,36,40,44,45,48,
|
38
|
+
49,50,52,54,56,60,63,64,68,72,75,76,80,81,84,88,
|
39
|
+
90,92,96,98,99,100,104,108,112,116,117,120,121,
|
40
|
+
124,125,126,128,132,135,136,140,144,147,148,150,
|
41
|
+
152,153,156,160].each do |number|
|
42
|
+
next if MOBIUS.size < number
|
43
|
+
number.mobius.should == 0
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it "returns nil if n is not a positive integer" do
|
48
|
+
(0..25).each do |number|
|
49
|
+
(-number).mobius.should be_nil
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
describe Integer, "#narcissistic?" do
|
2
|
+
NARCISSISTIC = [1,2,3,4,5,6,7,8,9,153,370,371,407,1634,8208,9474,
|
3
|
+
54748,92727,93084,548834,1741725,4210818,9800817,
|
4
|
+
9926315,24678050,24678051,88593477,146511208,
|
5
|
+
472335975,534494836,912985153,4679307774,
|
6
|
+
32164049650,32164049651]
|
7
|
+
it "returns true for narcissistic numbers" do
|
8
|
+
NARCISSISTIC.each do |number|
|
9
|
+
number.should be_narcissistic
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns false for non-narcissistic numbers" do
|
14
|
+
[10,30,100,178762628].each do |number|
|
15
|
+
number.should_not be_narcissistic
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
describe Integer, "#nivenmorphic?" do
|
2
|
+
NIVENMORPHIC = [1,2,3,4,5,6,7,8,9,910,0,912,11713,6314,915,3616,
|
3
|
+
15317,918,17119,9920,18921,9922,82823,19824,9925,
|
4
|
+
46826,18927,18928,78329,99930,585931,388832,
|
5
|
+
1098933,198934,289835,99936,99937,478838,198939,
|
6
|
+
1999840]
|
7
|
+
it "returns true for Nivenmorphic numbers" do
|
8
|
+
NIVENMORPHIC.each do |number|
|
9
|
+
number.should be_nivenmorphic
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns false for non-Nivenmorphic numbers" do
|
14
|
+
((1..300).to_a - NIVENMORPHIC).each do |number|
|
15
|
+
number.should_not be_nivenmorphic
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns false for negative numbers" do
|
20
|
+
(1..300).each do |number|
|
21
|
+
(-number).should_not be_nivenmorphic
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
describe Integer, "#number_of_distinct_prime_factors" do
|
3
|
+
# http://www.research.att.com/~njas/sequences/A001221
|
4
|
+
OMEGA = [0,1,1,1,1,2,1,1,1,2,1,2,1,2,2,1,1,2,1,2,2,2,1,2,
|
5
|
+
1,2,1,2,1,3,1,1,2,2,2,2,1,2,2,2,1,3,1,2,2,2,1,2,1,
|
6
|
+
2,2,2,1,2,2,2,2,2,1,3,1,2,2,1,2,3,1,2,2,3,1,2,1,2,
|
7
|
+
2,2,2,3,1,2,1,2,1,3,2,2,2,2,1,3,2,2,2,2,2,2,1,2,2,
|
8
|
+
2,1,3,1,2,3,2,1,2,1,3,2]
|
9
|
+
|
10
|
+
it "returns the number of distinct prime factors of self" do
|
11
|
+
OMEGA.each_with_index do |omega, number|
|
12
|
+
(number + 1).number_of_distinct_prime_factors.should == omega
|
13
|
+
end
|
14
|
+
|
15
|
+
# http://www.research.att.com/~njas/sequences/A046386
|
16
|
+
[210,330,390,462,510,546,570,690,714,770,798,858,
|
17
|
+
870,910,930,966,1110,1122,1155,1190,1218,1230,
|
18
|
+
1254,1290,1302,1326,1330,1365,1410,1430,1482,1518,
|
19
|
+
1554,1590,1610,1722,1770,1785,1794,1806,1830,1870,
|
20
|
+
1914,1938,1974].each do |number|
|
21
|
+
number.number_of_distinct_prime_factors.should == 4
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it "does not count any factor more than once" do
|
26
|
+
[4, 8, 9, 12, 16, 18, 20, 24, 25, 27, 28, 32, 36, 40, 44, 45, 48, 49,
|
27
|
+
50, 52, 54, 56, 60, 63, 64, 68, 72, 75, 76, 80, 81, 84, 88, 90, 92,
|
28
|
+
96, 98, 99, 100, 104, 108, 112, 116, 117, 120, 121, 124, 125, 126, 128,
|
29
|
+
132, 135, 136, 140, 144, 147, 148, 150].each do |number|
|
30
|
+
number.prime_factors.size.should >= 2
|
31
|
+
number.prime_factors.uniq.should_not == number.prime_factors
|
32
|
+
number.number_of_distinct_prime_factors.should == number.prime_factors.uniq.size
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
describe Integer, "#number_of_prime_factors" do
|
3
|
+
# http://www.research.att.com/~njas/sequences/A001222
|
4
|
+
BIGOMEGA = [0,1,1,2,1,2,1,3,2,2,1,3,1,2,2,4,1,3,1,3,2,2,1,4,
|
5
|
+
2,2,3,3,1,3,1,5,2,2,2,4,1,2,2,4,1,3,1,3,3,2,1,5,2,
|
6
|
+
3,2,3,1,4,2,4,2,2,1,4,1,2,3,6,2,3,1,3,2,3,1,5,1,2,
|
7
|
+
3,3,2,3,1,5,4,2,1,4,2,2,2,4,1,4,2,3,2,2,2,6,1,3,3,
|
8
|
+
4,1,3,1,4,3,2,1,5,1,3,2]
|
9
|
+
|
10
|
+
it "returns the number of prime factors (counted with multiplicity) of self" do
|
11
|
+
BIGOMEGA.each_with_index do |bigomega, number|
|
12
|
+
(number + 1).number_of_prime_factors.should == bigomega
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "is >= ω" do
|
17
|
+
BIGOMEGA.each_with_index do |bigomega, number|
|
18
|
+
(number + 1).number_of_prime_factors.should >= (number + 1).number_of_distinct_prime_factors
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it "counts factors more than once" do
|
23
|
+
[4, 8, 9, 12, 16, 18, 20, 24, 25, 27, 28, 32, 36, 40, 44, 45, 48, 49,
|
24
|
+
50, 52, 54, 56, 60, 63, 64, 68, 72, 75, 76, 80, 81, 84, 88, 90, 92,
|
25
|
+
96, 98, 99, 100, 104, 108, 112, 116, 117, 120, 121, 124, 125, 126, 128,
|
26
|
+
132, 135, 136, 140, 144, 147, 148, 150].each do |number|
|
27
|
+
number.prime_factors.size.should >= 2
|
28
|
+
number.prime_factors.uniq.should_not == number.prime_factors
|
29
|
+
number.number_of_prime_factors.should == number.prime_factors.size
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
describe Integer, "#ordinal" do
|
2
|
+
it "returns 'th' for numbers ending in 0" do
|
3
|
+
0.ordinal.should == 'th'
|
4
|
+
20.ordinal.should == 'th'
|
5
|
+
end
|
6
|
+
|
7
|
+
it "returns 'st' for numbers ending in 1" do
|
8
|
+
1.ordinal.should == 'st'
|
9
|
+
201.ordinal.should == 'st'
|
10
|
+
111.ordinal.should == 'st'
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns 'nd' for numbers ending in 2" do
|
14
|
+
2.ordinal.should == 'nd'
|
15
|
+
202.ordinal.should == 'nd'
|
16
|
+
222.ordinal.should == 'nd'
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns 'rd' for numbers ending in 3" do
|
20
|
+
3.ordinal.should == 'rd'
|
21
|
+
203.ordinal.should == 'rd'
|
22
|
+
333.ordinal.should == 'rd'
|
23
|
+
end
|
24
|
+
|
25
|
+
it "returns 'th' for numbers ending in 4" do
|
26
|
+
4.ordinal.should == 'th'
|
27
|
+
204.ordinal.should == 'th'
|
28
|
+
444.ordinal.should == 'th'
|
29
|
+
end
|
30
|
+
|
31
|
+
it "returns 'th' for numbers ending in 5" do
|
32
|
+
5.ordinal.should == 'th'
|
33
|
+
205.ordinal.should == 'th'
|
34
|
+
555.ordinal.should == 'th'
|
35
|
+
end
|
36
|
+
|
37
|
+
it "returns 'th' for numbers ending in 6" do
|
38
|
+
6.ordinal.should == 'th'
|
39
|
+
206.ordinal.should == 'th'
|
40
|
+
666.ordinal.should == 'th'
|
41
|
+
end
|
42
|
+
|
43
|
+
it "returns 'th' for numbers ending in 7" do
|
44
|
+
7.ordinal.should == 'th'
|
45
|
+
207.ordinal.should == 'th'
|
46
|
+
777.ordinal.should == 'th'
|
47
|
+
end
|
48
|
+
|
49
|
+
it "returns 'th' for numbers ending in 8" do
|
50
|
+
8.ordinal.should == 'th'
|
51
|
+
208.ordinal.should == 'th'
|
52
|
+
888.ordinal.should == 'th'
|
53
|
+
end
|
54
|
+
|
55
|
+
it "returns 'th' for numbers ending in 9" do
|
56
|
+
9.ordinal.should == 'th'
|
57
|
+
209.ordinal.should == 'th'
|
58
|
+
999.ordinal.should == 'th'
|
59
|
+
end
|
60
|
+
|
61
|
+
it "returns 'th' for 10..19" do
|
62
|
+
(10..19).each{|n| n.ordinal.should == 'th'}
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
describe Integer, "#parasitic?" do
|
2
|
+
# http://www.research.att.com/~njas/sequences/A092697
|
3
|
+
PARASITIC = [1,105263157894736842,
|
4
|
+
1034482758620689655172413793,102564,
|
5
|
+
102040816326530612244897959183673469387755,
|
6
|
+
1016949152542372881355932203389830508474576271186440677966,
|
7
|
+
1014492753623188405797,1012658227848,
|
8
|
+
10112359550561797752808988764044943820224719]
|
9
|
+
|
10
|
+
|
11
|
+
it "returns true for parasitic numbers" do
|
12
|
+
PARASITIC.each do |number|
|
13
|
+
number.should be_parasitic
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
PARASITIC.each_with_index do |number,n|
|
18
|
+
n += 1
|
19
|
+
it "returns true for #{n}-parasitic numbers when #{n} is the argument" do
|
20
|
+
number.parasitic?(n).should be_true
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns false for non-parasitic numbers" do
|
25
|
+
[2, 8, 105263157894736843].each do |number|
|
26
|
+
number.should_not be_parasitic
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
describe Integer, "#perfect_power?" do
|
2
|
+
PERFECT_POWERS = [1, 4,8,9,16,25,27,32,36,49,64,81,100,121,125,128,
|
3
|
+
144,169,196,216,225,243,256,289,324,343,361,400,
|
4
|
+
441,484,512,529,576,625,676,729,784,841,900,961,
|
5
|
+
1000,1024,1089,1156,1225,1296,1331,1369,1444,1521,
|
6
|
+
1600,1681,1728,1764]
|
7
|
+
it "returns true for perfect powers" do
|
8
|
+
PERFECT_POWERS.each do |power|
|
9
|
+
power.should be_perfect_power
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns false for imperfect powers" do
|
14
|
+
((1..1764).to_a - PERFECT_POWERS).each do |power|
|
15
|
+
power.should_not be_perfect_power
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns false for negative numbers" do
|
20
|
+
(1..1764).each do |power|
|
21
|
+
(-power).should_not be_perfect_power
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|