Epic Systems Interview Question
Software Engineer / Developersuse rahsonmethod
float x1=num,x2=0;
x2=.5*(x1+num/x1);
while((x1-x2)!=0)
{
x1=x2;
x2=.5*(x1+num/x1);
}
cout<<x2;
Newton - Rahson Method Implementation:
import java.util.Scanner;
public class Squareroot
{
public static void main(String[] args)
{
System.out.println("Please enter a number to find the squareroot");
Scanner scan = new Scanner(System.in);
double num =Integer.parseInt(scan.next());
double x= num;
double y = (0.5)*(x +(num/x));
while(x!=y)
{
x = y;
y= (0.5)*(x +(num/x));
}
System.out.println("Final squareroot of "+num+" ----> "+y);
}
}
public static void SquareRoot(int n)
{
float guess = 1;
float number = (float)n;
while (Math.Abs((number / guess) - guess) > 0.001f)
{
guess = (guess + (number / guess)) / 2;
}
Console.WriteLine("Square Root of " + n + ": " + guess);
}
stupid algo..
first of all u take int n when it should be a float !!
also take n=100
guess = 1
guess = (guess + number/guess)/2 => 51 !!
dumbass
void prob6()
{
float input;
cout<<"Input a non-negative float number!"<<endl;
cin>>input;
while(input<0) //if it is negative, re-input
{
cin>>input;
}
//Find an initial point x0, such that x0^2>input and x0 is minimal
int i=0;
while(true)
{
if(i*i>input)
break;
i++;
}
float x_0=(float)i;
float x_1;
float error=0.01f;
float diff;
while(true)
{
x_1=x_0-(x_0*x_0-input)/(2*x_0);
diff=x_1*x_1-input;
if((diff<error)||(diff>-error))
break;
else
x_0=x_1;
}
cout<<"The approximate (Error<="<<error<<")square-root is:"<<x_1<<endl;
}
#include <iostream>
using namespace std;
static double root;
static double diff;
void getroot(double num,double min, double max)
{
root=(min+max)/2;
diff=root*root-num;
if(0<=diff)
{
if(diff<0.001)
cout<<root;
else
{
max=root;
getroot(num,min,max);
}
}
else
{
min=root;
getroot(num,min,max);
}
}
int main(){
double num;
cout<<"please input the number:"<<endl;
cin>>num;
if(num<1)
getroot(num,num,1);
else
getroot(num,0,num);
}
const double epsilon = 0.00000000000000001;
double sqrt(const double x){
if (x < 0){
throw Exception("wrong input");
}
if (abs(x - 1) < epsilon)
return 1;
double left, mid, right;
if (x < 1){
left = x;
right = 1;
}
else{
left = 1;
right = x;
}
mid = (left + right)/2;
while (abs(mid * mid - x) > epsilon){
if (contains(left, mid, x)
right = mid;
else
left = mid;
mid = (left + right)/2;
}
return mid;
}
int contains(double left, double right, double key){
return (key >= left || key <= right)? 1: 0;
}
#include <iostream>
using namespace std;
/* Function declarations */
double mySqrt(double input);
/* Function definitions */
double mySqrt(double input)
{
double result=0.001;
while(result*result - input < 0.0 )
{
result+=0.001;
}
return result;
}
int main()
{
char key;
double result;
double input=5.67;
result=mySqrt(input);
cout<<result<<endl;
cin>>key;
return 0;
}
<pre lang="" line="1" title="CodeMonkey95907" class="run-this">public void square_root (float num) {
float n1 = 0f;
float n2 = 0f;
n1 = num/2;
/Newton-Raphson's method
while(n1 != n2) {
n1 = n2;
n1 = n2 + (((n2*n2) - num)/(2*n2));
}
System.out.println("Square root is " + n1);
}</pre><pre title="CodeMonkey95907" input="yes">
</pre>
Help.......How to stop and make stable of this output....it keeps on giving output...
#include<stdio.h>
main()
{ int i,j,k=1;
long double a,num,n;
printf("Enter the no whose square root is to find\n");
scanf("%Lf",&n);
i=0;
while(i*i<n)
i++;
a=(double)i-1.000;
printf("\n i is %d, %Lf\n",i,a);
while(a*a<n+1)
{
if(a*a==n)
break;
else
{ if(a*a<n)
a=a+1/(pow(10,k));
else
{ a=a-1/(pow(10,k));
k++;
}
}
printf("The square root of %Lf is %Lf\n",n,a);
}
}
enum Status {kValid = 0, kInvalid};
int g_nStatus = kValid;
float sqrf(const float input)
{
g_nStatus = kInvalid;
double base = 0.0;
if(input >= 0){
float tryNum = 1.0;
int i = 0;
int count = 0;
while(i < 8)
{
count = 0;
float midResult = 0.0;
while(midResult < input)
{
base = base + tryNum;
midResult = base * base;
count ++;
}
base = base - tryNum;
tryNum = tryNum / 10.0;
i++;
}
base = base - count * 0.0000001;
if(count > 5);
base = base + 0.000001;
g_nStatus = kValid;
}
return (float)base;
}
probably a binary search would also work
- Anonymous November 18, 2010for ex
2<sqrt(8)<3;
now try 2.5 since 2.5*2.5=6.25 so try betwen 2.5 and 3 and so on