Reusable formula for decrementing denominator

  • Thread starter Thread starter KevinMulito
  • Start date Start date
  • Tags Tags
    Formula
AI Thread Summary
The discussion focuses on finding a mathematical formula for incrementing denominators in a summation loop that calculates the harmonic series. The user is seeking a way to simplify their code by identifying a consistent relationship between terms, but notes that the relationship is nonlinear, as each term is derived from the previous one by simply incrementing the denominator. A suggestion is made to use a shorthand in JavaScript by employing the post-increment operator, which streamlines the code without sacrificing functionality. The conversation emphasizes that there is no simple additive or multiplicative relationship between the terms. Overall, the challenge lies in the nature of the harmonic series and its incremental structure.
KevinMulito
Messages
1
Reaction score
0
I am currently working on computer program that has to find the sum of during looping:

1 + 1/2 + 1/3 + 1/4 + etc.

So the loop looks something like this:

sum = 1;
counter = 2;

while(...){
sum = sum + (1/counter);
counter = counter +1;
}

My general question however is purely mathematical.

I am currently using a basic counter variable to keep track of what the next denominator value will be, but I was hoping there was some formula I could be used to go from: 1/2 to 1/3, then 1/3 to 1/4. I am unable to find a consistent relationship between each value and the next in order to simplify my code. Any suggestions would be greatly appreciated.
 
Mathematics news on Phys.org
The relationship is nonlinear -- you can't find a common difference or ratio between each pair of adjacent terms. The relationship is as follows: if the term in question is 1 \over n, then the following term will be 1 \over n + 1. It's not really like you can add or multiply some constant to get the next term in the sequence.
 
What he said. Looks like you're using JavaScript or similar, so you can sacrifice some readability and shorten it to
Code:
sum = 1;
counter = 2;

while(...){
sum = sum + (1/counter++);
}
The trailing ++ tells the compiler to add 1 to counter after the expression has been evaluated. That's about as simple as this code can get.

Fred
 
Suppose ,instead of the usual x,y coordinate system with an I basis vector along the x -axis and a corresponding j basis vector along the y-axis we instead have a different pair of basis vectors ,call them e and f along their respective axes. I have seen that this is an important subject in maths My question is what physical applications does such a model apply to? I am asking here because I have devoted quite a lot of time in the past to understanding convectors and the dual...
Insights auto threads is broken atm, so I'm manually creating these for new Insight articles. In Dirac’s Principles of Quantum Mechanics published in 1930 he introduced a “convenient notation” he referred to as a “delta function” which he treated as a continuum analog to the discrete Kronecker delta. The Kronecker delta is simply the indexed components of the identity operator in matrix algebra Source: https://www.physicsforums.com/insights/what-exactly-is-diracs-delta-function/ by...

Similar threads

Back
Top