difficult to code

S

shan

I have tried writing a program which should

1)get a number from user
2)calculate the following
for e.g if the input is 3 then 3
3
3
and the answer should be 19683.
3)similarly when the user entered 4 or 5 or any number it should
give the rquired result.
4)pow() SHOULD NOT TO BE USED
 
A

Antonio

shan said:
I have tried writing a program which should

1)get a number from user
2)calculate the following
for e.g if the input is 3 then 3
3
3
and the answer should be 19683.

Wrong. The result of the operation you wrote is not 19863. The correct
result is 7,625,597,484,987. Read again your math books from Primary
School. Either you don't want to perform the operation you wrote or you
didn't operate correctly.
3)similarly when the user entered 4 or 5 or any number it should
give the rquired result.
4)pow() SHOULD NOT TO BE USED

Ask somewhere else, this has nothing to do with C. And besides that,
it's not that difficult to code.
 
R

Richard Heathfield

shan said:
I have tried writing a program which should

1)get a number from user
2)calculate the following
for e.g if the input is 3 then 3
3
3
and the answer should be 19683.

Strictly, that's the answer to (3 to the 3) to the 3, rather than 3 to the
(3 to the 3).
3)similarly when the user entered 4 or 5 or any number it should
give the rquired result.
4)pow() SHOULD NOT TO BE USED

I suggest you try a little harder. The problem specification appears to me
to be:

Let N be user input
Let F be N to the power N
Let S be F to the power N
Display S

Coding this in C is trivial, but beware! Even for very small N, the C
Standard does not guarantee that the result, S, can be calculated
accurately. If you need integer-perfect accuracy, I suggest unsigned long
(or, if you have a C99 compiler which you don't, unsigned long long) - but
be aware that you still won't get very far in terms of a valid input
domain.

Doubles give you a bit more scope, but lack integer-perfect accuracy after a
certain point.

If you need integer-perfect accuracy AND biggish inputs, then you might
consider using a bignum library such as GMP or Miracl.
 
G

Grumble

shan said:
2)calculate the following
for e.g if the input is 3 then 3
3
3
and the answer should be 19683.

(3^3)^3 = 3^9 = 19683
3^(3^3) = 3^27 = 7625597484987 (43 bits)

P.S. "e.g." stands for "exempli gratia" which (roughly) translates to
"just because I'm a nice guy, here's an example fer ya." In other words,
there is no "for" before "e.g.".
 
A

Anonymous 7843

I have tried writing a program which should

1)get a number from user
2)calculate the following
for e.g if the input is 3 then 3
3
3
and the answer should be 19683.
3)similarly when the user entered 4 or 5 or any number it should
give the rquired result.
4)pow() SHOULD NOT TO BE USED

Just out curiosity, is the fact that there are three 3's
significant? I mean, if the input was 4, should it
be four 4's:

4
4
4
4 = 340282366920938463463374607431768211456

or just

4
4
4 = 4294967296

that might make the problem even more difficult/interesting.

(Note I have chosen left associativity for consistency with
original message's result of 19683. My personal understanding
of associativity differs.)
 
S

Sensei

I have tried writing a program which should

1)get a number from user
2)calculate the following
for e.g if the input is 3 then 3
3
3
and the answer should be 19683.
3)similarly when the user entered 4 or 5 or any number it should
give the rquired result.
4)pow() SHOULD NOT TO BE USED


Superpower, aka hyperpower and power tower, is a simple thing. You say:

3
3
3 = something too small.

That is a really huge number. If you want to deal with this, use
logarithms and exponentials (no pow() you say) and get the wrong
answer: it's a logarithmic approximation of the result, so big numbers,
less accuracy.

Now, all I can say to you, is: get a infinite precision algebra if you
really need.


Learn here: http://mathworld.wolfram.com/PowerTower.html

Use this: http://www.swox.com/gmp/
 
M

Michael Press

Just out curiosity, is the fact that there are three 3's
significant? I mean, if the input was 4, should it
be four 4's:

4
4
4
4 = 340282366920938463463374607431768211456

or just

4
4
4 = 4294967296

that might make the problem even more difficult/interesting.

(Note I have chosen left associativity for consistency with
original message's result of 19683. My personal understanding
of associativity differs.)

Hello.
The convention is a^b^c = a^(b^(c)) because (a^b)^c = a^(bc).
e.g

2
sqrt(pi) d -z
--------- -- erf(z) = e
2 dz
 
A

Anonymous 7843

Hello.
The convention is a^b^c = a^(b^(c)) because (a^b)^c = a^(bc).
e.g

2
sqrt(pi) d -z
--------- -- erf(z) = e
2 dz

That was from two months ago!
I look backward to hearing more about your time machine!
 
M

Michael Press

That was from two months ago!
I look backward to hearing more about your time machine!

Reading forward from the older posts on the server. Could
not pass up comment. You all write good expositions on C.
I enjoy the fine points of a discipline, and am always
ready for another paradigm to improve program structure.
 

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

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,462
Latest member
ChanaLipsc

Latest Threads

Top