factorials 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -0
- data/ext/factorials/factorials.c +27 -16
- data/lib/factorials/factorials.bundle +0 -0
- data/lib/factorials/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 28d71a92bcac85e5e2cbfb717158af14b002989c
|
4
|
+
data.tar.gz: ca8354d31f2c9bdb9184df5b6d498ebba60ecdc7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 545efa4dd1e5bc1f393d9a5a7ac0440139ec8aed606f6d51f3e6d3dd218d07ba693d9090687eab30796fb53a228776d42c495b34784e0c811866a888b9b8d75b
|
7
|
+
data.tar.gz: 444cc736f15e064600d280cbea83ae6b7398469c88cafd81a47356ac6225eec3d9fb881b88684541253db2ab822e649b06d3a1f64d6492a50d55a10a2e95b90a
|
data/README.md
CHANGED
@@ -38,6 +38,13 @@ $ gem install factorials
|
|
38
38
|
# => 3840
|
39
39
|
```
|
40
40
|
|
41
|
+
### Super Factorial
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
5.superfactorial
|
45
|
+
# => 34560
|
46
|
+
```
|
47
|
+
|
41
48
|
## Benchmark
|
42
49
|
|
43
50
|
Conditions:
|
@@ -70,6 +77,10 @@ Factorial (Ruby) 4.720000 2.400000 7.120000 ( 7.787638)
|
|
70
77
|
Factorial (C) 0.000000 0.000000 0.000000 ( 0.000131)
|
71
78
|
```
|
72
79
|
|
80
|
+
## TODO
|
81
|
+
|
82
|
+
* Support very long long long integers.
|
83
|
+
|
73
84
|
## Contributing
|
74
85
|
|
75
86
|
1. Fork it (https://github.com/droptheplot/factorials/fork)
|
data/ext/factorials/factorials.c
CHANGED
@@ -1,15 +1,22 @@
|
|
1
1
|
#include <ruby.h>
|
2
2
|
#include <math.h>
|
3
3
|
|
4
|
-
|
4
|
+
static long factorial(long num)
|
5
5
|
{
|
6
|
-
long
|
6
|
+
long result = 1, i;
|
7
7
|
|
8
|
-
for(i = 1; i <=
|
8
|
+
for(i = 1; i <= num; i++) {
|
9
9
|
result = result * i;
|
10
10
|
}
|
11
11
|
|
12
|
-
return
|
12
|
+
return result;
|
13
|
+
}
|
14
|
+
|
15
|
+
VALUE factorials_num_factorial(VALUE num)
|
16
|
+
{
|
17
|
+
long value = NUM2INT(num);
|
18
|
+
|
19
|
+
return INT2NUM(factorial(value));
|
13
20
|
}
|
14
21
|
|
15
22
|
VALUE factorials_num_double_factorial(VALUE num)
|
@@ -17,20 +24,23 @@ VALUE factorials_num_double_factorial(VALUE num)
|
|
17
24
|
long value = NUM2INT(num), result = 1;
|
18
25
|
|
19
26
|
if(value % 2 == 0) {
|
20
|
-
|
21
|
-
pow(2, value / 2) * NUM2INT(
|
22
|
-
factorials_num_factorial(INT2NUM(value / 2))
|
23
|
-
)
|
24
|
-
);
|
27
|
+
result = pow(2, value / 2) * factorial(value / 2);
|
25
28
|
} else {
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
result = factorial(value) / (pow(2, (value - 1) / 2) * factorial((value - 1) / 2));
|
30
|
+
}
|
31
|
+
|
32
|
+
return INT2NUM(result);
|
33
|
+
}
|
34
|
+
|
35
|
+
VALUE factorials_num_superfactorial(VALUE num)
|
36
|
+
{
|
37
|
+
long value = NUM2INT(num), result = 1, i;
|
38
|
+
|
39
|
+
for(i = 1; i <= value; i++) {
|
40
|
+
result = result * factorial(i);
|
33
41
|
}
|
42
|
+
|
43
|
+
return INT2NUM(result);
|
34
44
|
}
|
35
45
|
|
36
46
|
void Init_factorials(void)
|
@@ -41,4 +51,5 @@ void Init_factorials(void)
|
|
41
51
|
|
42
52
|
rb_define_method(rb_cFixnum, "factorial", factorials_num_factorial, 0);
|
43
53
|
rb_define_method(rb_cFixnum, "double_factorial", factorials_num_double_factorial, 0);
|
54
|
+
rb_define_method(rb_cFixnum, "superfactorial", factorials_num_superfactorial, 0);
|
44
55
|
}
|
Binary file
|
data/lib/factorials/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: factorials
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Novikov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: awesome_print
|