Here's a little time-killer project i made in an hour or two.
This program will take a number and calculate the square root, tell you what my formula got, what my rounder gets, and what the sqrt() function calculates.
>= x.5 rounds to x + 1. Less rounds to x.
Source:
#include <iostream>
#include <cmath>
unsigned round(long long m)
{
long long n2 = m;
if( m - n2 >= .5)
{
m = ++n2;
}
else if( m - n2 <.5 && m - n2 > 0.0)
{
m = n2;
}
else
{
m = m;
}
return m;
}
unsigned int_sqrt(unsigned long long x)
{
float i=0;
float x1,x2;
while( (i*i) < x )
{
i+=0.1;
}
x1=i;
for(int j=0;j<10;j++)
{
x2=x;
x2/=x1;
x2+=x1;
x2/=2;
x1=x2;
}
std::cout << "The square root is: " << x2 << std::endl;
std::cout << "The rounded integer is " << round(x2) << std::endl;
std::cout << "sqrt() says: " << std::sqrt(x) << std::endl;
return 0;
}
int main()
{
unsigned long long number;
std::cout << "Type in a number: ";
std::cin >> number;
int_sqrt(number);
std::cin.get();
std::cin.ignore();
return 0;
}
I'll have the download up in a sec...