solved prob451 :D. The actual result was gained using java though, but the code structure is practically isomorphic.