jakedacatman
jakedacatman

Reputation: 21

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

Upvotes: 1

Views: 576

Answers (1)

jakedacatman
jakedacatman

Reputation: 21

Much simpler method thanks to @Mike McCaughan:

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

Upvotes: 1

Related Questions