Efficient optical implementation of the Bernstein-Vazirani algorithm