Go (2)


How many trailing Zeros does the result of 1000! have? Brute-force solution with Google Go

A lot of tech companies ask interesting questions when they hire employees. One of these questions is: How many trailing zeros are there in 1000! (1 × 2 × 3 × .. × 1000).
As you might imagine, the actual result of 1000! is a rather large number, something you can hardly do in your mind, at least unless you’re some kind of savant.

However, there’s a solution you can come up with that doesn’t need a calculator or anything digital. Spoiler alert: There are certain multiplies that give you a zero at the end. Google knows the answer if you’re wondering how to solve this riddle with a paper and a pen.

I’ve been playing around with Google’s language “Go” for a while and wondered if I could solve this riddle using a more brute-force like solution. The code is rather simple, there’s a factorial function I needed to add because the result is too big for a data type like double or int. I then call that function and count the number of zeros using a simple regex pattern. Something you can do when you’re in an interview but it works as well and is also pretty fast.

package main
 
import "fmt"
import "math/big"
import "regexp"
 
func main() {
	c := int64(1000)
	f := big.NewInt(c)
	s := factorial(f)
	r := regexp.MustCompile(`[0]+$`)
 
	l := r.FindString(s.String())   
	fmt.Printf("There are %d zeros at the end of %d!\n", len(l), c);
}
 
func factorial(n *big.Int) (result *big.Int) {
	result = new(big.Int)
 
	switch n.Cmp(&big.Int{}) {
	case -1, 0:
		result.SetInt64(1)
	default:
		result.Set(n)
		var one big.Int
		one.SetInt64(1)
		result.Mul(result, factorial(n.Sub(n, &one)))
	}
	return
}



Statically linked Linux executables with Go

You might have seen my previous article about creating executables without dependencies http://www.codeblog.ch/2011/06/statically-linked-linux-executables/. While I still try to focus on Oracle and concrete5, there’s often a situation where I have to build a small, portable and fast tool. I didn’t look at something specific, I rather tried several solutions and now it’s time to add another language to this list.

Google created Go a while ago and released version 1.0 at the end of March 2012. Some might wonder why Google created yet another new language. You can find the official answer here: http://golang.org/doc/go_faq.html#What_is_the_purpose_of_the_project. To me the most important things worth mentioning are:

  • Fully garbage-collected
  • Support for concurrent execution and communication
  • Doesn’t need a virtual machine like JRE
  • Construction of system software on multicore machines

This doesn’t mean that Go is restricted to those things, you does a lot more but when comparing it to Java and other languages, these are a few major points of interest. I’ve run my experiment on a freshly installed Debian 6.0 operating system. My favourite distribution as it’s small and efficient, I can’t give you instructions for Fedora and other system but if you’re familiar with some of the basic libraries for your distribution, it shouldn’t be too difficult to get Go up and running.