1

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();
}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • 3
    If √x = y, then x = y^2, right? Why bother with a loop at all? – Heretic Monkey Feb 06 '18 at 01:39
  • The maximum value for `counter` will naturally be `num²`, but as per @MikeMcCaughan's question, why use a loop at all? – Simon MᶜKenzie Feb 06 '18 at 01:39
  • 1
    Every number is always equal to the suare root of another number. `x` will always just be equal to `√(x^2)`. Not sure why you want to loop here? – DavidG Feb 06 '18 at 01:40
  • 2
    Typically any method that starts with `Is` returns a bool (`Is` represents a yes/no question, like `IsNumeric` or `IsValid`). Consider using the verb `Get` instead, like `GetSqrt`. And a method that returns a number should not return a string. Let the client do the conversion if they want to. – Rufus L Feb 06 '18 at 01:40
  • @MikeMcCaughan I see where you're coming from with not using a loop, but how would I implement that? – jakedacatman Feb 06 '18 at 01:46
  • `public static double GetSquareOfNum(double num) { return num * num; }` – Rufus L Feb 06 '18 at 01:48
  • And I have it return a string so I can have the square root symbol when it returns (it would be easier to remove and reparse than to try and find what's an integer and what's not) – jakedacatman Feb 06 '18 at 01:49
  • @RufusL That just squares the number – jakedacatman Feb 06 '18 at 01:49
  • You Could potentially run into floating point issues with this approach – Ctznkane525 Feb 06 '18 at 01:50
  • @jakedacatman No, `num * 2` doubles the number. `num * num` gets the square. (by definition, `num` is the square root of `num * num`) – Rufus L Feb 06 '18 at 01:50
  • little brain fart there my bad – jakedacatman Feb 06 '18 at 01:50
  • Why don't you try something and see what you come up with, instead of getting others to do your thinking for you. You have the formula, just write it in code. – Heretic Monkey Feb 06 '18 at 01:52
  • I have the max as `num` squared for now, but I'll try to come up with how to implement your way. – jakedacatman Feb 06 '18 at 01:53
  • @jakedacatman Yes, it squares the number, which is exactly what your method is currently doing. Except it's using a formula instead of brute force searching through all possible numbers. – Rufus L Feb 06 '18 at 01:55
  • Now I've realized that I overthink things. Thanks for bringing me back to reality everyone – jakedacatman Feb 06 '18 at 01:55
  • now I'm kind of embarrased – jakedacatman Feb 06 '18 at 01:55

1 Answers1

1

Much simpler method thanks to @Mike McCaughan:

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