Perfect Squares

Discussion in 'C Programming' started by The 1, Jun 16, 2007.

  1. The 1

    The 1 Guest

    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, Jun 16, 2007
    #1
    1. Advertising

  2. The 1

    The 1 Guest

    Also (and obviously) use of lib-funcs like pow() and sqrt() is not
    allowed
    The 1, Jun 16, 2007
    #2
    1. Advertising

  3. The 1

    Army1987 Guest

    "The 1" <> ha scritto nel messaggio
    news:...
    > 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.
    Army1987, Jun 16, 2007
    #3
  4. 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.
    Richard Heathfield, Jun 16, 2007
    #4
  5. The 1

    Army1987 Guest

    "The 1" <> ha scritto nel messaggio
    news:...
    > 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;
    }
    Army1987, Jun 17, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. venkat
    Replies:
    3
    Views:
    1,543
    Robert Kern
    Jun 1, 2005
  2. Replies:
    9
    Views:
    826
    Tim Roberts
    Mar 17, 2006
  3. Replies:
    9
    Views:
    674
    Mark Jeffcoat
    Oct 19, 2006
  4. Erik Max Francis
    Replies:
    15
    Views:
    457
    Dan Stromberg
    Aug 19, 2007
  5. Arnab das

    Divide the image into 1cm squares

    Arnab das, Jul 30, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    601
    Cowboy \(Gregory A. Beamer\)
    Jul 30, 2008
Loading...

Share This Page