0

我想看看一个数字是否等于另一个数字的平方根。我写了一个方法来实现这一点,但它会搜索到最大值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();
}
4

1 回答 1

0

感谢@Mike McCaughan,方法更简单:

public static string GetSqrOfNum(double num)
{
    return "√" + (num*num).ToString();
}
于 2018-02-06T01:59:52.227 回答