-3

I want to solve this math question in processing:

S=1+1/2-1/3+1/4...+1/99-1/100.

Here is my code (don't know why it doesn't work. I suppose it will print one number in the console, but it comes out a series of natural numbers):

float N = 0;
float T = 0;
int i = 1;

void draw() {
    for (i = 1; i < 100; i += 2) {
        N = N + 1 / i;
        T = T + 1 / (i + 1);
    }
    println(N - T);
}
khelwood
  • 46,621
  • 12
  • 59
  • 83
Leon
  • 11
  • 2
  • Did you use System.out.println(N-T)? Then the result is "1.0" – mrbela Dec 11 '18 at 20:59
  • 2
    Read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ for tips on debugging your code. – Code-Apprentice Dec 11 '18 at 21:00
  • 1
    See: https://stackoverflow.com/questions/4685450/why-is-the-result-of-1-3-0 – Jacob G. Dec 11 '18 at 21:02
  • You got the formula wrong in the question. At the beginning, you use `+` for *even* divisor (2 and 4) and `-` for *odd* divisor (3), but at the end you use `+` for *odd* divisor (99) and `-` for *even* divisor (100). Which is it? – Andreas Dec 11 '18 at 21:35
  • Sorry guys! the formula was wrong in the question. It should be S=1-1/2+1/3-1/4...+1/99-1/100. But the code was right. – Leon Dec 11 '18 at 22:42
  • @Leon - this is the well-known [alternating harmonic series](https://en.wikipedia.org/wiki/Harmonic_series_(mathematics)#Alternating_harmonic_series) that converges to `ln(2)`. – Brett Hale Dec 12 '18 at 11:22

2 Answers2

0

1 / i is a division expression between two integers, the result of which will be an int obtained by truncating the fraction part of the result. Since you are calculating float you can resolve it by using a 1f float literal

for (int i = 1; i < 100; i += 2) {
  N = N + 1f / i;
  T = T + 1f / (i + 1);
}

However your code currently counts the following:

foo+bar

Consider i = 1 for which you get T = 0.5 which you later subtract from N while you should be adding it as per your problem statement. One way to sole it is to write the code as:

float sum = 1;
for (int i = 2; i <= 100; i++) {
  float term = 1f / i;
  if (i % 2 != 0) {
    term *= -1;
  }
  sum += term;
}
System.out.println(sum);
Karol Dowbecki
  • 38,744
  • 9
  • 58
  • 89
0

First off, draw() runs every frame. Use setup() instead, it runs once.

Second, dividing ints results in an int. Go ahead, do println(1/i) and see the magic.

Third, always google around for some time before going to SO. You'll learn more by finding it out yourself. And this was very solvable with google and a little effort ;)

working code:

float N = 0;
float T = 0;
int i = 1;

void setup() {
    for (i = 1; i < 100; i += 2) {
        N = N + 1.0 / i;
        T = T + 1.0 / (i + 1);
    }
    println(N - T);
}
J.D.
  • 3,823
  • 2
  • 4
  • 18
  • Nice answer. I would stick with standard naming conventions and make sure variables are descriptive names that start with a lower-case letter. Also note that you can run this code directly without using a `setup()` function. – Kevin Workman Dec 11 '18 at 21:38