2
$\begingroup$

I have a sum in mind, but I want to break it into two sums and get a separate total for each of the two.

  1. All cases where the square root is accurate, i.e., exact
  2. All cases where the square root is not accurate, i.e., inexact.

Sum[1/Sqrt[k], {k, 1 , 2003}]
$\endgroup$
  • $\begingroup$ Accurate? Not accurate? What do these mean here? $\endgroup$ – Mr.Wizard Jul 1 '15 at 20:56
  • 3
    $\begingroup$ @Mr.Wizard Probably means "Integer", "not integer". Language issues $\endgroup$ – Dr. belisarius Jul 1 '15 at 20:58
  • $\begingroup$ You might be interested in this. $\endgroup$ – J. M. is away Jul 1 '15 at 21:11
  • 1
    $\begingroup$ 1/Sqrt[1]+1/Sqrt[4]+1/Sqrt[9]+1/Sqrt[16]+....+ ...>accurate $\endgroup$ – juan muñoz Jul 1 '15 at 22:03
5
$\begingroup$

Following belisarius's interpretation of your question, and if the total number of terms is not too large, I might write:

Total /@ (1.0/GroupBy[Array[Sqrt, 2003], IntegerQ])
<|True -> 4.37273, False -> 83.6879|>

Another formulation:

fn[x_Integer] := {1.0/x, 0};
fn[x_] := {0, 1.0/x}

Sum[fn @ Sqrt[k], {k, 1, 2003}]
{4.37273, 83.6879}

Neither of these will be particularly efficient but you did not state that as a goal and your question remains somewhat ambiguous.


Explanation

GroupBy method

This method starts by building an Array of all square roots of integers one through 2003. Then the GroupBy operator is applied with the parameter IntegerQ. This collects all integer values into one pile and all non-integer values into another. The output is an Association expression.

GroupBy[{1, 2, Pi, E}, IntegerQ]
<|True -> {1, 2}, False -> {π, E}|>

Association is specially handled by many operations, so for example multiplication is passed down to the values, and since they are lists and multiplication is Listable they are applied to each individual value:

x / <|True -> {1, 2}, False -> {π, E}|>
<|True -> {x, x/2}, False -> {x/π, x/E}|>

Map the Total function over this expression and you get the sums for each list:

 Total /@ <|True -> {x, x/2}, False -> {x/π, x/E}|>
<|True -> (3 x)/2, False -> x/E + x/π|>

If you wish the values can be extracted with Values:

% // Values
{(3 x)/2, x/E + x/π}

Sum

This method uses a symbolic Sum leveraging the fact that lists of the same length can be added:

{a1, b1} + {a2, b2} + {a3, b3}
{a1 + a2 + a3, b1 + b2 + b3}

So the function fn transforms its output into either the form {num, 0} for integer cases, or {0, num} for non-integer cases. The inversion is applied as part of this function also, because I wanted raw Sqrt intput to the function for the purpose of pattern matching.

$\endgroup$
  • $\begingroup$ This isn't different enough from your solution to post as a separate answer, but I want to record it because it incorporates language closer to what I think is the OP's concept of the problem. Total /@ KeyMap[If[#, "exact-root-sum", "inexact-root-sum"] &, 1.0/GroupBy[Array[Sqrt, 2003], ExactNumberQ]] which produces <|"exact-root-sum" -> 4.37273, "inexact-root-sum" -> 83.6879|> $\endgroup$ – m_goldberg Jul 1 '15 at 21:48
  • $\begingroup$ Total /@ (1.0/GroupBy[Array[Sqrt, 2003], IntegerQ]) <|True -> 4.37273, False -> 83.6879|> Another formulation: fn[x_Integer] := {1.0/x, 0}; fn[x_] := {0, 1.0/x} Sum[fn @ Sqrt[k], {k, 1, 2003}] {4.37273, 83.6879} it could explain one bit code ? $\endgroup$ – juan muñoz Jul 1 '15 at 22:07
  • $\begingroup$ @juan I added some explanation for my code. I hope it helps. $\endgroup$ – Mr.Wizard Jul 1 '15 at 22:20
3
$\begingroup$
Sum[If[IntegerQ[#], {1, 0}, {0, 1}]*1/# &@Sqrt[k], {k, 1, 2003}] // N

or

Sum[If[# ∈ Rationals, {1, 0}, {0, 1}]*# &[1/Sqrt[k]], {k, 1,  2003}] // N

both return

{4.37273, 83.6879}

$\endgroup$
  • $\begingroup$ You can replace Total@Table with Sum. $\endgroup$ – Karsten 7. Jul 1 '15 at 21:07
  • $\begingroup$ Why not start with Sum in your answer? $\endgroup$ – Mr.Wizard Jul 1 '15 at 21:10
  • $\begingroup$ Is not that, like, thanks $\endgroup$ – juan muñoz Jul 1 '15 at 22:05
  • $\begingroup$ @juanmuñoz Hope my revised answer does what you want. $\endgroup$ – Karsten 7. Jul 1 '15 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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