black_yurizan
black_yurizan

Reputation: 437

How do I create a formula in which a compounded numeric variable equals a specified value?

If you clicked on this post, I know the question is a little bit unclear. So I have to show you what I mean instead.

var value = 30;
var num = 1;
var total = 0;

while(num < 10){

total +=num;
num++;

}
if(total == value){
console.log("They are equivalent");
}

Alright consider the variables value, num, and total. While num is less than 10, the specified value. I want it to add the num value into total, but at the end of the loop I want total and value to be equivalent. I just don't know how to do that or what formula I need to create in order to do that.

Upvotes: 1

Views: 70

Answers (3)

Nina Scholz
Nina Scholz

Reputation: 386604

Basically you need a factor for your value to add.

If you have just to add the counter, it goes from 1 to 9, the formular for the sum is Triangular Number

 sum = n * (n + 1) / 2

You know the sum, you get 45 and that is to much. For the right sum you need a factor to multiply num before adding. This is just the division of

wanted result / actual result = factor
      30      /       45      = 2 / 3  = 0.6666666666666667

This factor can be used in your iteration.

For accuracy i suggest to use equivalent fractions.

var value = 30,
    num = 1,
    total = 0;

while (num < 10) {
    total += num * 2 / 3; // apply factor
    console.log(num, total);
    num++;
}

if (total === value){
    console.log("They are equivalent");
}

Upvotes: 2

bgoldst
bgoldst

Reputation: 35314

Your loop is computing the sum of an arithmetic progression, also known as an arithmetic series, starting from 1 and with a common difference of 1. Mathematically, the progression is:

a1

d

an

And the sum is:

Sn


It sounds to me like you want to calculate the required value of n (num in your code) for a prespecified value for the sum Sn (value in your code). We can accomplish this by solving the above sum equation for n.

We can start by rearranging as follows:

Sn-rearrange-1

Sn-rearrange-2

This is a quadratic equation. Plugging into the quadratic formula:

quad


Here's a demonstration of the above result using R.

First, just to demonstrate the sum formula:

calcTotal <- function(num) num*(num+1L)/2L;
calcTotal(1:20);
##  [1]   1   3   6  10  15  21  28  36  45  55  66  78  91 105 120 136 153 171 190 210

Now, here's how we can implement the derived solution for n. I wrote this function to produce a matrix giving the input Sn value under the total column, then the next two columns showing the result value of the derived formula for both the plus and minus choices for the ± operation.

calcNum <- function(total) cbind(total,matrix(rep(each=2L,sqrt(2*total+0.25))*c(1,-1)-0.5,ncol=2L,byrow=T,dimnames=list(NULL,c('+','-'))));
res <- calcNum(1:36);
res;
##       total        +         -
##  [1,]     1 1.000000 -2.000000
##  [2,]     2 1.561553 -2.561553
##  [3,]     3 2.000000 -3.000000
##  [4,]     4 2.372281 -3.372281
##  [5,]     5 2.701562 -3.701562
##  [6,]     6 3.000000 -4.000000
##  [7,]     7 3.274917 -4.274917
##  [8,]     8 3.531129 -4.531129
##  [9,]     9 3.772002 -4.772002
## [10,]    10 4.000000 -5.000000
## [11,]    11 4.216991 -5.216991
## [12,]    12 4.424429 -5.424429
## [13,]    13 4.623475 -5.623475
## [14,]    14 4.815073 -5.815073
## [15,]    15 5.000000 -6.000000
## [16,]    16 5.178908 -6.178908
## [17,]    17 5.352350 -6.352350
## [18,]    18 5.520797 -6.520797
## [19,]    19 5.684658 -6.684658
## [20,]    20 5.844289 -6.844289
## [21,]    21 6.000000 -7.000000
## [22,]    22 6.152067 -7.152067
## [23,]    23 6.300735 -7.300735
## [24,]    24 6.446222 -7.446222
## [25,]    25 6.588723 -7.588723
## [26,]    26 6.728416 -7.728416
## [27,]    27 6.865460 -7.865460
## [28,]    28 7.000000 -8.000000
## [29,]    29 7.132169 -8.132169
## [30,]    30 7.262087 -8.262087
## [31,]    31 7.389867 -8.389867
## [32,]    32 7.515610 -8.515610
## [33,]    33 7.639410 -8.639410
## [34,]    34 7.761356 -8.761356
## [35,]    35 7.881527 -8.881527
## [36,]    36 8.000000 -9.000000

First, observe that the choice of minus always results in negative numbers. Since we're only interested in positive results, we can ignore that column.

Second, note how the choice of plus always results in positive numbers, but many of them are non-integers. This makes sense, given that the arithmetic series does not land on all possible integer totals. For totals that satisfy the integer basis of the progression, the function returns the correct integral results.

We can subset out the integral results as follows, using a custom function for proper floating-point comparison:

feq <- function(a,b,TOL=max(a,b)*1e-12) if (is.infinite(a) || is.infinite(b)) a==b else abs(a-b)<=TOL;
res[feq(res[,'+'],round(res[,'+'])),];
##      total +  -
## [1,]     1 1 -2
## [2,]     3 2 -3
## [3,]     6 3 -4
## [4,]    10 4 -5
## [5,]    15 5 -6
## [6,]    21 6 -7
## [7,]    28 7 -8
## [8,]    36 8 -9

Now, for the exact total you have in your code sample:

calcNum(30);
##      total        +         -
## [1,]    30 7.262087 -8.262087

This means that we would require 7.262087 iterations of your loop in order to arrive at a total value of 30. This is, of course, impossible.

Upvotes: 0

yanguya995
yanguya995

Reputation: 143

Your calculation wont work to give you 30, if you are looking for that exact compouding method to be equal to value after 9 iterations then use

var value =36;

This is because

1+2+3+4+5+6+7+8 =36;

This compund method will never give you 30

Upvotes: -1

Related Questions