Entries:
Posts:

Something went wrong getting user information from Channel 9

Latest Achievement:

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Something went wrong getting the Visual Studio Achievements

# Finding square numbers

• Oops, something didn't work.

Getting "follow" information
• I'm a little rusty on this part of Math(s)

But given X, let X = 13. And I want to find the next square number, which is 16.

So how would I do that?

Would this work?

```int x = 13;
int y, z, w;

y = Math.Sqrt(x);

z = Math.Ceiling(y);

w = Math.Pow(z, 2);

```

Where "w" would now by 16?

• W3bbo wrote:

Would this work?
That would only work if your variables are typed double, so that code won't compile.

```int x = 13;
double y, z;
int w;

y = Math.Sqrt((double) x);

z = Math.Ceiling(y);

w = (int) Math.Pow(z, 2);
```

• Couldn't you do it by:
taking the number.
sqrt'ing it (would probably have to be float or double)
round it up to the nearest integer
sqr the integer

Cos if you enter a number, sqrt it, it'll give you a long number, then you just go to the next highest integer to get the next highest sqr number.

No? Won't that work?
Just an idea.
Shafiq.

• s_jetha wrote:
Couldn't you do it by:
taking the number.
sqrt'ing it (would probably have to be float or double)
round it up to the nearest integer
sqr the integer

That's what the thing I wrote does

• Fair warning; I suck at maths, I can't program my way out of a paper bag and I am a dumb @ss, also I did little testing... But here is the function I came up with.

```       static int findNext(int start)        {            if (start == 1 || start == 0) return start;
while(true)             {                for (float x = 1; x < start / 2; x++)                    if (x == start / x)                        return start;                start++;              }        }
```

• Technically that is the thing Microsoft wrote..

• I once had an interesting argument with someone as to what I considered to be a "square" number..  Fits in there with my envisionment of what an "odd" number is..

• What if you're given x = 16?  Are you supposed to echo 16 back (as the algorithm does) or give the next square, 25?

• Couldn't you just do this:

double x;
double y, z, w;

x = (UserInput);
y = Math.Sqrt(x);
w = Math.Floor(y);
w = w++;
z = Math.Pow(w, 2);

It's just an idea. This would sort out the problem of a square number being given as a user input, and also settle the needs of non square numbers as user inputs.

Angus Higgins

• int next_square(int x)
{
/* sqrt value is cast *down* to int, like floor() */
/* EDIT: the (double) cast may not be necessary, try it without */
int t = (int)Math.Sqrt((double)x) + 1;
return t * t;
}