How to express it?

I am trying to figure out how to write the following:

total = (value * 0.95 ^ 0) + (value * 0.95 ^ 1) + (value * 0.95 ^ 2) ...

or

x = (y * z ^ 0) + (y * z ^ 1) + (y * z ^ 2) + (y * z ^ 3) ...

This expresses how to calculate x for 4 iterations, but how can I express it to work with a variable number of iterations? Obviously, I could create a loop and add the values ​​together, but I would really like to find one equation that solves this.

I use C ++, but I think this is not a problem in the language (sorry, I do not know where else to ask this question!).

Any ideas?

Thank you, Chris.

+4
source share
8 answers

The solution to the equation is a geometric series and therefore can be calculated using

double geometric_series(double y, double z, int N) {
  return y * (std::pow(z, N) - 1.0) / (z - 1.0);
}

fun ++: , ++ 17 fold

template<std::size_t... N> 
double calculate_x(double y, double z, std::index_sequence<N...>) { // [0;N[
 auto f = [](double y_p, double z_p, double exp) {
   return y_p * std::pow(z_p, exp);
 };
 return (f(y, z, N) + ...);
}

template <std::size_t N>
auto calculate_x(double y, double z) {
  return calculate_x(y, z, std::make_index_sequence<N>{}); 
}

pre-++ 17

template <int N>
double calculate_x(double y, double z) {
  return calculate_x<N-1>(y, z) + (y * std::pow(z, N - 1));
}

template <>
double calculate_x<0>(double, double) {
  return 0;
}

double calculate_x_simple(double y, double z, int N) {
  double ret = 0.0;
  for (int i = 0 ; i < N ; ++i)
    ret += y * std::pow(z, i);
  return ret;
}

int main() {

   // x = (y * z ^ 0) + (y * z ^ 1) + (y * z ^ 2) + (y * z ^ 3)
   double y = 42.0;
   double z = 44.5;
   std::cout << (calculate_x<3>(y, z) == calculate_x_simple(y, z, 3)); // 1

}
+4

, .

,

y * (z 0 + z 1 +... + z n)

z 0 + z 1 +... + z n

(z (n + 1) - 1)/(z - 1)

x = y * (z (n + 1) - 1)/(z - 1)

+9

:

double x = 0;
int n = 5;
for(int exponent = 0; exponent <= n; ++exponent)
  x += y*pow(z, exponent);
+3

, . , :

template <int n>
double foo(double y, double z)
{
    return foo<n-1>(y, z) + y * std::pow(z, n);
}

template <>
double foo<-1>(double, double)
{
    return 0;
}

.

:

#include <iostream>
#include <cmath>

template <int n>
double foo(double y, double z)
{
    return foo<n-1>(y, z) + y * std::pow(z, n);
}

template <>
double foo<-1>(double, double)
{
    return 0;
}


int main()
{
    std::cout << foo<2>(2,3) << std::endl;
}

: 26

+3

math.pow for

#include <stdio.h>
#include <math.h>

int main(void) {
    int i;
    int n = 5;
    double y = 0.5;
    double z = 0.3;
    double answer = 0;
    for (i = 0  ; i < n ; i++)
        answer += y * pow(z,i);

    printf("%f", answer);
    return 0;
}
+2

n ,

#include <cmath>
double foo(double y, double z, int n)
{
   double x =0;
   for(int i = 0 ; i<n; ++i){
      x+=y*std::pow(z,i);
   }
   return x;
}

Where std::powis a function of power.

+2
source

It can be expressed as the sum from n = 0 to m. It can be expressed in one formula, according to wolframalpha .

+2
source

I don’t know if this suits your purpose, but you can use recursion (which in real terms is only a loop :))

int x = evaluate(y, z, count);


int evaluate(y,z, count)
{
   if (count <= 0)
    return 0;

   return (evaluate(y, z, count-1) + y*z^count);
}
+2
source

Source: https://habr.com/ru/post/1661069/


All Articles