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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6afc96035b9e66fb37ff14bd970cc7eff458eb23
4
- data.tar.gz: 6e761be02a9d636f1d49be09784461c1d1e2733d
3
+ metadata.gz: 28d71a92bcac85e5e2cbfb717158af14b002989c
4
+ data.tar.gz: ca8354d31f2c9bdb9184df5b6d498ebba60ecdc7
5
5
  SHA512:
6
- metadata.gz: f45172bc825cdb9304338d7c2b31f6ea7bf0308917b73d839a5cf0d2a4f2848d48b98911726c982f3a7ee3d0271a2708eeed87bd440a7a1534b07e5459cfb095
7
- data.tar.gz: e9c4d3100a29a9d7da108ed8fa65d0e5b72d3961d21478daf3a7adf347a7978cd395008d98cce75728ff9bf4af07b1e9734d55d52042e7361277370081699cc1
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)
@@ -1,15 +1,22 @@
1
1
  #include <ruby.h>
2
2
  #include <math.h>
3
3
 
4
- VALUE factorials_num_factorial(VALUE num)
4
+ static long factorial(long num)
5
5
  {
6
- long value = NUM2INT(num), result = 1, i;
6
+ long result = 1, i;
7
7
 
8
- for(i = 1; i <= value; i++) {
8
+ for(i = 1; i <= num; i++) {
9
9
  result = result * i;
10
10
  }
11
11
 
12
- return INT2NUM(result);
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
- return INT2NUM(
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
- 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
- );
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
  }
@@ -1,3 +1,3 @@
1
1
  module Factorials
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.0.2'.freeze
3
3
  end
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.1
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-22 00:00:00.000000000 Z
11
+ date: 2016-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print