0
$\begingroup$

I had been trying to limit for some c++ programs, the bigest whole number they can handle is

lim = 18446744073709551615

and the different functions are:

tfn[k_] := Total[Table[n!, {n, k}]]

or

spd2[n_] := Total[Table[2^i, {i, n}]]

the only way I found to know the limit k that do not overflow is

klimtfn = For[i = 1, 1 < 100, i++, If[tfn[i] > lim, Break[]]]; i - 1

or

klimspd2 = For[i = 1, 1 < 100, i++, If[spd2[i] > lim, Break[]]]; i - 1

for both cases I found correct answers, my concern is I have used forand this is like unfair, surely I must learn something better otherwise. Thanks for advice.

$\endgroup$
11
  • 1
    $\begingroup$ What does Developer`$MaxMachineInteger return for you? $\endgroup$ Commented Oct 17, 2018 at 10:31
  • 1
    $\begingroup$ i=1; While[tfn[i] < lim, i++]; i-1 $\endgroup$
    – ZaMoC
    Commented Oct 17, 2018 at 10:32
  • $\begingroup$ @J.M. is computer-less it gives me 9223372036854775807 $\endgroup$ Commented Oct 17, 2018 at 10:48
  • $\begingroup$ Okay. So, you know your spd2 is a finite geometric series, yes? Equate it to Developer`$MaxMachineInteger and solve for the largest possible index. $\endgroup$ Commented Oct 17, 2018 at 10:52
  • $\begingroup$ As for the Kurepa function, it might be possible to do a modified binary search. $\endgroup$ Commented Oct 17, 2018 at 10:57

1 Answer 1

4
$\begingroup$

Mathematica can do the sums symbolically, after which you can use FindRoot (actually, the symbolic sum isn't necessary, as can be seen by replacing = with := in the following). First example:

f[k_] = Sum[n!, {n, k}]

-1 - Subfactorial[-1] + (-1)^(1 + k) Gamma[2 + k] Subfactorial[-2 - k]

Using FindRoot:

k1 = k /. FindRoot[f[k]== 18446744073709551615, {k, 40},WorkingPrecision->16]

20.65081840115899

Check:

Sum[n!, {n, Floor[k1]}] < 18446744073709551615 < Sum[n!, {n, Floor[k1]+1}]

True

For your second example:

g[k_] = Sum[2^i, {i, k}]

2 (-1 + 2^k)

Using FindRoot:

k2 = k /. FindRoot[g[k] == 18446744073709551615, {k, 100}]

63.

Check:

Sum[2^i, {i, Floor[k2]}] < 18446744073709551615 < Sum[2^i, {i, Floor[k2]+1}]

True

$\endgroup$
1
  • $\begingroup$ FindRoot !! this is Thanks it works for all my "Series" $\endgroup$ Commented Oct 17, 2018 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.