Here is the code, it looks ok and runs and gives me an accurate answer on other ...
www.dreamincode.net