Last active
September 20, 2022 21:15
-
-
Save costajob/e3e08ddd130ee16324fb6988355bf3e1 to your computer and use it in GitHub Desktop.
Factorial implementation using Big numbers in Ruby, GO and Crystal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "big_int" | |
def fact(n) | |
return 1 if n == 0 | |
n * fact(n - 1) | |
end | |
n = BigInt.new(ARGV[0]) | |
puts fact(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/big" | |
"os" | |
"strconv" | |
) | |
var z, o = big.NewInt(0), big.NewInt(1) | |
func main() { | |
n, _ := strconv.Atoi(os.Args[1]) | |
b := big.NewInt(int64(n)) | |
fmt.Println(fact(b)) | |
} | |
func fact(n *big.Int) (f *big.Int) { | |
if n.Cmp(z) == 0 { | |
f = o | |
} else { | |
var t1, t2 big.Int | |
f = t1.Mul(n, fact(t2.Sub(n, o))) | |
} | |
return | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fact(n) | |
return 1 if n == 0 | |
n * fact(n - 1) | |
end | |
puts fact(ARGV[0].to_i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "big_int" | |
def mul_range(a, b) | |
return 0 if a == 0 | |
return 1 if a > b | |
return a if a == b | |
return a * b if a + 1 == b | |
m = (a + b) / 2 | |
mul_range(a, m) * mul_range(m + 1, b) | |
end | |
puts mul_range(1, ARGV[0].to_big_i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/big" | |
"os" | |
"strconv" | |
) | |
func main() { | |
n, _ := strconv.Atoi(os.Args[1]) | |
fac := big.NewInt(1) | |
fac.MulRange(int64(1), int64(n)) | |
fmt.Println(fac) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mul_range(a, b) | |
return 0 if a == 0 | |
return 1 if a > b | |
return a if a == b | |
return a * b if a + 1 == b | |
m = (a + b) / 2 | |
mul_range(a, m) * mul_range(m + 1, b) | |
end | |
puts mul_range(1, ARGV[0].to_i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just for curiosity: reimplemented MulRange in Crystal and Ruby (updated gist).
Here's the benchmarks (augmented N to 300_000):
Crystal:
GO:
Ruby: