Pergunta de entrevista da empresa Meta

calculate x^1/2

Respostas da entrevista

Sigiloso

14 de abr. de 2012

You can calculate this efficiently using a numerical method which is a twist on binary search. 1- Let z = 0 2- Let y = x+z/2 3- Calculate y^y If equal return y else if smaller than x let z = y else let x = y 4- Repeat

Sigiloso

22 de abr. de 2012

based on netwon raphson method; x(n+1)=x(n)+a/x(n); is the recursion equation for sqrt(a); double sqrt(int a, int err) { if(aerr) { prev=ans; ans=ans+(a*1.0/ans); diff=ans-prev; diff=diff<0?-diff:diff; diff/=prev; } return ans; }

Sigiloso

22 de abr. de 2012

a couple of mistakes ans=(ans+a/ans); replaced by ans=(ans+a/ans)*0.5; and err is double not int sorry

Sigiloso

9 de nov. de 2012

http://collabedit.com/tuvng

Sigiloso

2 de abr. de 2012

x to the power 1/2 is sqrt(x) - so implement sqrt of x

1