简体   繁体   中英

check if number is equal to the square root of another number c#

I want to see if a number is equal to the square root of another. I wrote a method to achieve this, but it would search until the maximum Int32 value (which would take a long time). I really would like to search beyond numbers greater than 100 (the current limit I have in place), but I'm not sure what the maximum should be.

public static string IsSqrtOfNum(double num, int counter = 1)
{
    while (true)
    {
        if (Math.Sqrt(counter) == num)
        {
            return "√" + counter.ToString();
        }
        if (counter >= 100) break;
        counter++;
    }
    return num.ToString();
}

Much simpler method thanks to @Mike McCaughan:

public static string GetSqrOfNum(double num)
{
    return "√" + (num*num).ToString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM