-3

Is there a fast and simple way to write a program in C#, that finds out if a big (something like 25 digits big) number is a perfect square or not?

Perfect squares are the numbers: 0^2=0,1^2=1,2^2=4,3^2=9,4^2=16,...

  • What did you try so far? – diiN__________ Nov 21 '19 at 12:04
  • 1
    @diiN__________ well I'm more a math guy, so my programming skills stops while using Wolfram Mathematica. Now I want to use C#, but I've no clue whatsoever to even start ;) – Jan Eerland Nov 21 '19 at 12:05
  • 1
    Do you have a mathematical formula that can detect this? Converting formulas into C# code is a very simple process. – Longoon12000 Nov 21 '19 at 12:06
  • @Longoon12000 No, there are ony properties of perfect numbers that can be used to rule out numbers that can not be a perfect square (https://www.ask-math.com/properties-of-square-numbers.html). – Jan Eerland Nov 21 '19 at 12:08
  • Try Newton method: https://www.math.upenn.edu/~kazdan/202F09/sqrt.pdf – Dmitry Bychenko Nov 21 '19 at 12:08
  • 2
    I'm pretty sure that something like "if the square root of a number is a natural number then it's a perfect square" would be a good start? – Longoon12000 Nov 21 '19 at 12:12
  • Before trying to bake that into C#-code, try to formulate descrbe what you want to achieve in some logic steps. How would you achieve what you want step by step without even writing a single line of code? Langooon gave a good start. – MakePeaceGreatAgain Nov 21 '19 at 13:16
  • 1
    This has been asked an answered [here](https://stackoverflow.com/q/2489435/238704). Almost all those answers are easy to translate to C#. – President James K. Polk Nov 21 '19 at 14:43

1 Answers1

0

You can not convert any 25 digit number into ulong or long or int. But you can try if the number is ulong

public bool test(ulong num)
      { var sqrt = Math.Sqrt(num);
        double dbl = Convert.ToDouble(sqrt);
        long dblint = Convert.ToInt64(sqrt);
        if (dbl == dblint)
        {
            return true;
        }        

        return false;
    }
canton7
  • 37,633
  • 3
  • 64
  • 77
Md. Asaduzzaman
  • 137
  • 1
  • 12
  • How come you say you can't convert a 25-digit number to a ulong, then post an answer which tries to use ulongs? – canton7 Nov 21 '19 at 12:42