solve Problem#8 by BF