ruby-numtheory 0.0.9.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/Gemfile +2 -2
- data/ext/numtheory/numtheory.c +1 -1
- data/ext/numtheory/reduce.c +1 -1
- data/ruby-numtheory.gemspec +1 -1
- data/spec/numtheory/arithmetic_functions_spec.rb +10 -10
- data/spec/numtheory/bit_function_spec.rb +10 -10
- data/spec/numtheory/modular_arithmetic_spec.rb +23 -23
- data/spec/numtheory/primality_tests_spec.rb +16 -22
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e863efa8ec92c5c6a67c5cc17df53c1355e0565
|
4
|
+
data.tar.gz: 0543e5d9d99b856c4442a95b91951999d9e0e134
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 03b09382a59295c16333ccc73a368e8976072209a709773735752382478974d111beb12f1e225d2c370ee21f04ec2424db6d8508d8ebcc659f96a301d731cc72
|
7
|
+
data.tar.gz: 02295d8eda2029415b4de4f9c2b0ab2f50c34effc8bd0501a1cb40ec66aa3e8e1b04d4cfcf4ef1a54562427b9069c00a2e02a7b83544b009bbb04383878281b1
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/ext/numtheory/numtheory.c
CHANGED
@@ -99,7 +99,7 @@ Init_numtheory()
|
|
99
99
|
numtheory_nextprime, 0);
|
100
100
|
rb_define_method(rb_cInteger, "prime_division",
|
101
101
|
numtheory_prime_division, 0);
|
102
|
-
rb_define_method(
|
102
|
+
rb_define_method(rb_cInteger, "factorial",
|
103
103
|
numtheory_factorial_primeswing, 0);
|
104
104
|
rb_define_method(rb_cInteger, "primes_upto",
|
105
105
|
numtheory_primes_upto, 1);
|
data/ext/numtheory/reduce.c
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
"adcl $0, %2 \n\t" : \
|
13
13
|
"=m"(c0), "=m"(c1), "=m"(c2) : \
|
14
14
|
"m"(c0), "m"(c1), "m"(c2), "m"(i), "m"(j) : \
|
15
|
-
"%eax","%edx", "
|
15
|
+
"%eax","%edx", "cc");
|
16
16
|
|
17
17
|
#define BITSPERDIG (SIZEOF_BDIGITS * CHAR_BIT)
|
18
18
|
#define BIGRAD ((BDIGIT_DBL)1 << BITSPERDIG)
|
data/ruby-numtheory.gemspec
CHANGED
@@ -3,7 +3,7 @@ Gem::Specification.new do |gem|
|
|
3
3
|
gem.homepage = "http://github.com/lomereiter/ruby-numtheory"
|
4
4
|
gem.license = "MIT"
|
5
5
|
gem.summary = "Ruby number theory library"
|
6
|
-
gem.version = "0.0
|
6
|
+
gem.version = "0.1.0"
|
7
7
|
gem.platform = Gem::Platform::RUBY
|
8
8
|
|
9
9
|
gem.description = <<-EOF
|
@@ -5,27 +5,27 @@ describe "arithmetic function" do
|
|
5
5
|
|
6
6
|
describe "primepi" do
|
7
7
|
it "should raise ArgumentError if there're not enough precomputed primes" do
|
8
|
-
expect { primepi(100_000_000_000) }.
|
8
|
+
expect { primepi(100_000_000_000) }.to raise_error(ArgumentError)
|
9
9
|
end
|
10
10
|
|
11
11
|
it "should return the number of primes less or equal than a given number" do
|
12
|
-
primepi(1).
|
13
|
-
primepi(2).
|
14
|
-
primepi(123512).
|
15
|
-
primepi(498765).
|
16
|
-
primepi(312361).
|
17
|
-
primepi(-123513).
|
12
|
+
expect(primepi(1)).to eq(0)
|
13
|
+
expect(primepi(2)).to eq(1)
|
14
|
+
expect(primepi(123512)).to eq(11607)
|
15
|
+
expect(primepi(498765)).to eq(41438)
|
16
|
+
expect(primepi(312361)).to eq(26983)
|
17
|
+
expect(primepi(-123513)).to eq(0)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
describe "eulerphi" do
|
22
22
|
it "should raise ArgumentError if the number is zero" do
|
23
|
-
expect { eulerphi(0) }.
|
23
|
+
expect { eulerphi(0) }.to raise_error(ArgumentError)
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should return the number of positive integers less than 'n' and coprime with 'n'" do
|
27
|
-
eulerphi(12312361361).
|
28
|
-
eulerphi(12361212371236123).
|
27
|
+
expect(eulerphi(12312361361)).to eq(11664342324)
|
28
|
+
expect(eulerphi(12361212371236123)).to eq(11713214239144000)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
@@ -3,30 +3,30 @@ require 'numtheory'
|
|
3
3
|
describe "bit functions" do
|
4
4
|
describe "popcount" do
|
5
5
|
it "should return the number of bits set in a non-negative number" do
|
6
|
-
12351231231235091328209183029825132908123295012351.popcount.
|
7
|
-
82362346211236.popcount.
|
6
|
+
expect(12351231231235091328209183029825132908123295012351.popcount).to eq (84)
|
7
|
+
expect(82362346211236.popcount).to eq(23)
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should raise ArgumentError for negative numbers" do
|
11
|
-
expect { (-5).popcount }.
|
12
|
-
expect { (-112351235123512351235).popcount }.
|
11
|
+
expect { (-5).popcount }.to raise_error(ArgumentError)
|
12
|
+
expect { (-112351235123512351235).popcount }.to raise_error(ArgumentError)
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
describe "bitlength" do
|
17
17
|
it "should return the length of a positive number in bits" do
|
18
|
-
123512351235125312.bitlength.
|
19
|
-
12355312.bitlength.
|
20
|
-
9123616312361235523462346311236136123123136212361.bitlength.
|
18
|
+
expect(123512351235125312.bitlength).to eq(57)
|
19
|
+
expect(12355312.bitlength).to eq(24)
|
20
|
+
expect(9123616312361235523462346311236136123123136212361.bitlength).to eq(163)
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should return 1 when argument is 0" do
|
24
|
-
0.bitlength.
|
24
|
+
expect(0.bitlength).to eq(1)
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should raise ArgumentError for negative numbers" do
|
28
|
-
expect { (-12351).bitlength }.
|
29
|
-
expect { (-2039462306492384059132).bitlength }.
|
28
|
+
expect { (-12351).bitlength }.to raise_error(ArgumentError)
|
29
|
+
expect { (-2039462306492384059132).bitlength }.to raise_error(ArgumentError)
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
@@ -4,61 +4,61 @@ describe "Z/pZ" do
|
|
4
4
|
|
5
5
|
describe "modular inverse" do
|
6
6
|
it "should raise ArgumentError if the inverse doesn't exist" do
|
7
|
-
expect { 3.inverse(1236123) }.
|
8
|
-
expect { 636356661312236113261221231.inverse(71324141236123712312361347131) }.
|
7
|
+
expect { 3.inverse(1236123) }.to raise_error(ArgumentError)
|
8
|
+
expect { 636356661312236113261221231.inverse(71324141236123712312361347131) }.to raise_error(ArgumentError)
|
9
9
|
end
|
10
10
|
|
11
11
|
it "should return the inverse modulo 'p' if it exists" do
|
12
|
-
170149610396103139612301.inverse(91023960139513512035130519351035).
|
13
|
-
10239613261.inverse(1713461351236).
|
14
|
-
61235.inverse(13271).
|
12
|
+
expect(170149610396103139612301.inverse(91023960139513512035130519351035)).to eq(47319168690720853552108144675271)
|
13
|
+
expect(10239613261.inverse(1713461351236)).to eq(1314959799745)
|
14
|
+
expect(61235.inverse(13271)).to eq(10552)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
18
|
describe "jacobi symbol (a|n)" do
|
19
19
|
it "should raise ArgumentError for negative 'n'" do
|
20
|
-
expect { 23.jacobi(-15) }.
|
20
|
+
expect { 23.jacobi(-15) }.to raise_error(ArgumentError)
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should raise ArgumentError for even 'n'" do
|
24
|
-
expect { 17.jacobi(24) }.
|
24
|
+
expect { 17.jacobi(24) }.to raise_error(ArgumentError)
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should return 0 when 'a' and 'n' have common divisors" do
|
28
|
-
25.jacobi(35).
|
29
|
-
-24.jacobi(9).
|
30
|
-
33.jacobi(3).
|
31
|
-
128367169183989283691832619381.jacobi(1829631238193612396183612312361231123).
|
28
|
+
expect(25.jacobi(35)).to be_zero
|
29
|
+
expect(-24.jacobi(9)).to be_zero
|
30
|
+
expect(33.jacobi(3)).to be_zero
|
31
|
+
expect(128367169183989283691832619381.jacobi(1829631238193612396183612312361231123)).to be_zero
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should return 1 when 'a' is a square residue modulo 'n'" do
|
35
|
-
26.jacobi(25).
|
36
|
-
261235125312351.jacobi(251235123123512351).
|
37
|
-
123412354123123512351235123512455123.jacobi(12351231236123112351235123523513411).
|
35
|
+
expect(26.jacobi(25)).to eq(1)
|
36
|
+
expect(261235125312351.jacobi(251235123123512351)).to eq(1)
|
37
|
+
expect(123412354123123512351235123512455123.jacobi(12351231236123112351235123523513411)).to eq(1)
|
38
38
|
end
|
39
39
|
|
40
40
|
it "should return -1 in all the other cases" do
|
41
|
-
123412354123123512351235123512455123.jacobi(1235123123612311235123512352351341).
|
42
|
-
1386136123613661.jacobi(19381923138198391).
|
43
|
-
2.jacobi(5).
|
44
|
-
17.jacobi(23).
|
41
|
+
expect(123412354123123512351235123512455123.jacobi(1235123123612311235123512352351341)).to eq(-1)
|
42
|
+
expect(1386136123613661.jacobi(19381923138198391)).to eq(-1)
|
43
|
+
expect(2.jacobi(5)).to eq(-1)
|
44
|
+
expect(17.jacobi(23)).to eq(-1)
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
48
|
describe "square root" do
|
49
49
|
|
50
50
|
it "should raise ArgumentError if 'p' is not a prime number" do
|
51
|
-
expect { 7.sqrt_mod(9)}.
|
51
|
+
expect { 7.sqrt_mod(9)}.to raise_error(ArgumentError)
|
52
52
|
end
|
53
53
|
|
54
54
|
it "should return nil if there is no square root" do
|
55
|
-
33.sqrt_mod(3).
|
56
|
-
1231356123521351251.sqrt_mod(123061236013903516123121612316123612379).
|
55
|
+
expect(33.sqrt_mod(3)).to be_nil
|
56
|
+
expect(1231356123521351251.sqrt_mod(123061236013903516123121612316123612379)).to be_nil
|
57
57
|
end
|
58
58
|
|
59
59
|
it "should otherwise return the square root from range [0 .. p/2]" do
|
60
|
-
1235112351311.sqrt_mod(123061936032513553).
|
61
|
-
910836013912301293612035135.sqrt_mod(45734753461235122313131523613612412341612361691).
|
60
|
+
expect(1235112351311.sqrt_mod(123061936032513553)).to eq(20377270042474840)
|
61
|
+
expect(910836013912301293612035135.sqrt_mod(45734753461235122313131523613612412341612361691)).to eq(17194653522779996910096883279884862754880186510)
|
62
62
|
end
|
63
63
|
end
|
64
64
|
end
|
@@ -1,31 +1,25 @@
|
|
1
1
|
require 'numtheory'
|
2
2
|
|
3
|
-
RSpec.configure do |config|
|
4
|
-
config.expect_with :rspec do |c|
|
5
|
-
c.syntax = [:should]
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
3
|
describe "primality tests" do
|
10
4
|
|
11
5
|
describe "prime? method" do
|
12
6
|
it "should return exact result for precomputed values" do
|
13
|
-
0.
|
14
|
-
1.
|
15
|
-
2.
|
16
|
-
3.
|
17
|
-
9.
|
18
|
-
-1.
|
19
|
-
-2.
|
20
|
-
-13.
|
21
|
-
-16.
|
7
|
+
expect(0).not_to be_prime
|
8
|
+
expect(1).not_to be_prime
|
9
|
+
expect(2).to be_prime
|
10
|
+
expect(3).to be_prime
|
11
|
+
expect(9).not_to be_prime
|
12
|
+
expect(-1).not_to be_prime
|
13
|
+
expect(-2).to be_prime
|
14
|
+
expect(-13).to be_prime
|
15
|
+
expect(-16).not_to be_prime
|
22
16
|
end
|
23
17
|
|
24
18
|
it "should apply BPSW test for larger values" do
|
25
|
-
12351236123627.
|
26
|
-
12351236123659.
|
27
|
-
12351236123627123612361235012532906135124234123512356123512342219.
|
28
|
-
12351236123627123612361235012532906135124234123512356123512342221.
|
19
|
+
expect(12351236123627).not_to be_prime
|
20
|
+
expect(12351236123659).to be_prime
|
21
|
+
expect(12351236123627123612361235012532906135124234123512356123512342219).not_to be_prime
|
22
|
+
expect(12351236123627123612361235012532906135124234123512356123512342221).to be_prime
|
29
23
|
end
|
30
24
|
|
31
25
|
it "should return true for BPSW pseudoprimes" do
|
@@ -38,13 +32,13 @@ describe "primality tests" do
|
|
38
32
|
NumTheory.module_eval { remove_const :PrimalityTests }
|
39
33
|
NumTheory::PrimalityTests = 1
|
40
34
|
srand(100) # MR test uses random base to start
|
41
|
-
671.
|
42
|
-
671.
|
35
|
+
expect(671).to be_MR_pseudoprime
|
36
|
+
expect(671).not_to be_BPSW_pseudoprime
|
43
37
|
|
44
38
|
NumTheory.module_eval { remove_const :PrimalityTests }
|
45
39
|
NumTheory::PrimalityTests = 10
|
46
40
|
srand(42)
|
47
|
-
671.
|
41
|
+
expect(671).not_to be_MR_pseudoprime
|
48
42
|
end
|
49
43
|
end
|
50
44
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-numtheory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Artem Tarasov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-03-07 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |2
|
14
14
|
The library is written as C extension and aims to provide
|
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
64
|
version: '0'
|
65
65
|
requirements: []
|
66
66
|
rubyforge_project:
|
67
|
-
rubygems_version: 2.
|
67
|
+
rubygems_version: 2.5.1
|
68
68
|
signing_key:
|
69
69
|
specification_version: 4
|
70
70
|
summary: Ruby number theory library
|