# Number-theoretic function using Table

I'm trying to make a function to calculate the Log sum of primes over a limited range $1/2n$ to $n$ or Chebychev theta function over limited range $1/2n$ to $n$. This will be used only for even numbers.

To start off I'm trying to modify a function that works:

LogSumPrime[n_] := Total[Log[Table[Prime[i], {i, PrimePi[n]}]]];


this will output as desired for LogSumPrime:

Log + Log + Log + Log + Log


So far so good. Now modifying to get range $1/2n$ to $n$:

LogSumAllUpperPrime[n_] :=
Total[Log[Table[(Prime[i + PrimePi[n] - PrimePi[n/2] + 1]),
{i, PrimePi[n] - PrimePi[n/2]}]]];


for LogSumAllUpperPrime, it is fine:

Log + Log


for LogSumAllUpperPrime, it is not fine:

Log


It should be Log + Log. Now the problem stems from half of 6 being odd and the limits not starting for this at 3, while for 12 it starts at 6 and the problem does not matter.

I do not understand Mathematica, so I cannot see how to solve this problem efficiently. If it were C, I'd just see if the bit is 1 or 0 to determine whether its even, but that may not be the best remedy here.

I would like to do this quite efficiently as I will be working with quite large numbers and the Table approach is said to be reasonably efficient.

• If primePi was input correctly then it depends on how that function is defined. If it was intended to be PrimePi[n/2] then possibly use of Floor or Ceiling will remedy the issue. – Daniel Lichtblau May 28 '18 at 14:49
• @Daniel Lichtblau I thought this initially too but this is not an issue since only even numbers are used so it made no difference when I tried so I removed it from the code. The actual problem is when half the n is an odd number. – onepound May 28 '18 at 15:07
• Please post the definition of primePi – m_goldberg May 28 '18 at 15:12
• Or when n/2 is prime... Could use Table[Prime[i + PrimePi[n/2] - Boole[PrimeQ[n/2]]], {i, PrimePi[n] - PrimePi[n/2] + Boole[PrimeQ[n/2]]}] – Daniel Lichtblau May 28 '18 at 17:49
• Asymptotically this approaches n/2. It has been a long time since I saw this nifty function (like, three decades). I had forgotten all about it, and the relation to PNT. – Daniel Lichtblau May 29 '18 at 2:46

Perhaps this is what you mean

LogSumPrime[n_] := Total@Log@Prime@Range@PrimePi@n

LogSumAllUpperPrime[n_] :=
Total@Log@Prime@Range[Max[1, PrimePi[n/2]], PrimePi@n]

{#, LogSumPrime[#], LogSumAllUpperPrime[#]} & /@ Range[2, 12, 2] //
Prepend[#, Style[#, Bold] & /@ {"n", LogSumPrime, LogSumAllUpperPrime}] & //
Grid[#, Frame -> All] & EDIT: Based on your comment, you apparently intend

LogSumAllUpperPrime[n_] := Total@Log@Select[Prime@Range@PrimePi@n, # >= n/2 &]

{#, LogSumPrime[#], LogSumAllUpperPrime[#]} & /@ Range[2, 12, 2] //
Prepend[#, Style[#, Bold] & /@ {"n", LogSumPrime, LogSumAllUpperPrime}] & //
Grid[#, Frame -> All] & • not quite I think LogSumAllUpperPrime[] with 8 and 12 should start at Log and Log otherwise it includes partial sums < n/2 – onepound May 28 '18 at 16:04
• yes that's it, thanks. – onepound May 28 '18 at 18:14