# Expressing a series formula

I want to generate a series of the following kind in Mathematica:

$\quad \quad a(n+1) = a(n) + ({\rm prime}(n+1) - 1)/2 \quad \mbox{for odd primes},$

so that the resultant series is

1, 2, 4, 7, 12, 18, 26, 35, ...


I have no idea how to find the odd prime numbers.

Link showing the general formula for series.

Here is a direct implementation of your series formula:

a = 1
a[n_] := a[n - 1] + (Prime[n] - 1)/2


You can speed up calculation by memoizing a:

a = 1
a[n_] := a[n] = a[n - 1] + (Prime[n] - 1)/2

• thanks a lot for the helpful reply. – zenith Apr 21 '15 at 21:36

You can also use RecurrenceTable:

ClearAll[a, n]
RecurrenceTable[{a[n + 1] == a[n] + (Prime[n + 1] - 1)/2, a == 1}, a, {n, 1, 10}]
(* {1, 2, 4, 7, 12, 18, 26, 35, 46, 60} *)


Prime[n] returns the $n$th prime number, and all primes except Prime are odd. So the following works:

Accumulate[Prepend[Table[(Prime[i] - 1)/2, {i, 2, 10}], 1]]

• thank you. it generated the required results. – zenith Apr 21 '15 at 21:33