This repository was archived by the owner on Dec 12, 2023. It is now read-only.
File tree 2 files changed +75
-0
lines changed
2 files changed +75
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Observation:
3
+
4
+ C Language doesn't have native support to Big Integer numbers,
5
+ so on a x86 processor the maximum factorial number that can be reached is 12
6
+ and on a x86_64 is 20.
7
+
8
+ If you need to use big integer numbers, I recommend you search about the OpenSSL Big Numbers library.
9
+ http://www.openssl.org/docs/crypto/bn.html
10
+
11
+ To print a `unsigned long int` number use `%lu` on prinf() function.
12
+
13
+ */
14
+
15
+ #include "stdio.h"
16
+
17
+ // Interative Loop
18
+ unsigned long int factorial (unsigned short int number ){
19
+ unsigned long int factorial = 1 ;
20
+ unsigned short int i = 2 ;
21
+
22
+ do {
23
+ factorial *= i ++ ;
24
+ } while (i <=number );
25
+ return factorial ;
26
+ }
27
+
28
+ // Interative Reverse Loop
29
+ unsigned long int factorialReverse (unsigned short int number ){
30
+ unsigned long int factorial = 1 ;
31
+ unsigned short int i ;
32
+
33
+ for (i = number ; i > 0 ; i -- ){
34
+ factorial *= i ;
35
+ }
36
+ return factorial ;
37
+ }
38
+
39
+ // Recursive
40
+ unsigned long int factorialRecursive (unsigned short int number ){
41
+ if (number == 1 ) {
42
+ return 1 ;
43
+ } else {
44
+ return number * factorialRecursive (number - 1 );
45
+ }
46
+ }
Original file line number Diff line number Diff line change
1
+ class Integer
2
+
3
+ # Iterative
4
+ def factorial_simple number
5
+ fat = 1
6
+ for n in 2 ..number do
7
+ fat = fat * n
8
+ end
9
+ fat
10
+ end
11
+
12
+ # Iterative Reverse Loop
13
+ def factorial_reverse number
14
+ ( n -1 ) . downto ( 1 ) . each { |i | n *= i }
15
+ n
16
+ end
17
+
18
+ # Recursive - Ternary Operator
19
+ def factorial_recursive number
20
+ ( number == 1 ) ? 1 : ( number * factorial_recursive ( number -1 ) )
21
+ end
22
+
23
+ # The Ruby-iest version, using reduce function
24
+ def factorial_reduce ( number )
25
+ ( 1 ..number ) . reduce ( :* )
26
+ end
27
+
28
+ alias_method :factorial :factorial_reduce
29
+ end
You can’t perform that action at this time.
0 commit comments