factorials 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/MIT-LICENSE +20 -0
- data/README.md +83 -0
- data/Rakefile +6 -0
- data/ext/factorials/extconf.rb +5 -0
- data/ext/factorials/factorials.c +44 -0
- data/lib/factorials.rb +4 -0
- data/lib/factorials/factorials.bundle +0 -0
- data/lib/factorials/version.rb +3 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6afc96035b9e66fb37ff14bd970cc7eff458eb23
|
4
|
+
data.tar.gz: 6e761be02a9d636f1d49be09784461c1d1e2733d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f45172bc825cdb9304338d7c2b31f6ea7bf0308917b73d839a5cf0d2a4f2848d48b98911726c982f3a7ee3d0271a2708eeed87bd440a7a1534b07e5459cfb095
|
7
|
+
data.tar.gz: e9c4d3100a29a9d7da108ed8fa65d0e5b72d3961d21478daf3a7adf347a7978cd395008d98cce75728ff9bf4af07b1e9734d55d52042e7361277370081699cc1
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Sergey Novikov
|
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.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# Factorials
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/factorials.svg)](https://badge.fury.io/rb/factorials)
|
4
|
+
|
5
|
+
Ruby factorials as C extension.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'factorials'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
```bash
|
17
|
+
$ bundle
|
18
|
+
```
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
```bash
|
22
|
+
$ gem install factorials
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### Factorial
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
10.factorial
|
31
|
+
# => 3628800
|
32
|
+
```
|
33
|
+
|
34
|
+
### Double Factorial
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
10.double_factorial
|
38
|
+
# => 3840
|
39
|
+
```
|
40
|
+
|
41
|
+
## Benchmark
|
42
|
+
|
43
|
+
Conditions:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
Benchmark.bmbm(7) do |x|
|
47
|
+
int = 100_000
|
48
|
+
|
49
|
+
x.report('Factorial (Ruby)') do
|
50
|
+
(1..int).inject(:*)
|
51
|
+
end
|
52
|
+
|
53
|
+
x.report('Factorial (C)') do
|
54
|
+
int.factorial
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
```
|
59
|
+
|
60
|
+
Results:
|
61
|
+
|
62
|
+
```shell
|
63
|
+
Rehearsal ----------------------------------------------------
|
64
|
+
Factorial (Ruby) 4.710000 2.320000 7.030000 ( 7.495018)
|
65
|
+
Factorial (C) 0.000000 0.000000 0.000000 ( 0.000156)
|
66
|
+
------------------------------------------- total: 7.030000sec
|
67
|
+
|
68
|
+
user system total real
|
69
|
+
Factorial (Ruby) 4.720000 2.400000 7.120000 ( 7.787638)
|
70
|
+
Factorial (C) 0.000000 0.000000 0.000000 ( 0.000131)
|
71
|
+
```
|
72
|
+
|
73
|
+
## Contributing
|
74
|
+
|
75
|
+
1. Fork it (https://github.com/droptheplot/factorials/fork)
|
76
|
+
2. Create your feature branch (git checkout -b my-new-feature)
|
77
|
+
3. Commit your changes (git commit -am 'Add some feature')
|
78
|
+
4. Push to the branch (git push origin my-new-feature)
|
79
|
+
5. Create new Pull Request
|
80
|
+
|
81
|
+
## License
|
82
|
+
|
83
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include <math.h>
|
3
|
+
|
4
|
+
VALUE factorials_num_factorial(VALUE num)
|
5
|
+
{
|
6
|
+
long value = NUM2INT(num), result = 1, i;
|
7
|
+
|
8
|
+
for(i = 1; i <= value; i++) {
|
9
|
+
result = result * i;
|
10
|
+
}
|
11
|
+
|
12
|
+
return INT2NUM(result);
|
13
|
+
}
|
14
|
+
|
15
|
+
VALUE factorials_num_double_factorial(VALUE num)
|
16
|
+
{
|
17
|
+
long value = NUM2INT(num), result = 1;
|
18
|
+
|
19
|
+
if(value % 2 == 0) {
|
20
|
+
return INT2NUM(
|
21
|
+
pow(2, value / 2) * NUM2INT(
|
22
|
+
factorials_num_factorial(INT2NUM(value / 2))
|
23
|
+
)
|
24
|
+
);
|
25
|
+
} else {
|
26
|
+
return INT2NUM(
|
27
|
+
NUM2INT(factorials_num_factorial(num)) / (
|
28
|
+
pow(2, (value - 1) / 2) * NUM2INT(
|
29
|
+
factorials_num_factorial(INT2NUM((value - 1) / 2))
|
30
|
+
)
|
31
|
+
)
|
32
|
+
);
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
void Init_factorials(void)
|
37
|
+
{
|
38
|
+
rb_cNumeric = rb_define_class("Numeric", rb_cObject);
|
39
|
+
rb_cInteger = rb_define_class("Integer", rb_cNumeric);
|
40
|
+
rb_cFixnum = rb_define_class("Fixnum", rb_cInteger);
|
41
|
+
|
42
|
+
rb_define_method(rb_cFixnum, "factorial", factorials_num_factorial, 0);
|
43
|
+
rb_define_method(rb_cFixnum, "double_factorial", factorials_num_double_factorial, 0);
|
44
|
+
}
|
data/lib/factorials.rb
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: factorials
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergey Novikov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: awesome_print
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rubocop
|
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: rspec
|
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: rake-compiler
|
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:
|
70
|
+
email:
|
71
|
+
- novikov359@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions:
|
74
|
+
- ext/factorials/extconf.rb
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- MIT-LICENSE
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- ext/factorials/extconf.rb
|
81
|
+
- ext/factorials/factorials.c
|
82
|
+
- lib/factorials.rb
|
83
|
+
- lib/factorials/factorials.bundle
|
84
|
+
- lib/factorials/version.rb
|
85
|
+
homepage: https://github.com/droptheplot/factorials
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.5.1
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Factorials as C extension.
|
109
|
+
test_files: []
|