Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > not a homework question

Reply
Thread Tools

not a homework question

 
 
Micah Cowan
Guest
Posts: n/a
 
      03-12-2008
Micah Cowan <(E-Mail Removed)> 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/
 
Reply With Quote
 
 
 
 
Ark Khasin
Guest
Posts: n/a
 
      03-12-2008
Robert Gamble wrote:
> On Mar 12, 12:57 am, Three Headed Monkey
> <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
WANG Cong
Guest
Posts: n/a
 
      03-12-2008
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.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-12-2008
Micah Cowan <(E-Mail Removed)> writes:
> Three Headed Monkey <(E-Mail Removed)> 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) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      03-12-2008

"Three Headed Monkey" <(E-Mail Removed)> wrote in message
news:12984088$sWSEgdgrr$(E-Mail Removed)...
>
> 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



 
Reply With Quote
 
Doug Miller
Guest
Posts: n/a
 
      03-12-2008
In article <12984088$sWSEgdgrr$(E-Mail Removed)>, Three Headed Monkey <(E-Mail Removed)> 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.
 
Reply With Quote
 
Noob
Guest
Posts: n/a
 
      03-12-2008
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
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      03-12-2008
On Tue, 11 Mar 2008 22:25:01 -0700 (PDT), Brice Rebsamen
<(E-Mail Removed)> wrote:

>On Mar 12, 12:57 pm, Three Headed Monkey
><(E-Mail Removed)> 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
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      03-12-2008
Keith Thompson <(E-Mail Removed)> writes:

> Micah Cowan <(E-Mail Removed)> writes:
>> Three Headed Monkey <(E-Mail Removed)> 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/
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-13-2008
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) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
not homework... something i find an interesting problem Trip Technician Python 7 04-21-2009 08:31 PM
Another Tricky Problem I am Messing With (Not Homework) joebenjamin C Programming 31 10-11-2007 05:32 PM
remove entry from text file [NOT A HOMEWORK] tmp123 C Programming 36 03-03-2006 02:01 AM
Homework question FML Computer Support 6 06-25-2005 05:25 PM
exercise problem -- not homework Charles C++ 5 07-28-2003 10:29 PM



Advertisments