A numerical simulation of the formation of the terrestrial planets