-4

Hope your ok I'll be happy if you help me out with this problem ... Thnx

I wrote this Code for the Formula : "http://up.upinja.com/zvhev.png"

Console.WriteLine("Enter two Numbers to start the Equation (X=Number , N=Power)" + "");
int X = int.Parse(Console.ReadLine());
int N = int.Parse(Console.ReadLine());
double sum = 0;
double up = 1;
int down = 1;

double sentence;
for (int i = 1; i < N; i++)
{
up = Math.Pow(X, N);
down = N * i;
sentence = up / down;
sum += sentence;
}
Console.WriteLine("The Sum is : " + sum);

I want that math Formula to be done in C# way.... my Code is running but wrong answer I get ... Could you plz help me to fix it ?

MeTe12
  • 1
  • 1
  • Please provide sample input, current output and expected output – Camilo Terevinto Nov 22 '18 at 23:53
  • Your `down` is wrong. For example `4!` should be `4*3*2*1` and not `4*i` – DavidG Nov 22 '18 at 23:57
  • Your `down` calculation looks to be incorrect. The formula you link indicates it should be `i!` in each loop. Instead you're doing `N * i`. Also your loop should be `for (int i = 1; i <= N; i++) {...}` – lumberjack4 Nov 22 '18 at 23:58
  • In the first place, you get the wrong result, because your denominator (variable `down`) is wrong. According to the image, the denominator is supposed to equal the factorial of the counter, i.e. in mathematical terms down = i!. (Beware: The exclamation mark is *not* the correct C# operator). As a quick solution you can insert a nested loop that iterates over j from 1 to i and multiplies all the j's. However, I do not recommend to do it, because the overall formula does not seem to be numerical stable. Even after you have fixed the obvious errors in your formula, the results might surprise you. – user2690527 Nov 23 '18 at 00:18
  • `up = Math.Pow(X, i); down = down * i;` ... and `i <= N` but it will work only for small N (<13)... changing `down` to `double` would help a liitle bit but still ... – Selvin Nov 23 '18 at 00:20

4 Answers4

0

n! represents Factorial. Please refer Factorial to read about Factorial.

A sample factorial function in C# is as follows:

long Factorial(long value)
{          
    if (value == 0)
        return 1;

    return value * Factorial(value - 1);
}

Also, following line needs correction.

up = Math.Pow(X, N);

The exponent part needs to increase according to your formula. It needs to be

up = Math.Pow(X, i);
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • your function is not working ... Factorial(13) returns 1 932 053 504 which is wrong – Selvin Nov 23 '18 at 00:09
  • Ooops, thanks for pointing for Selvin. 'int' was the culprint, i have corrected the code – Anu Viswan Nov 23 '18 at 00:14
  • My apologies @selvin. Limitations of the data type is obvious in the code. But crux of idea was to highlight key areas where to look at, as there was problem in the way formula was interpreted, especially exponents and factorials. – Anu Viswan Nov 23 '18 at 00:21
0

Use this:

Console.WriteLine("Enter two Numbers to start the Equation (X=Number , N=Power)" + "");
int X = int.Parse(Console.ReadLine());
int N = int.Parse(Console.ReadLine());
double sum = 0;
double up = 1;
int down = 1;

double sentence;
for (int i = 1; i <= N; i++)
//Change < to <=
{
    up = Math.Pow(X, i);
    //This is a for loop i created to get the factorial of any number, sometimes creating your own functions may be worth it
    for (int a = N, a > 0, a--)
    {
     //Function of this Loop : multiply the value of the denominator by its value - 1 to get the factorial of  the number
        down *= a;
    } 
    sentence = up / down;
    sum += sentence;
}
Console.WriteLine("The Sum is : " + sum);
preciousbetine
  • 2,959
  • 3
  • 13
  • 29
  • Code dumps without explanation are not useful here. And what do you think this answer gives that the existing ones doesn't? – DavidG Nov 23 '18 at 00:27
  • 1
    A code dump is literally that, a dump of code. In this case it means that you should explain what the code is doing, that's what makes a good answer. – DavidG Nov 23 '18 at 00:31
0

You can actually simplify this quite a bit. But first, what's wrong with your existing code is that you're not computing i! correctly. The sequence you're actually computing right now is:

X^N / N + X^N / (2 * N) + X^N / (3 * N) + ... + X^N / ((N - 1) * N)

A few things: - The loop has an off-by-one issue - The numerator is raised to the wrong power - The denominator of these needs to be i!

Factorials are defined through a simple recurrence relation:

i! = i * (i - 1)! and 0! = 1

Or, in other words, the product of the first i consecutive natural numbers. So one way to correct this is by creating a method called Factorial:

static long Factorial(int i) 
{ 
    long product = 1;
    while (i > 0) {
        product *= i;
        --i;
    }
    return product;
}

Then you can fix your loop this way:

for (int i = 1; i < N; i++)
{
    sum += Math.Pow(X, i) / Factorial(i);
}

However, we're redoing a lot of work every iteration of the loop that we don't need to do. This is where the simplification comes in. The general formula for the i'th term is this:

C[i] = X^i / i!

But we can also write this in terms of the term that came before it:

C[i] = C[i-1] * X / i

So we can rewrite the loop like this:

double lastTerm = 1;
for (int i = 1; i <= N; ++i)
{
    // Cast to double here or, probably better, make X a double in the first place.
    lastTerm *= (double) X / i;
    sum += lastTerm;
}

This has several advantages:

  • It'll generally be faster since we're doing less work each iteration (Math.Pow computes arbitrary powers, so it's a bit slower than just multiplying).
  • It'll be less prone to numerical issues. Factorials get really big really fast. In fact, 21! is already too big to store in a long so if your N is bigger than that, everything will break.
Kyle
  • 6,500
  • 2
  • 31
  • 41
-4

Something wrong in pow, shoud incremental value

    up = Math.Pow(X, i);
Lemons
  • 107
  • 1
  • 13
  • 1
    Note that you're getting downvotes because your answer is simply wrong. – DavidG Nov 23 '18 at 00:02
  • lol, where? everbody already comment with wrong factorial, so i answer something else that indicated wrong, clearly every loop x shoud pow with incremental value – Lemons Nov 23 '18 at 00:09
  • 1
    You can't just fix one tiny part and expect that to be enough. Answers need to be complete. – DavidG Nov 23 '18 at 00:13
  • it doesn't make any sense, do you think for what stackoverflow provide every post with possibility multiple answer? yes, because to achieve the perfect answer is very difficult, so, the other user can give their perspective, u're simply wrong – Lemons Nov 23 '18 at 00:22
  • 1
    Answers need to be complete or they are not useful. What you have said is correct, but you could have written that as a comment as it doesn't *answer the question being asked*. I'm not trying to be mean here, but I've been around long enough to know what makes a good answer... – DavidG Nov 23 '18 at 00:24
  • i only said "something wrong" not "correct" , i think u just defend ur self, see this post : https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it , every post got popular not only just because one simply answer solve the problem, but because the other users aware something else and their make another answer – Lemons Nov 23 '18 at 00:32
  • 2
    OK, I've tried to help you but you're not listening. Now you have 3 people all agreeing that this is a bad answer. As for the question you link, look how much detail is in those answers? – DavidG Nov 23 '18 at 00:34
  • 1
    Oh the irony, comparing any of the answers in those question with what you have provided as "answer" here. It boggles my mind. Any of those answers provide orders of magniture more information, knowledge and context with regard to "their" question than the two-liner here does for the question at hand. Yet here we are, pretending those other answers are in some shape or form similar to this one here. –  Nov 23 '18 at 00:37