not a homework question

  • Thread starter Three Headed Monkey
  • Start date
T

Three Headed Monkey

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!
 
I

Ian Collins

Three said:
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...
 
B

Brice Rebsamen

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?
 
C

CBFalconer

Three said:
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;
 
P

Peter Nilsson

Three Headed Monkey said:
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. ;)
 
U

user923005

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?
 
U

user923005

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.


  #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!


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
 
R

Richard Heathfield

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;
}
 
R

Robert Gamble

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.
 
M

Micah Cowan

Three Headed Monkey said:
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.
 
M

Micah Cowan

Micah Cowan said:
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...)
 
A

Ark Khasin

Robert said:
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.

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
 
W

WANG Cong

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.
 
K

Keith Thompson

Micah Cowan said:
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).
 
B

Bartc

Three Headed Monkey said:
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;

}
 
D

Doug Miller

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.
 
N

Noob

Three said:
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
 
B

Barry Schwarz

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
 
M

Micah Cowan

Keith Thompson said:
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.
 
K

Keith Thompson

Noob said:
(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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top