Factorial: Difference between revisions

Content deleted Content added
Sonia (talk | contribs)
Prime swing algorithm
Sonia (talk | contribs)
Line 749:
<lang golfscript>{.1<{;1}{.(fact*}if}:fact;</lang>
=={{header|Go}}==
Iterative, sequential, but at least handling big numbers:
<lang go>
package main
 
import (
"big"
"fmt"
)
 
func main() {
fmt.Println(factorial(800))
}
 
func factorial(n int64) *big.Int {
if n < 0 {
return nil
}
r := big.NewInt(1)
var f big.Int
for i := int64(2); i <= n; i++ {
r.Mul(r, f.SetInt64(i))
}
return r
}
</lang>
Built in function currently uses a simple divide and conquer technique. It's a step up from sequential multiplication.
<lang go>
Line 764 ⟶ 789:
 
func main() {
fmt.Println(factorial(100800))
}
</lang>
For a bigger step up, here is an implementation of one of Peter Luschny's [http://www.luschny.de/math/factorial/FastFactorialFunctions.htm fast factorial algorithms]. Fast as this algorithm is, I believe there is room to speed it up more with parallelization and attention to cache effects. The Go library has a nice Karatsuba multiplier but it is yet single threaded.
<lang go>
package main