Reusable formula for decrementing denominator

  • Context: Undergrad 
  • Thread starter Thread starter KevinMulito
  • Start date Start date
  • Tags Tags
    Formula
Click For Summary
SUMMARY

The discussion focuses on optimizing a loop in a computer program that calculates the sum of a series defined as 1 + 1/2 + 1/3 + 1/4, using a counter variable to track the denominator. The user seeks a formulaic approach to transition between terms in the series but discovers that the relationship is nonlinear, as each term is defined by 1/n, with the next term being 1/(n+1). A suggested optimization in JavaScript involves using the post-increment operator (counter++) to streamline the code, enhancing readability and efficiency.

PREREQUISITES
  • Understanding of basic programming concepts, particularly loops.
  • Familiarity with JavaScript syntax and operators.
  • Knowledge of mathematical series and their properties.
  • Experience with code optimization techniques.
NEXT STEPS
  • Research JavaScript post-increment operator usage and its implications on code efficiency.
  • Explore mathematical series and convergence for deeper insights into summation techniques.
  • Learn about alternative looping constructs in JavaScript, such as forEach and map.
  • Investigate performance profiling tools for JavaScript to analyze and optimize code execution.
USEFUL FOR

Programmers, particularly those working with JavaScript, mathematicians interested in series summation, and anyone looking to optimize looping constructs in their code.

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
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
9
Views
3K
  • · Replies 5 ·
Replies
5
Views
730
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K