http://www.hpmuseum.org/forum/thread-916.html

Calculate the sum of 1-1/2+1/3-1/4...+1/9999-1/10000.

In trying to show up the HP Prime, I wrote my first attempt like this:

- Code: Select all
`function () {`

var t = 1;

for (var j = 2; j <= 10000; j++) {

if (calculator.functions.isEven(j))

t = t - 1/j;

else {

t = t + 1/j;

}

}

return (t);

}

This comes up with 0.693097183059958

My second try is

- Code: Select all
`function () {`

var t = 1;

for (var j = 10000; j >= 2; j--) {

if (calculator.functions.isEven(j))

t -= 1/j;

else {

t += 1/j;

}

}

return (t);

}

(This follows the instruction to work from right to left). It comes up with 0.693097183059949; the last two digits are 49 instead of 58.

Now, I wanted to get more precision, so I thought I could use BigNum for that, but I get an error with this code (basing on my first attempt:

- Code: Select all
`function () {`

var t = BigInt.ONE;

var n - t;

var a;

for (var j = 2; j <= 10000; j++) {

a = BigNum("/"](n,j);

if (calculator.functions.isEven(j))

t = BigNum["-"](t,a);

else {

t = BigNum["+"](t,a);

}

}

return (t);

}

I get this message:

'undefined' is not an object

So, what I am doing wrong? I have looked at all of the code I can find about BigNum, and considered using other options like toBigF (and RPL+) for this exercise.

David Motto