well ..
i am facing a problem and i hope pro coders here can gimme some help
there is a monthly test for coders here in egypt in my city ..
and i was given a question , i solved .. but as they commented ' in a bad way '
the problem was :
create a program where the user will give x , n
and the program must give him the result of this sequence:
1 + 1/x^1 + 2/x^2 + 3/x^3 + 4/x^4 ..... + n/x^n
btw : this ^ means power ..
well .. its not a very hard question ..
i solved it by giving them in the test this code:
===================================
===================================Code:#include <stdio.h> #include <math.h> main() { double x,n,b,y=1; printf("Write The Value Of x: "); scanf("%lf",&x); printf("Write The Value Of n: "); scanf("%lf",&n); for(b=1;b<=n;b++) y+=b/(pow(x,b )); printf("y Equals %lf\n",y); getchar(); }
this solution worked perfectly the question ..
but they gave me 5 out of 10
when i went to ask them
they told me .. dont panic .. no one solved this question like we was expected ..
they told me the computer will solve this equation like this
he will solve at x ( as for power 1 )and then x*x ( as for power 2 )and then x*x*x ( as for power 3 )and then x*x*x*x ( as for power 4)
they told me imagine the input number of n was a million 1000000
the computer will take minutes to solve it
which is a big time ..
they told me try to solve it where if we input million the computer solve it in a second normally
i told they give me a hint
he told me ..
instead if the computer making x ( as for power 1 )and then x*x ( as for power 2 )and then x*x*x ( as for power 3 )and then x*x*x*x ( as for power 4)
the computer can make it easier .. if the computer already made x*x*x .. why making again x*x*x*x , why not making the computer use the before equation .. he already timed x*x*x , why not just multiplying just another one x
this will save a lot of the memory of the program ..
well ..
i thought a lot
i cant find a solution ..
do u think if we made it for example multiplying every time with 1/x ?? but what about the n ?
anyone have an idea ??
sure you pro coders wont have a problem in doing this
many thanks guys
peace
i am sure grizzlybear can help me ..
hes a decent coder ..
and he also likes c and hate c++
just like me