prime_sieve 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/prime_sieve/extconf.rb +63 -0
- data/ext/prime_sieve/prime_sieve.c +195 -0
- data/ext/prime_sieve/prime_sieve.h +9 -0
- data/lib/prime_sieve.rb +5 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4f8cbfde40f177077ab71f59b186bb4cf29a3c44
|
4
|
+
data.tar.gz: 5de440372c422105f0a2aed7d90d2c22eee096f4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 430633f078e2a7688fc0be914269cf4c3c2ca65e67d931c108e22901bd9ddecdf2449bec5ec5504b4548aa2e94995c57eca3cb2f6b65ddf4f648b9225c883e42
|
7
|
+
data.tar.gz: 3b02746a0e5a6ff087549eb856bd3ef4c1be44b47d8704a339a80fde4d1a90836dfeeaded9425fbb394c488565a0ceef788499b7807506d830a1b408c4fee7f5
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require "mkmf"
|
2
|
+
|
3
|
+
LIBDIR = RbConfig::CONFIG['libdir']
|
4
|
+
INCLUDEDIR = RbConfig::CONFIG['includedir']
|
5
|
+
|
6
|
+
HEADER_DIRS = [
|
7
|
+
'/usr/local/include',
|
8
|
+
INCLUDEDIR,
|
9
|
+
'/usr/include',
|
10
|
+
]
|
11
|
+
|
12
|
+
LIB_DIRS = [
|
13
|
+
'/usr/local/lib',
|
14
|
+
LIBDIR,
|
15
|
+
'/usr/lib',
|
16
|
+
]
|
17
|
+
|
18
|
+
dir_config('primesieve', HEADER_DIRS, LIB_DIRS)
|
19
|
+
|
20
|
+
fns = [
|
21
|
+
'primesieve_generate_primes',
|
22
|
+
'primesieve_generate_n_primes',
|
23
|
+
'primesieve_nth_prime',
|
24
|
+
'primesieve_parallel_nth_prime',
|
25
|
+
'primesieve_count_primes',
|
26
|
+
'primesieve_count_twins',
|
27
|
+
'primesieve_count_triplets',
|
28
|
+
'primesieve_count_quadruplets',
|
29
|
+
'primesieve_count_quintuplets',
|
30
|
+
'primesieve_count_sextuplets',
|
31
|
+
'primesieve_parallel_count_primes',
|
32
|
+
'primesieve_parallel_count_twins',
|
33
|
+
'primesieve_parallel_count_triplets',
|
34
|
+
'primesieve_parallel_count_quadruplets',
|
35
|
+
'primesieve_parallel_count_quintuplets',
|
36
|
+
'primesieve_parallel_count_sextuplets',
|
37
|
+
'primesieve_print_primes',
|
38
|
+
'primesieve_print_twins',
|
39
|
+
'primesieve_print_triplets',
|
40
|
+
'primesieve_print_quadruplets',
|
41
|
+
'primesieve_print_quintuplets',
|
42
|
+
'primesieve_print_sextuplets',
|
43
|
+
'primesieve_callback_primes',
|
44
|
+
'primesieve_parallel_callback_primes',
|
45
|
+
'primesieve_get_sieve_size',
|
46
|
+
'primesieve_get_num_threads',
|
47
|
+
'primesieve_get_max_stop',
|
48
|
+
'primesieve_set_sieve_size',
|
49
|
+
'primesieve_set_num_threads',
|
50
|
+
'primesieve_test'
|
51
|
+
]
|
52
|
+
|
53
|
+
unless find_header('primesieve.h')
|
54
|
+
abort 'primesieve header is missing. please install primesieve'
|
55
|
+
end
|
56
|
+
|
57
|
+
fns.each do |fn|
|
58
|
+
unless find_library('primesieve', fn)
|
59
|
+
abort "primesieve library is missing. could not find function #{fn}. please install primesieve"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
create_makefile("prime_sieve/prime_sieve")
|
@@ -0,0 +1,195 @@
|
|
1
|
+
#include <prime_sieve.h>
|
2
|
+
|
3
|
+
VALUE mPrimeSieve;
|
4
|
+
|
5
|
+
static VALUE generate_primes(VALUE self, VALUE start, VALUE stop) {
|
6
|
+
size_t size;
|
7
|
+
uint64_t* primes = (uint64_t *) primesieve_generate_primes(NUM2ULL(start), NUM2ULL(stop), &size, UINT64_PRIMES);
|
8
|
+
VALUE ruby_primes = rb_ary_new2(size);
|
9
|
+
for(size_t i = 0; i < size; i++) {
|
10
|
+
rb_ary_store(ruby_primes, i, ULL2NUM(primes[i]));
|
11
|
+
}
|
12
|
+
primesieve_free(primes);
|
13
|
+
return ruby_primes;
|
14
|
+
}
|
15
|
+
|
16
|
+
static VALUE generate_n_primes(VALUE self, VALUE n, VALUE start) {
|
17
|
+
uint64_t* primes = (uint64_t *) primesieve_generate_n_primes(NUM2ULL(n), NUM2ULL(start), UINT64_PRIMES);
|
18
|
+
VALUE ruby_primes = rb_ary_new2(NUM2LONG(n));
|
19
|
+
for(long i = 0; i < NUM2LONG(n); i++) {
|
20
|
+
rb_ary_store(ruby_primes, i, ULL2NUM(primes[i]));
|
21
|
+
}
|
22
|
+
primesieve_free(primes);
|
23
|
+
return ruby_primes;
|
24
|
+
}
|
25
|
+
|
26
|
+
static VALUE nth_prime(VALUE self, VALUE n, VALUE start) {
|
27
|
+
return ULL2NUM(primesieve_nth_prime(NUM2LL(n), NUM2ULL(start)));
|
28
|
+
}
|
29
|
+
|
30
|
+
static VALUE parallel_nth_prime(VALUE self, VALUE n, VALUE start) {
|
31
|
+
return ULL2NUM(primesieve_parallel_nth_prime(NUM2LL(n), NUM2ULL(start)));
|
32
|
+
}
|
33
|
+
|
34
|
+
static VALUE count_primes(VALUE self, VALUE start, VALUE stop) {
|
35
|
+
return ULL2NUM(primesieve_count_primes(NUM2ULL(start), NUM2ULL(stop)));
|
36
|
+
}
|
37
|
+
|
38
|
+
static VALUE count_twins(VALUE self, VALUE start, VALUE stop) {
|
39
|
+
return ULL2NUM(primesieve_count_twins(NUM2ULL(start), NUM2ULL(stop)));
|
40
|
+
}
|
41
|
+
|
42
|
+
static VALUE count_triplets(VALUE self, VALUE start, VALUE stop) {
|
43
|
+
return ULL2NUM(primesieve_count_triplets(NUM2ULL(start), NUM2ULL(stop)));
|
44
|
+
}
|
45
|
+
|
46
|
+
static VALUE count_quadruplets(VALUE self, VALUE start, VALUE stop) {
|
47
|
+
return ULL2NUM(primesieve_count_quadruplets(NUM2ULL(start), NUM2ULL(stop)));
|
48
|
+
}
|
49
|
+
|
50
|
+
static VALUE count_quintuplets(VALUE self, VALUE start, VALUE stop) {
|
51
|
+
return ULL2NUM(primesieve_count_quintuplets(NUM2ULL(start), NUM2ULL(stop)));
|
52
|
+
}
|
53
|
+
|
54
|
+
static VALUE count_sextuplets(VALUE self, VALUE start, VALUE stop) {
|
55
|
+
return ULL2NUM(primesieve_count_sextuplets(NUM2ULL(start), NUM2ULL(stop)));
|
56
|
+
}
|
57
|
+
|
58
|
+
static VALUE parallel_count_primes(VALUE self, VALUE start, VALUE stop) {
|
59
|
+
return ULL2NUM(primesieve_parallel_count_primes(NUM2ULL(start), NUM2ULL(stop)));
|
60
|
+
}
|
61
|
+
|
62
|
+
static VALUE parallel_count_twins(VALUE self, VALUE start, VALUE stop) {
|
63
|
+
return ULL2NUM(primesieve_parallel_count_twins(NUM2ULL(start), NUM2ULL(stop)));
|
64
|
+
}
|
65
|
+
|
66
|
+
static VALUE parallel_count_triplets(VALUE self, VALUE start, VALUE stop) {
|
67
|
+
return ULL2NUM(primesieve_parallel_count_triplets(NUM2ULL(start), NUM2ULL(stop)));
|
68
|
+
}
|
69
|
+
|
70
|
+
static VALUE parallel_count_quadruplets(VALUE self, VALUE start, VALUE stop) {
|
71
|
+
return ULL2NUM(primesieve_parallel_count_quadruplets(NUM2ULL(start), NUM2ULL(stop)));
|
72
|
+
}
|
73
|
+
|
74
|
+
static VALUE parallel_count_quintuplets(VALUE self, VALUE start, VALUE stop) {
|
75
|
+
return ULL2NUM(primesieve_parallel_count_quintuplets(NUM2ULL(start), NUM2ULL(stop)));
|
76
|
+
}
|
77
|
+
|
78
|
+
static VALUE parallel_count_sextuplets(VALUE self, VALUE start, VALUE stop) {
|
79
|
+
return ULL2NUM(primesieve_parallel_count_sextuplets(NUM2ULL(start), NUM2ULL(stop)));
|
80
|
+
}
|
81
|
+
|
82
|
+
static VALUE print_primes(VALUE self, VALUE start, VALUE stop) {
|
83
|
+
primesieve_print_primes(NUM2ULL(start), NUM2ULL(stop));
|
84
|
+
return Qnil;
|
85
|
+
}
|
86
|
+
|
87
|
+
static VALUE print_twins(VALUE self, VALUE start, VALUE stop) {
|
88
|
+
primesieve_print_twins(NUM2ULL(start), NUM2ULL(stop));
|
89
|
+
return Qnil;
|
90
|
+
}
|
91
|
+
|
92
|
+
static VALUE print_triplets(VALUE self, VALUE start, VALUE stop) {
|
93
|
+
primesieve_print_triplets(NUM2ULL(start), NUM2ULL(stop));
|
94
|
+
return Qnil;
|
95
|
+
}
|
96
|
+
|
97
|
+
static VALUE print_quadruplets(VALUE self, VALUE start, VALUE stop) {
|
98
|
+
primesieve_print_quadruplets(NUM2ULL(start), NUM2ULL(stop));
|
99
|
+
return Qnil;
|
100
|
+
}
|
101
|
+
|
102
|
+
static VALUE print_quintuplets(VALUE self, VALUE start, VALUE stop) {
|
103
|
+
primesieve_print_quintuplets(NUM2ULL(start), NUM2ULL(stop));
|
104
|
+
return Qnil;
|
105
|
+
}
|
106
|
+
|
107
|
+
static VALUE print_sextuplets(VALUE self, VALUE start, VALUE stop) {
|
108
|
+
primesieve_print_sextuplets(NUM2ULL(start), NUM2ULL(stop));
|
109
|
+
return Qnil;
|
110
|
+
}
|
111
|
+
|
112
|
+
static void callback_fn(uint64_t prime) {
|
113
|
+
rb_yield(ULL2NUM(prime));
|
114
|
+
return;
|
115
|
+
}
|
116
|
+
|
117
|
+
static VALUE callback_primes(VALUE self, VALUE start, VALUE stop) {
|
118
|
+
if (!rb_block_given_p())
|
119
|
+
rb_raise(rb_eArgError, "Expected block");
|
120
|
+
primesieve_callback_primes(NUM2ULL(start), NUM2ULL(stop), callback_fn);
|
121
|
+
return Qnil;
|
122
|
+
}
|
123
|
+
|
124
|
+
static void parallel_callback_fn(uint64_t prime, int thread_id) {
|
125
|
+
rb_yield_values(2, ULL2NUM(prime), INT2NUM(thread_id));
|
126
|
+
return;
|
127
|
+
}
|
128
|
+
|
129
|
+
static VALUE parallel_callback_primes(VALUE self, VALUE start, VALUE stop) {
|
130
|
+
if (!rb_block_given_p())
|
131
|
+
rb_raise(rb_eArgError, "Expected block");
|
132
|
+
primesieve_parallel_callback_primes(NUM2ULL(start), NUM2ULL(stop), parallel_callback_fn);
|
133
|
+
return Qnil;
|
134
|
+
}
|
135
|
+
|
136
|
+
static VALUE get_sieve_size(VALUE self) {
|
137
|
+
return INT2NUM(primesieve_get_sieve_size());
|
138
|
+
}
|
139
|
+
|
140
|
+
static VALUE get_num_threads(VALUE self) {
|
141
|
+
return INT2NUM(primesieve_get_num_threads());
|
142
|
+
}
|
143
|
+
|
144
|
+
static VALUE get_max_stop(VALUE self) {
|
145
|
+
return ULL2NUM(primesieve_get_max_stop());
|
146
|
+
}
|
147
|
+
|
148
|
+
static VALUE set_sieve_size(VALUE self, VALUE sieve_size) {
|
149
|
+
primesieve_set_sieve_size(NUM2INT(sieve_size));
|
150
|
+
return Qnil;
|
151
|
+
}
|
152
|
+
|
153
|
+
static VALUE set_num_threads(VALUE self, VALUE num_threads) {
|
154
|
+
primesieve_set_num_threads(NUM2INT(num_threads));
|
155
|
+
return Qnil;
|
156
|
+
}
|
157
|
+
|
158
|
+
static VALUE test(VALUE self) {
|
159
|
+
return INT2NUM(primesieve_test());
|
160
|
+
}
|
161
|
+
|
162
|
+
void Init_prime_sieve() {
|
163
|
+
mPrimeSieve = rb_define_module("PrimeSieve");
|
164
|
+
|
165
|
+
rb_define_module_function(mPrimeSieve, "generate_primes", generate_primes, 2);
|
166
|
+
rb_define_module_function(mPrimeSieve, "generate_n_primes", generate_n_primes, 2);
|
167
|
+
rb_define_module_function(mPrimeSieve, "nth_prime", nth_prime, 2);
|
168
|
+
rb_define_module_function(mPrimeSieve, "parallel_nth_prime", parallel_nth_prime, 2);
|
169
|
+
rb_define_module_function(mPrimeSieve, "count_primes", count_primes, 2);
|
170
|
+
rb_define_module_function(mPrimeSieve, "count_twins", count_twins, 2);
|
171
|
+
rb_define_module_function(mPrimeSieve, "count_triplets", count_triplets, 2);
|
172
|
+
rb_define_module_function(mPrimeSieve, "count_quadruplets", count_quadruplets, 2);
|
173
|
+
rb_define_module_function(mPrimeSieve, "count_quintuplets", count_quintuplets, 2);
|
174
|
+
rb_define_module_function(mPrimeSieve, "count_sextuplets", count_sextuplets, 2);
|
175
|
+
rb_define_module_function(mPrimeSieve, "parallel_count_primes", parallel_count_primes, 2);
|
176
|
+
rb_define_module_function(mPrimeSieve, "parallel_count_twins", parallel_count_twins, 2);
|
177
|
+
rb_define_module_function(mPrimeSieve, "parallel_count_triplets", parallel_count_triplets, 2);
|
178
|
+
rb_define_module_function(mPrimeSieve, "parallel_count_quadruplets", parallel_count_quadruplets, 2);
|
179
|
+
rb_define_module_function(mPrimeSieve, "parallel_count_quintuplets", parallel_count_quintuplets, 2);
|
180
|
+
rb_define_module_function(mPrimeSieve, "parallel_count_sextuplets", parallel_count_sextuplets, 2);
|
181
|
+
rb_define_module_function(mPrimeSieve, "print_primes", print_primes, 2);
|
182
|
+
rb_define_module_function(mPrimeSieve, "print_twins", print_twins, 2);
|
183
|
+
rb_define_module_function(mPrimeSieve, "print_triplets", print_triplets, 2);
|
184
|
+
rb_define_module_function(mPrimeSieve, "print_quadruplets", print_quadruplets, 2);
|
185
|
+
rb_define_module_function(mPrimeSieve, "print_quintuplets", print_quintuplets, 2);
|
186
|
+
rb_define_module_function(mPrimeSieve, "print_sextuplets", print_sextuplets, 2);
|
187
|
+
rb_define_module_function(mPrimeSieve, "callback_primes", callback_primes, 2);
|
188
|
+
rb_define_module_function(mPrimeSieve, "parallel_callback_primes", parallel_callback_primes, 2);
|
189
|
+
rb_define_module_function(mPrimeSieve, "get_sieve_size", get_sieve_size, 0);
|
190
|
+
rb_define_module_function(mPrimeSieve, "get_num_threads", get_num_threads, 0);
|
191
|
+
rb_define_module_function(mPrimeSieve, "get_max_stop", get_max_stop, 0);
|
192
|
+
rb_define_module_function(mPrimeSieve, "set_sieve_size", set_sieve_size, 1);
|
193
|
+
rb_define_module_function(mPrimeSieve, "set_num_threads", set_num_threads, 1);
|
194
|
+
rb_define_module_function(mPrimeSieve, "test", test, 0);
|
195
|
+
}
|
data/lib/prime_sieve.rb
ADDED
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prime_sieve
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert Looby
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-21 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A wrapper for the primesieve C prime number generator
|
14
|
+
email: robertjlooby@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions:
|
17
|
+
- ext/prime_sieve/extconf.rb
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ext/prime_sieve/extconf.rb
|
21
|
+
- ext/prime_sieve/prime_sieve.c
|
22
|
+
- ext/prime_sieve/prime_sieve.h
|
23
|
+
- lib/prime_sieve.rb
|
24
|
+
homepage: https://github.com/robertjlooby/prime_sieve
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 2.2.2
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: A wrapper for the primesieve lib
|
48
|
+
test_files: []
|