Skip to content

Instantly share code, notes, and snippets.

@shawnsmithdev
Last active November 23, 2015 17:03
Show Gist options
  • Save shawnsmithdev/c6a1ec8cf6e0d8779c82 to your computer and use it in GitHub Desktop.
Save shawnsmithdev/c6a1ec8cf6e0d8779c82 to your computer and use it in GitHub Desktop.
Base-10 digits in factorial
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