Message Boards Message Boards

0
|
5929 Views
|
1 Reply
|
0 Total Likes
View groups...
Share
Share this post:

best way to replace element in list with sum of two consecutive elements?

Posted 10 years ago
POSTED BY: p linh

Hi, your spec didn't seem quite consistent, but hopefully the code below will give you some ideas, particularly with the use of Accumulate and TakeWhile.

g[2] = {2, 4};
g[i_] := Nest[Join[#, g[i - 1]] &, g[i - 1], Prime[i] - 1]

hsum[i_] := Module[{g1, sum, p, g2, t},
  g1 = g[i];
  sum = g1[[i - 1]] + g1[[i]];
  p = Prime[i];
  If[Mod[sum, p] == 1,
   g2 = ReplacePart[Delete[g1, i], (i - 1) -> sum];
   t = TakeWhile[Drop[Accumulate[g1], 2], Mod[#, p] != 1 &];
   Take[g2, Length[t] + 1]]]

hsum[3]
POSTED BY: Chris Degnen
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract