简体   繁体   English

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

[英]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). 我编写了一种方法来实现此目的,但是它将一直搜索到最大Int32值(这将花费很长时间)。 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. 我真的很想搜索大于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();
}

Much simpler method thanks to @Mike McCaughan: 多亏@Mike McCaughan,方法简单得多:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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