By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,154 Members | 1,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,154 IT Pros & Developers. It's quick & easy.

Perfect Squares

P: n/a
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!

Jun 16 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Also (and obviously) use of lib-funcs like pow() and sqrt() is not
allowed

Jun 16 '07 #2

P: n/a

"The 1" <ju**********@gmail.comha scritto nel messaggio
news:11**********************@a26g2000pre.googlegr 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.
Jun 16 '07 #3

P: n/a
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.
Jun 16 '07 #4

P: n/a

"The 1" <ju**********@gmail.comha scritto nel messaggio
news:11**********************@a26g2000pre.googlegr 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) {
fputs("Provide a number, please...\n", stderr);
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;
}
Jun 17 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.