5

Given

p={2, 3, 5, 7, 11, 13, 17, 19}

I want to get the values which can be calculated by

 Table[(p[[i + 1]]*p[[i + 3]] - p[[i]]*p[[i + 2]]), {i, 5}]

resulting in

 {11, 34, 36, 96, 60}

I also can do it by

p4 = Partition[p, 4, 1];

f[a_, b_, c_, d_] := bd - ac

f[#, #2, #3, #4] & @@@ p4

What would be a concise way using FoldList without using Partition beforehand?

user57467
  • 2,708
  • 6
  • 12

3 Answers3

4

"... without using Partition":

BlockMap[f @@ # &, p, 4, 1]
{11, 34, 36, 96, 60}
kglr
  • 394,356
  • 18
  • 477
  • 896
  • Wow! I never came across BlockMap. What would be a similiar solution as given in [120904] (https://mathematica.stackexchange.com/questions/120904/problem-with-foldlist-use-foldlist-with-more-than-one-list-use-foldlist-with-mu) italic bold – user57467 Mar 29 '21 at 12:37
2
p = {2, 3, 5, 7, 11, 13, 17, 19};

f[a_, b_, c_, d_] := b*d - a*c

Three variants of SequenceCases

SequenceCases[p, {a_, b_, c_, d_} :> f[a, b, c, d], Overlaps -> True]

{11, 34, 36, 96, 60}

SequenceCases[p, x : {_, _, _, _} :> f @@ x, Overlaps -> True]

{11, 34, 36, 96, 60}

SequenceCases[p, x : {Repeated[_, {4}]} :> f @@ x, Overlaps -> True]

{11, 34, 36, 96, 60}

eldo
  • 67,911
  • 5
  • 60
  • 168
2
p = {2, 3, 5, 7, 11, 13, 17, 19};

f[a_, b_, c_, d_] := b*d - a*c

Using MovingMap:

Drop[MovingMap[f @@ # &, p, 3, 1], 3]

({11, 34, 36, 96, 60})

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44