eratostene_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/.gitignore +17 -0
- data/.rspec +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +8 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +6 -0
- data/eratostene_sieve.gemspec +25 -0
- data/lib/eratostene_sieve.rb +40 -0
- data/lib/eratostene_sieve/railtie.rb +9 -0
- data/lib/eratostene_sieve/version.rb +4 -0
- data/spec/eratostene_sieve_spec.rb +25 -0
- data/spec/spec_helper.rb +5 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 81ec8dafae3375fe3e780ca0b2628a0a034c1b69
|
4
|
+
data.tar.gz: 0cc4f60a248d6573c4801c6333f8b93ce35c4807
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8d35cea9876b38fd68baa75b4bf1deb26730dbd3027486497449df9c0aabd353b1fdac38a5fa223054a4dd267db8ae6d40bdf9f7e9b75b8df4b70716d1a445d3
|
7
|
+
data.tar.gz: a390c0c589cc403b3380100da61edc0a3ca43f56864e9af337a11b8471a90274222a68262aa2b4e580f81b4de7d68dcad97cde246aab68bd0dfc43f6d09c7464
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.0
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Andrea Salicetti
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/knightq/eratostene_sieve.png?branch=master)](https://travis-ci.org/knightq/eratostene_sieve)
|
2
|
+
[![Dependency Status](https://gemnasium.com/knightq/eratostene_sieve.png)](https://gemnasium.com/knightq/eratostene_sieve)
|
3
|
+
[![Coverage Status](https://coveralls.io/repos/knightq/eratostene_sieve/badge.png)](https://coveralls.io/r/knightq/eratostene_sieve)
|
4
|
+
[![GitHub version](https://badge.fury.io/gh/knightq%2Feratostene_sieve.png)](http://badge.fury.io/gh/knightq%2Feratostene_sieve)
|
5
|
+
|
6
|
+
# EratosteneSieve
|
7
|
+
|
8
|
+
This gem provides you a set of minimal utility functions to manage prime numbers.
|
9
|
+
|
10
|
+
## Changelog
|
11
|
+
|
12
|
+
- **1.0.0**: initial release. It provides just these class functions:
|
13
|
+
|
14
|
+
- `nth_prime(n)` : retun the nth prime number.
|
15
|
+
- `prime_serie` : return an enumerator over the prime serie.
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
gem 'eratostene_sieve'
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install eratostene_sieve
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
Just require eratostene_sieve wherever you want to user a function it provides:
|
34
|
+
|
35
|
+
require 'eratostene_sieve'
|
36
|
+
|
37
|
+
Then call the function you like in your code:
|
38
|
+
|
39
|
+
# nth_prime(n) example
|
40
|
+
EratosterneSieve.nth_prime(10) # => 29
|
41
|
+
|
42
|
+
# prime_serie example
|
43
|
+
enumerator = EratosteneSieve.prime_serie
|
44
|
+
serie = []
|
45
|
+
(0..9).each { |i| serie << enumerator.next }
|
46
|
+
puts serie # => [1, 2, 3, 5, 7, 11, 13, 17, 19, 23]
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it ( http://github.com/<my-github-username>/eratostene_sieve/fork )
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'eratostene_sieve/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'eratostene_sieve'
|
8
|
+
spec.version = EratosteneSieve::VERSION
|
9
|
+
spec.authors = ['Andrea Salicetti']
|
10
|
+
spec.email = ['andrea.salicetti@gmail.com']
|
11
|
+
spec.summary = %q{A bunch of utilities to play with prime numbers.}
|
12
|
+
spec.description = %q{Gem to play with prime numbers}
|
13
|
+
spec.homepage = 'http://www.andreasalicetti.com'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(/^(test|spec|features)\//)
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
22
|
+
spec.add_development_dependency 'pry'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
spec.add_development_dependency 'rspec'
|
25
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'eratostene_sieve/version'
|
2
|
+
require 'eratostene_sieve/railtie' if defined? Rails
|
3
|
+
|
4
|
+
# Utility to get:
|
5
|
+
# - nth_prime(n): the nth prime number.
|
6
|
+
# - prime_serie: an enumerator on prime number serie.
|
7
|
+
module EratosteneSieve
|
8
|
+
Inf = 1.0 / 0.0
|
9
|
+
|
10
|
+
def self.nth_prime(index)
|
11
|
+
return first_primes[index] if index < first_primes.size
|
12
|
+
counter = first_primes.size
|
13
|
+
((first_primes.last + 2)..Inf).step(2) do |candidate|
|
14
|
+
(2..((candidate**0.5).to_i)).each do |m|
|
15
|
+
break if candidate % m == 0
|
16
|
+
counter += 1 if m == (candidate**0.5).to_i
|
17
|
+
end
|
18
|
+
return candidate.to_i if counter == index
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.prime_serie
|
23
|
+
Enumerator.new do |y|
|
24
|
+
a = 1
|
25
|
+
b = 2
|
26
|
+
index = 2
|
27
|
+
loop do
|
28
|
+
y << a
|
29
|
+
a, b = b, nth_prime(index)
|
30
|
+
index += 1
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def self.first_primes
|
38
|
+
@first_primes ||= [1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe EratosteneSieve do
|
4
|
+
|
5
|
+
describe '.nth_prime' do
|
6
|
+
it 'should output the nth prime number' do
|
7
|
+
EratosteneSieve.nth_prime(1).should be_eql(2)
|
8
|
+
EratosteneSieve.nth_prime(2).should be_eql(3)
|
9
|
+
EratosteneSieve.nth_prime(3).should be_eql(5)
|
10
|
+
EratosteneSieve.nth_prime(4).should be_eql(7)
|
11
|
+
EratosteneSieve.nth_prime(10).should be_eql(29)
|
12
|
+
EratosteneSieve.nth_prime(20).should be_eql(67)
|
13
|
+
EratosteneSieve.nth_prime(30).should be_eql(109)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '.prime_serie' do
|
18
|
+
it 'should return ' do
|
19
|
+
enumerator = EratosteneSieve.prime_serie
|
20
|
+
serie = []
|
21
|
+
(0..9).each { |i| serie << enumerator.next }
|
22
|
+
serie.should == [1, 2, 3, 5, 7, 11, 13, 17, 19, 23]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: eratostene_sieve
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrea Salicetti
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Gem to play with prime numbers
|
70
|
+
email:
|
71
|
+
- andrea.salicetti@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".ruby-version"
|
79
|
+
- ".travis.yml"
|
80
|
+
- CHANGELOG.md
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- eratostene_sieve.gemspec
|
86
|
+
- lib/eratostene_sieve.rb
|
87
|
+
- lib/eratostene_sieve/railtie.rb
|
88
|
+
- lib/eratostene_sieve/version.rb
|
89
|
+
- spec/eratostene_sieve_spec.rb
|
90
|
+
- spec/spec_helper.rb
|
91
|
+
homepage: http://www.andreasalicetti.com
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.2.0
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: A bunch of utilities to play with prime numbers.
|
115
|
+
test_files:
|
116
|
+
- spec/eratostene_sieve_spec.rb
|
117
|
+
- spec/spec_helper.rb
|