Last active
November 23, 2015 17:03
-
-
Save shawnsmithdev/c6a1ec8cf6e0d8779c82 to your computer and use it in GitHub Desktop.
Base-10 digits in factorial
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" | |
) | |
// http://www.johndcook.com/blog/2015/10/06/number-of-digits-in-n/ | |
func digitsInFactorial(x int) int { | |
if x <= 0 { | |
panic("bad argument") | |
} | |
lg, _ := math.Lgamma(float64(x) + 1) | |
return int(math.Floor(lg/math.Log(10)) + 1) | |
} | |
func main() { | |
fmt.Println("x -> digits in x! (matches?)") | |
fmt.Println("===================") | |
for x := 1; x <= 100; x++ { | |
lg := digitsInFactorial(x) | |
fmt.Printf("%v -> %v (%v)\n", x, lg, x == lg) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment