not a homework question

Discussion in 'C Programming' started by Three Headed Monkey, Mar 12, 2008.

  1. write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))

    I tried

    #include <stdio.h>

    int pow(int n)
    {
    int i,power;
    power=n;
    for(i=0;i<n;i=i+1)
    power=power*power;
    return power;
    }

    void main()
    {
    int result;
    char ignore;
    result= pow(9,pow(8,pow(7,pow(6,pow(5,pow(4,pow(3,pow(2,1))))))));
    printf("\nresult is %d", result);
    printf("\nPress ENTER");
    gets(&ignore);
    }


    but it does not work.

    how to do that in "C" standard language?

    I am using lcc-win32 compiler & windows 98.

    help!
     
    Three Headed Monkey, Mar 12, 2008
    #1
    1. Advertising

  2. Three Headed Monkey

    Ian Collins Guest

    Three Headed Monkey wrote:
    > write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))
    >

    I don't think you can in standard C, the result will be huge...

    --
    Ian Collins.
     
    Ian Collins, Mar 12, 2008
    #2
    1. Advertising

  3. On Mar 12, 12:57 pm, Three Headed Monkey
    <> wrote:
    > write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))
    >
    > I tried
    >
    > #include <stdio.h>
    >
    > int pow(int n)
    > {
    > int i,power;
    > power=n;
    > for(i=0;i<n;i=i+1)
    > power=power*power;
    > return power;
    >
    > }
    >
    > void main()
    > {
    > int result;
    > char ignore;
    > result= pow(9,pow(8,pow(7,pow(6,pow(5,pow(4,pow(3,pow(2,1))))))));
    > printf("\nresult is %d", result);
    > printf("\nPress ENTER");
    > gets(&ignore);
    >
    > }
    >
    > but it does not work.
    >
    > how to do that in "C" standard language?
    >
    > I am using lcc-win32 compiler & windows 98.
    >
    > help!


    First, you may want to have the pow function take 2 arguments: int
    pow(int a, int n) { ... }
    Second, the pow function defined in math.h does the job for you,
    except that it deals with doubles: float pow(double a, double n)
    returns a to the power of n as a float. So if you want to deal with
    integers you have to convert the result.
    Finally you may want to use a loop to do this. It'd look like this:

    #include <math.h>
    #include <stdio.h>
    int main(){
    double res=1;
    int n;
    for( n=2; n<=9; n++ )
    res = pow((double)n,res);
    printf("res=%f\n",res);
    return 0;
    }

    Don't forget to link with the math library when compiling (-lm)

    However this might overflow, resulting in res reaching inf. You can
    try using long double and powl... Or more complicated stuff. Any idea
    of what the resulting number might be?
     
    Brice Rebsamen, Mar 12, 2008
    #3
  4. Three Headed Monkey

    CBFalconer Guest

    Three Headed Monkey wrote:
    >
    > write a program in "C" language that computes
    > 9^(8^(7^(6^(5^(4^(3^(2^1)))))))


    Since 2 xor 1 is identically zero, that value suffices.

    x = 0;

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.


    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Mar 12, 2008
    #4
  5. Three Headed Monkey <> wrote:
    > Subject: not a homework question


    Your subject should reflect the nature of your problem,
    not merely that you have one.

    It's not homework questions we mind, rather it's homework
    questions that are quoted verbatim without so much as an
    attempt.

    > write a program in "C" language that computes
    > 9^(8^(7^(6^(5^(4^(3^(2^1)))))))


    #include <stdio.h>

    int main(void)
    {
    printf("%d\n", 9^(8^(7^(6^(5^(4^(3^(2^1))))))) );
    return 0;
    }

    If you mean exponentiation, then realise it's pretty big!
    Just 5^(4^(3^(2^1))) alone yields a 183000+ digit number.
    Raise 6 to the power of that and you have... big!

    > I am using lcc-win32 compiler & windows 98.


    Ah well... the non-standard extension qfloat should knock
    that over easily. ;)

    --
    Peter
     
    Peter Nilsson, Mar 12, 2008
    #5
  6. Three Headed Monkey

    user923005 Guest

    On Mar 11, 9:57 pm, Three Headed Monkey <>
    wrote:
    > write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))


    I guess that even LCC's qfloat data type will be too small.
    Do you have any idea how many digits there are in that number?
     
    user923005, Mar 12, 2008
    #6
  7. Three Headed Monkey

    user923005 Guest

    On Mar 11, 10:35 pm, Peter Nilsson <> wrote:
    > Three Headed Monkey <> wrote:
    >
    > > Subject: not a homework question

    >
    > Your subject should reflect the nature of your problem,
    > not merely that you have one.
    >
    > It's not homework questions we mind, rather it's homework
    > questions that are quoted verbatim without so much as an
    > attempt.
    >
    > > write a program in "C" language that computes
    > > 9^(8^(7^(6^(5^(4^(3^(2^1)))))))

    >
    >   #include <stdio.h>
    >
    >   int main(void)
    >   {
    >     printf("%d\n", 9^(8^(7^(6^(5^(4^(3^(2^1))))))) );
    >     return 0;
    >   }
    >
    > If you mean exponentiation, then realise it's pretty big!
    > Just 5^(4^(3^(2^1))) alone yields a 183000+ digit number.
    > Raise 6 to the power of that and you have... big!
    >
    > > I am using lcc-win32 compiler & windows 98.

    >
    > Ah well... the non-standard extension qfloat should knock
    > that over easily. ;)


    I doubt it.
    Maple with precision set to 10,000,000 digits overflowed:
    > Digits=10000000; y := 9.^(8.^(7.^(6.^(5.^(4.^(3.^(2.^1.)))))));
    >


    10 = 10000000

    Error, (in evalf/power) argument too large
    >
     
    user923005, Mar 12, 2008
    #7
  8. Three Headed Monkey said:

    >
    > write a program in "C" language that computes
    > 9^(8^(7^(6^(5^(4^(3^(2^1)))))))


    #include <stdio.h>

    int main(void)
    {
    printf("%d\n",
    9^(8^(7^(6^(5^(4^(3^(2^1))))))));
    return 0;
    }

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Mar 12, 2008
    #8
  9. On Mar 12, 12:57 am, Three Headed Monkey
    <> wrote:
    > write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))


    Assuming that the caret represents exponentiation (which based on the
    context of the rest of your post it is obvious that it does, except
    perhaps to Mr. Heathfield ;) ), this isn't a programming problem, it
    is a math comprehension problem. Even if it were possible to
    calculate the result of this expression you wouldn't be able to store
    it. There are more digits in the result than there are particles in
    the universe by an unimaginable factor.

    --
    Robert Gamble
     
    Robert Gamble, Mar 12, 2008
    #9
  10. Three Headed Monkey

    Micah Cowan Guest

    Three Headed Monkey <> writes:

    > write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))


    No one's mentioned this, so perhaps I'd better:

    Are you certain that the expression above (assuming your subject may
    just be a slight fib) is intended to represent exponentiation? The ^
    is a real operator in C, and means something rather different (**
    would be a better choice to represent exponentiation, as it doesn't
    have another, real, meaning in C).

    Otherwise, you may be best-suited using the standard library's own
    pow() function along with a floating point type (double would make
    sense, given that's what pow() deals in). OTOH, if you happen to have
    an implementation with <tgmath.h> (and don't care to be portable to
    them wot don't), you might opt for long double.

    --
    Micah J. Cowan
    Programmer, musician, typesetting enthusiast, gamer...
    http://micah.cowan.name/
     
    Micah Cowan, Mar 12, 2008
    #10
  11. Three Headed Monkey

    Micah Cowan Guest

    Micah Cowan <> writes:

    > Otherwise, you may be best-suited using the standard library's own
    > pow() function along with a floating point type (double would make
    > sense, given that's what pow() deals in). OTOH, if you happen to have
    > an implementation with <tgmath.h> (and don't care to be portable to
    > them wot don't), you might opt for long double.


    (Obviously, I wasn't thinking too clearly on the magnitude of this
    number...)

    --
    Micah J. Cowan
    Programmer, musician, typesetting enthusiast, gamer...
    http://micah.cowan.name/
     
    Micah Cowan, Mar 12, 2008
    #11
  12. Three Headed Monkey

    Ark Khasin Guest

    Robert Gamble wrote:
    > On Mar 12, 12:57 am, Three Headed Monkey
    > <> wrote:
    >> write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))

    >
    > Assuming that the caret represents exponentiation (which based on the
    > context of the rest of your post it is obvious that it does, except
    > perhaps to Mr. Heathfield ;) ), this isn't a programming problem, it
    > is a math comprehension problem. Even if it were possible to
    > calculate the result of this expression you wouldn't be able to store
    > it. There are more digits in the result than there are particles in
    > the universe by an unimaginable factor.
    >
    > --
    > Robert Gamble


    There are more digits in PI than in the number in question :).
    I mean, in analogy with actual vs. potential infinities, one could
    consider a number computed if he can tell for each x what the x-th digit
    of the number is.
    -- Ark
     
    Ark Khasin, Mar 12, 2008
    #12
  13. Three Headed Monkey

    WANG Cong Guest

    On Wed, 12 Mar 2008 05:57:12 +0100,Three Headed Monkey wrote:

    > write a program in "C" language that computes
    > 9^(8^(7^(6^(5^(4^(3^(2^1)))))))
    >
    > I tried
    >
    > #include <stdio.h>
    >
    > int pow(int n)


    Change the name please. C has a std library function with the same
    name and it's prototype is:

    double pow(double x, double y);

    > {
    > int i,power;
    > power=n;
    > for(i=0;i<n;i=i+1)
    > power=power*power;
    > return power;
    > }
    >
    > void main()


    main() is never void in C.


    > {
    > int result;
    > char ignore;
    > result=
    > pow(9,pow(8,pow(7,pow(6,pow(5,pow(4,pow(3,pow(2,1))))))));



    Your _own_ pow only takes one parameter, why here it has two??

    > printf("\nresult is %d", result);


    I suspect it overflows, since 'result' is only an int.

    > printf("\nPress ENTER");
    > gets(&ignore);


    gets() is considered harmful, NEVER use it.

    > }
    >
    >
    > but it does not work.
    >
    > how to do that in "C" standard language?
    >


    I am afraid you can't, the result may be too big. You can choose
    a non-standard libary that supports huge number operations,
    e.g. gmp.
     
    WANG Cong, Mar 12, 2008
    #13
  14. Micah Cowan <> writes:
    > Three Headed Monkey <> writes:
    >> write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))

    >
    > No one's mentioned this, so perhaps I'd better:
    >
    > Are you certain that the expression above (assuming your subject may
    > just be a slight fib) is intended to represent exponentiation? The ^
    > is a real operator in C, and means something rather different (**
    > would be a better choice to represent exponentiation, as it doesn't
    > have another, real, meaning in C).
    >
    > Otherwise, you may be best-suited using the standard library's own
    > pow() function along with a floating point type (double would make
    > sense, given that's what pow() deals in). OTOH, if you happen to have
    > an implementation with <tgmath.h> (and don't care to be portable to
    > them wot don't), you might opt for long double.


    There's no need to use <tgmath.h>; just use powl() (which, like
    <tgmath.h>, is new in C99, but is perhaps more likely to be supported
    as an extension by pre-C99 implementations).

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 12, 2008
    #14
  15. Three Headed Monkey

    Bartc Guest

    "Three Headed Monkey" <> wrote in message
    news:12984088$sWSEgdgrr$...
    >
    > write a program in "C" language that computes
    > 9^(8^(7^(6^(5^(4^(3^(2^1)))))))


    So if it's not homework, where does the problem come from? I suspect you
    don't really to know the answer to this.

    The algorithm can be done neatly in C using integer arithmetic. You were on
    the right lines with your code, but you should have tested with a smaller N.

    However, it is likely to overflow above N=4. Using bigger ints will help a
    little but
    will not come near N=9. This is my version tested to N=4:


    #include <stdio.h>

    int solve(int);

    int main(void)
    {int n,result;

    n=4;

    result=solve(n);

    printf("Answer for N = %d is %d\n",n,result);

    }

    int solve(int n)
    {
    int i,x,a;

    if (n<=1) return 1;

    x=solve(n-1);

    a=n;
    for (i=1; i<x; ++i) a*=n;

    return a;

    }

    --
    Bart
     
    Bartc, Mar 12, 2008
    #15
  16. Three Headed Monkey

    Doug Miller Guest

    In article <12984088$sWSEgdgrr$>, Three Headed Monkey <> wrote:
    >
    >write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))


    Do you have even the slightest idea how large that number is? Did you perhaps
    mean to type * instead of ^ ?
    >
    >I tried
    >
    >#include <stdio.h>
    >
    >int pow(int n)
    >{
    > int i,power;
    > power=n;
    > for(i=0;i<n;i=i+1)
    > power=power*power;
    > return power;
    >}


    Examine what you've written here a little more carefully, why don't you, and
    see exactly what this function calculates. If you want to compute a^b, one
    might suppose that you'd probably want a function that accepts both a and b as
    input parameters.
    >
    >void main()
    >{
    > int result;
    > char ignore;
    > result= pow(9,pow(8,pow(7,pow(6,pow(5,pow(4,pow(3,pow(2,1))))))));
    > printf("\nresult is %d", result);
    > printf("\nPress ENTER");
    > gets(&ignore);
    >}
    >
    >
    >but it does not work.


    A little more explanation would be helpful. Start with a complete description
    of the manner in which it "does not work", including what you expect it to do
    that it does not, and what it does that you expect it not to do.
     
    Doug Miller, Mar 12, 2008
    #16
  17. Three Headed Monkey

    Noob Guest

    Three Headed Monkey wrote:

    > write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))


    (I know your post was written tongue-in-cheek, but it's an interesting
    problem nonetheless.)

    Let u1 = 1 and u(n) = n ^ u(n-1)

    Assume base 10.

    u1 = 1
    u2 = 2
    u3 = 9
    u4 = 262144
    u5 = a number with 183231 digits
    u6 = a number with (roughly) 10^183231 digits

    u9 might be larger than one googolplex.

    http://en.wikipedia.org/wiki/Googolplex
     
    Noob, Mar 12, 2008
    #17
  18. On Tue, 11 Mar 2008 22:25:01 -0700 (PDT), Brice Rebsamen
    <> wrote:

    >On Mar 12, 12:57 pm, Three Headed Monkey
    ><> wrote:
    >> write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))
    >>
    >> I tried
    >>
    >> #include <stdio.h>
    >>
    >> int pow(int n)
    >> {
    >> int i,power;
    >> power=n;
    >> for(i=0;i<n;i=i+1)
    >> power=power*power;
    >> return power;
    >>
    >> }
    >>
    >> void main()
    >> {
    >> int result;
    >> char ignore;
    >> result= pow(9,pow(8,pow(7,pow(6,pow(5,pow(4,pow(3,pow(2,1))))))));
    >> printf("\nresult is %d", result);
    >> printf("\nPress ENTER");
    >> gets(&ignore);
    >>
    >> }
    >>
    >> but it does not work.
    >>
    >> how to do that in "C" standard language?
    >>
    >> I am using lcc-win32 compiler & windows 98.
    >>
    >> help!

    >
    >First, you may want to have the pow function take 2 arguments: int
    >pow(int a, int n) { ... }
    >Second, the pow function defined in math.h does the job for you,
    >except that it deals with doubles: float pow(double a, double n)
    >returns a to the power of n as a float. So if you want to deal with


    Close. pow returns a double, not a float.

    >integers you have to convert the result.
    >Finally you may want to use a loop to do this. It'd look like this:
    >
    >#include <math.h>
    >#include <stdio.h>
    >int main(){
    > double res=1;
    > int n;
    > for( n=2; n<=9; n++ )
    > res = pow((double)n,res);


    The cast is superfluous.

    > printf("res=%f\n",res);


    %g might be better.

    > return 0;
    >}
    >
    >Don't forget to link with the math library when compiling (-lm)


    A system specific issue that may or may not be applicable to the OP.

    >
    >However this might overflow, resulting in res reaching inf. You can
    >try using long double and powl... Or more complicated stuff. Any idea
    >of what the resulting number might be?



    Remove del for email
     
    Barry Schwarz, Mar 12, 2008
    #18
  19. Three Headed Monkey

    Micah Cowan Guest

    Keith Thompson <> writes:

    > Micah Cowan <> writes:
    >> Three Headed Monkey <> writes:
    >>> write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))

    >>
    >> No one's mentioned this, so perhaps I'd better:
    >>
    >> Are you certain that the expression above (assuming your subject may
    >> just be a slight fib) is intended to represent exponentiation? The ^
    >> is a real operator in C, and means something rather different (**
    >> would be a better choice to represent exponentiation, as it doesn't
    >> have another, real, meaning in C).
    >>
    >> Otherwise, you may be best-suited using the standard library's own
    >> pow() function along with a floating point type (double would make
    >> sense, given that's what pow() deals in). OTOH, if you happen to have
    >> an implementation with <tgmath.h> (and don't care to be portable to
    >> them wot don't), you might opt for long double.

    >
    > There's no need to use <tgmath.h>; just use powl() (which, like
    > <tgmath.h>, is new in C99, but is perhaps more likely to be supported
    > as an extension by pre-C99 implementations).


    Doh! Totally forgot about that.

    --
    Micah J. Cowan
    Programmer, musician, typesetting enthusiast, gamer...
    http://micah.cowan.name/
     
    Micah Cowan, Mar 12, 2008
    #19
  20. Noob <root@localhost> writes:
    > Three Headed Monkey wrote:
    >
    >> write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))

    >
    > (I know your post was written tongue-in-cheek, but it's an interesting
    > problem nonetheless.)
    >
    > Let u1 = 1 and u(n) = n ^ u(n-1)
    >
    > Assume base 10.
    >
    > u1 = 1
    > u2 = 2
    > u3 = 9
    > u4 = 262144
    > u5 = a number with 183231 digits
    > u6 = a number with (roughly) 10^183231 digits
    >
    > u9 might be larger than one googolplex.
    >
    > http://en.wikipedia.org/wiki/Googolplex


    (Assuming, of course, that "^" denotes exponentiation.)

    u5 is substantially larger than one googol (10^100); it has 183231
    digits compared to just 101 digits for one googol.

    u6 is substantially larger than one goolplex; it has 10^183231 digits,
    compared to just 10^100+1 digits for one googolplex.

    u7, u8, and u9 are Really Really Big (but still tiny compared to the
    largest numbers that have actually been used in mathematics).

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 13, 2008
    #20
    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. Charles
    Replies:
    5
    Views:
    544
    Charles
    Jul 28, 2003
  2. tmp123
    Replies:
    36
    Views:
    813
    Dave Thompson
    Mar 3, 2006
  3. joebenjamin
    Replies:
    31
    Views:
    919
    Mark McIntyre
    Oct 11, 2007
  4. Trip Technician
    Replies:
    7
    Views:
    291
  5. John Ladasky
    Replies:
    25
    Views:
    285
    John Ladasky
    Jul 25, 2013
Loading...

Share This Page