10
$\begingroup$

To know how fast values in a list approach 1 I tried

#[[1]]/#[[2]] & /@ Partition[{5040, 1460, 280, 76, 16, 2, 1}, 2, 1]

This works, but I seek a better method, perhaps through mapping a function over the list:

f /@ {5040, 1460, 280, 76, 16, 2, 1}

What could be the form of this f?

$\endgroup$

1 Answer 1

15
$\begingroup$
list = {5040, 1460, 280, 76, 16, 2, 1};

Ratios:

1 / Ratios @ list

{252/73, 73/14, 70/19, 19/4, 8, 2}

Reverse @ Ratios @ Reverse @ list

{252/73, 73/14, 70/19, 19/4, 8, 2}

Divide:

Divide[Most @ #, Rest @ #]& @ list

{252/73, 73/14, 70/19, 19/4, 8, 2}

MovingMap:

MovingMap[Divide @@ # &, list, 1]

{252/73, 73/14, 70/19, 19/4, 8, 2}

BlockMap:

BlockMap[Divide @@ # &, list, 2, 1]

{252/73, 73/14, 70/19, 19/4, 8, 2}

Partition + Divide:

Divide @@@ Partition[list, 2, 1]

{252/73, 73/14, 70/19, 19/4, 8, 2}

Partition[list, 2, 1, {1, -1}, {}, Divide]

{252/73, 73/14, 70/19, 19/4, 8, 2}

$\endgroup$
1
  • 2
    $\begingroup$ +1 There is also Most[#]/Rest[#]&@list. Of these, Reverse@Ratios@Reverse@list is the quickest (RepeatedTiming) for this size list. $\endgroup$
    – Bob Hanlon
    Commented Jan 1, 2020 at 19:14

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.