简体   繁体   中英

Logic for formula calculation

I need to compute the following problem in code:

x0 = 2 and xi = (−1/2) * x(sub i - 1) * sqrt(x(sub i - 1))

find the result of (1/e^(x1 + x2 + x3 + ...)).

在此处输入图片说明

(Or as marked up text)

  1. Write a function of an appropriate type that calculates and returns the result of:
    e (x 1 -1 - x 2 -1 + x 3 -1 - x 4 -1 + ...) , for n elements, defined as: x 0 = 2 and x i = -½√|x i-1 |

It has to be done in C but I am just trying to figure out the logistics of it.

What I have thought till now : x0 has to be a variable initialized with 2 along with x1. x2, x3... will be calculated in a recursive function n-1 times. I am not sure how the results should be stored, also a variable or maybe an array? Would an array be appropriate??

Thank you.

Would it not be simpler to do it iteratively like this? I'm not actually sure if this generates the correct answer but this seems to be what your formula would imply.

long double
compute(unsigned n)
{
    long double x = 2.0L;
    for (unsigned i = 0; i < n; ++i)
        x = (-(1.0L/2.0L) * x) * sqrtl(fabsl(x));
    return x;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM