# Sum broken into two sub-sums according to a condition on the terms

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}]

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

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.

• 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|> – m_goldberg Jul 1 '15 at 21:48
• 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 ? – juan muñoz Jul 1 '15 at 22:07
• @juan I added some explanation for my code. I hope it helps. – Mr.Wizard Jul 1 '15 at 22:20
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}

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