检查数字是否等于另一个数字的平方根c#

时间:2018-02-06 01:32:03

标签: c# math.sqrt

我想看看一个数字是否等于另一个数字的平方根。我写了一个方法来实现这一点,但它会搜索到最大Int32值(这需要很长时间)。我真的想搜索超过100的数字(我现有的限制),但我不确定最大值应该是多少。

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();
}

1 个答案:

答案 0 :(得分:0)

感谢@Mike McCaughan提供了更简单的方法:

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