1
$\begingroup$

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.

$\endgroup$
2

3 Answers 3

5
$\begingroup$

Here is a direct implementation of your series formula:

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

You can speed up calculation by memoizing a:

a[1] = 1
a[n_] := a[n] = a[n - 1] + (Prime[n] - 1)/2
$\endgroup$
1
  • $\begingroup$ thanks a lot for the helpful reply. $\endgroup$
    – zenith
    Commented Apr 21, 2015 at 21:36
4
$\begingroup$

You can also use RecurrenceTable:

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

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

Accumulate[Prepend[Table[(Prime[i] - 1)/2, {i, 2, 10}], 1]]
$\endgroup$
1
  • $\begingroup$ thank you. it generated the required results. $\endgroup$
    – zenith
    Commented Apr 21, 2015 at 21:33

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.