开发者

To find the nearest perfect square if the number entered is not a perfect square?

#include <iostream>
#include <limits>
#include <cmath>

using namespace std;

int main()
{
    int number;
    cout <开发者_JAVA百科< "Enter the number whose sqare root needs to be calculated";
    cin >> number;
    cout << "Square root of " << number << " is " << (int)sqrt((float)number)  << " OR " << sqrt((float)number) << endl;
    if( (int)sqrt((float)number) == sqrt((float)number) )
    cout << "The number is a perfect sqaure";
    else
    cout << "The number is not a perfect square";
    //To find the nearest perfect square if the number entered
   // is not a perfect square?

    return 0;
}

I hope what i have done to check the perfect squares is OK, but furthermore I want to find out the number nearest perfect square if the number entered is not a perfect square Any Ideas


Actually, here is the better answer:

int number = 13;
int iRoot = static_cast<int>(sqrt(static_cast<float>(number)) + .5f);

You don't need to check between the ceil or the floor of which is greater, doing a simple round does the trick.

sqrt(13) is 3.6 and when you add .5 casts to 4. sqrt(12) is 3.46 and when you add .5 casts to 3. (we're trying to round, that's why we add the .5). As you can see, when number is closer to the higher root, it'll give you a decimal greater than .5; when the number is closer to a lower value root, the decimal is less than .5, simple as that!


Start by finding the floor and ceiling of the root:

float root = sqrt((float)number);
int floor = (int)root;
int ceil = floor + 1

Then just check which is closer out of ceil * ceil and floor * floor.


Square the integers next to the root.


Let r be int(sqrt(double(number))+0.5). Then you need to check which of (r-1)*(r-1), r*r and (r+1)*(r+1) is the nearest to number. That's all.


you need to find out the two power of two for: (int)sqrt((float)number) and ((int)sqrt((float)number)+1), (denote them by d1,d2) and find out min{|number-d1|,|number-d2|}.
also, to improve performance, I would have cached the float sqrt((float)number) as a variable and use it (instead of calculating again sqrt() )


Use the same check to see if a number is a perfect square:

if( (int)sqrt((float)number) == sqrt((float)number) )

And apply it successively to the integers above and below the numbers specified by the user.

int delta=1;
int perfectSquare = 0;
bool perfectSquareFound = false;
while(!perfectSquareFound)
{
   int above = number+delta;
   int below = number-delta;
   float aboveSquareRoot = sqrt((float)above);
   float belowSquareRoot = sqrt((float)below);
   if( (int)aboveSquareRoot == aboveSquareRoot )
   {
      perfectSquareFound = true;
      perfectSquare = above;
   }
   else if( (int)belowSquareRoot == belowSquareRoot )
   {
      perfectSquareFound = true;
      perfectSquare = below;
   }
}


Here is my answer

int main()
{

    long long int n, ans;
    cin >> n;
    ans = round(sqrt(n));
    cout << ans<< endl;
}


Should be like (psuedo code),

int sqrtValue = (int)sqrt((float)number);  // store in a temporary value
int lowerPerfectSquare = pow(sqrtValue, 2);
int higerPerfectSquare = pow(sqrtValue + 1, 2);
int nearPerfectSquare = (higerPerfectSquare - number) < (number - lowerPerfectSquare)?
                        sqrtValue + 1 : sqrtValue;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜