Velocity Reviews > Perfect Squares

# Perfect Squares

The 1
Guest
Posts: n/a

 06-16-2007
Q-1
Can sm1 suggest a program to check whether a given no is perfect
square or not??
Q-2
Also sm1 suggest a program to check whether a given no is perfect
power or not??
* PEfect power is a no which can be expressed some power of a any noo.

Its better if it is very efficient!

The 1
Guest
Posts: n/a

 06-16-2007
Also (and obviously) use of lib-funcs like pow() and sqrt() is not
allowed

Army1987
Guest
Posts: n/a

 06-16-2007

"The 1" <(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed) oups.com...
> Q-1
> Can sm1 suggest a program to check whether a given no is perfect
> square or not??

Check wheter there is some integer such as (i * i == n).
If you have problems with your code, post it here.

> Q-2
> Also sm1 suggest a program to check whether a given no is perfect
> power or not??
> * PEfect power is a no which can be expressed some power of a any noo.

C is based on English, so if you want to write the former it would
be a good idea to learn the latter.

> Its better if it is very efficient!

On a modern computer, even the least efficient way to do that takes
several orders of magnitude shorter time than I/O operations, so
unless you have to check for zilion numbers, it doesn't matter. And
modern compilers are often able to optimize it, anyway.

Richard Heathfield
Guest
Posts: n/a

 06-16-2007
The 1 said:

> Q-1
> Can sm1 suggest a program to check whether a given no is perfect
> square or not??

I'm not sure what you mean by "sm1" or "no" (in the context). I'm
guessing at "someone" and "number", but I'm not certain that my guesses
are correct.

The important thing about homework is to try doing it for yourself.
That's a vital part of the learning process.

To start you off:

The term "perfect square" suggests that this is an exercise in integers.
The highest value one can portably represent in a native unsigned
integer type is 4294967295, which is just a shade short of being a
perfect square itself. To be able to represent this, use unsigned long
int. The highest perfect square that can be portably represented in an
unsigned long int is 4294836225.

The lowest perfect square you can represent in an unsigned long int is 0
(or, if you don't count that, 1).

So you have lower and upper bounds on your problem.

Your next step is to capture a value from the user, and ensure it is
within the range 0 (or 1) to 4294836225. If it isn't, it might yet be a
perfect square, but not one that you can handle easily within the
context of a simple homework exercise, so you can reject it as being
outside the supported range.

Now you need to find the integer that is closest to the square root of
the number. You can find this via binary search.

Now multiply that number by itself. If it is equal to the input number,
the answer is "yes, this is a perfect square". Otherwise, the answer is
"no, this is not a perfect square".

> Q-2
> Also sm1 suggest a program to check whether a given no is perfect
> power or not??

I suggest you finish Question 1 before attempting Question 2.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.

Army1987
Guest
Posts: n/a

 06-17-2007

"The 1" <(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed) oups.com...
> Q-1
> Can sm1 suggest a program to check whether a given no is perfect
> square or not??
> Q-2
> Also sm1 suggest a program to check whether a given no is perfect
> power or not??
> * PEfect power is a no which can be expressed some power of a any noo.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
struct Power { int Is; long Base, Expon; } is_power(long n)
{
struct Power result;
result.Is = 1;
result.Base = n;
result.Expon = 1;
return result;
}

int main(int argc, char *argv[])
{
long n;
char *end;
struct Power is;
if (argc < 2) {
exit(EXIT_FAILURE);
}
n = strtol(argv[1], &end, 10);
if (*end != '\0')
printf("Whatever garbage you mean by \"%s\" will be "
"ignored.\n", end);
if (errno == ERANGE)
printf("The number you wrote is too large or too small, "
"let's pretend you wrote %ld.\n", n);
is = is_power(n);
if (is.Is)
printf("%ld is a perfect power as it equals %ld**%ld.\n"
n, is.Base, is.Expon);
else
printf("%ld is not a perfect power.");
return 0;
}

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post totalgeekdom@gmail.com Python 9 03-17-2006 08:03 AM venkat Python 3 06-01-2005 08:06 PM kellie hudson Computer Support 4 01-14-2005 06:05 AM fredcromer Computer Support 1 04-21-2004 06:41 PM fredcromer Computer Information 0 04-21-2004 04:38 PM