2
$\begingroup$

Assume we have a set of numbers, for instance $A=\{2,3,4,5,6,7,8,9,10\}$ and we are looking for the sums of reciprocals such that they are less than one. I mean I am looking for all $S_B$'s $$ S_B=\sum_{i\in B}\frac1i<1, $$ where $B\subset A$. How can I write such code in "Mathematica"?

I wrote the following code but it does not work properly

Clear[A]
A = {2, 3, 4, 5, 6, 7, 8, 9, 10};
Table[If[SubsetQ[A,B] == True&& Sum[1/i, {i, B}]<1,Sum[1/i, {i, B}], Nothing], {B,A}]
$\endgroup$
3
$\begingroup$

Something like this?

A = Subsets@Range[2., 10.];
Select[{#, Sum[i^-1, {i, #}]} & /@ A, #[[2]] < 1 &]

{{{},0},{{2.},0.5},{{3.},0.333333},{{4.},0.25},<<267>>,{{4.,5.,7.,8.,9.,10.},0.928968},{{4.,6.,7.,8.,9.,10.},0.895635},{{5.,6.,7.,8.,9.,10.},0.845635}}

$\endgroup$
  • $\begingroup$ thanks, what if we need only the second values, {0,0.5,0.3333,0.25,...}? $\endgroup$ – asad Nov 11 at 10:42
  • 1
    $\begingroup$ @asad If you only want the sums and don't care about the subsets that correspond to them, then just use Select[Sum[i^-1, {i, #}] & /@ A, # < 1 &]. $\endgroup$ – That Gravity Guy Nov 11 at 10:50
  • 2
    $\begingroup$ The sum can also be written as Total[1/#]& for improved readability & speed $\endgroup$ – Lukas Lang Nov 11 at 13:16
4
$\begingroup$
selected = Select[# < 1 &] @ Total[Subsets[1. /Range[2, 10]], {2}];

selected // Short

{0., 0.5, 0.333333, 0.25, 0.2, << 265 >>, 0.952778, 0.928968, 0.895635, 0.845635}

Length @ selected

274

$\endgroup$

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.