Skip to content

Instantly share code, notes, and snippets.

@goulu
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goulu/0e35207dedf4e337c937 to your computer and use it in GitHub Desktop.
Save goulu/0e35207dedf4e337c937 to your computer and use it in GitHub Desktop.
Sum of Consecutive Primes
SCP[i_, n_] := Sum [Prime[j], {j, i, i + n - 1}]
PSP[n_, i_] := {m = i; While[p = SCP[m, n]; Not[PrimeQ[p]], m++]; p, m}
PSP[n_] := PSP[n, 1]
CPSP[list_] := {p = Map[PSP, list]; p1 = {};
While[p != p1, p1 = p; Print[p];
For[i = 2, i <= Length[list], i++,
While[p[[i, 1]] < p[[1, 1]],
p = ReplacePart[p, i -> PSP[list[[i]], p[[i, 2]] + 1]]];
While[p[[1, 1]] < p[[i, 1]],
p = ReplacePart[p, 1 -> PSP[list[[1]], p[[1, 2]] + 1]]];
];
]
@goulu
Copy link
Author

goulu commented May 10, 2015

code for Google Treasure Hunt 2008 explained (in french) here : http://www.drgoulu.com/2008/06/03/sommes-egales-de-nombres-premiers-consecutifs/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment