top | item 4449236

(no title)

zxoq | 13 years ago

I would just write the code to compute it to arbitrary precision and then tell the interviewer that I would test how many iterations are required and hard-code it.

If they insist that it must be solved at runtime, I would check the size of the computed term (1/(1+2x)) and repeat 'til it's less than 10E-6.

discuss

order

cube13|13 years ago

If you're hardcoding it, n will be around 400,000.

That's a lot of iterations.

nemetroid|13 years ago

Besides the slowness of such a solution, that condition is not enough to ensure the first five decimal places are correct.

danielweber|13 years ago

If you are told the formula to use, you use it. No, it's not the bestest way ever of finding pi. It's to make sure the guy sitting across from you can actually write code, which a scary number of people actually cannot do.