1
$\begingroup$

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[12]:

Log[2] + Log[3] + Log[5] + Log[7] + Log[11]

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[12], it is fine:

Log[7] + Log[11]

for LogSumAllUpperPrime[6], it is not fine:

Log[5]

It should be Log[3] + Log[5]. 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.

$\endgroup$
8
  • 1
    $\begingroup$ 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. $\endgroup$ Commented May 28, 2018 at 14:49
  • $\begingroup$ @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. $\endgroup$
    – onepound
    Commented May 28, 2018 at 15:07
  • $\begingroup$ Please post the definition of primePi $\endgroup$
    – m_goldberg
    Commented May 28, 2018 at 15:12
  • 2
    $\begingroup$ 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]]}] $\endgroup$ Commented May 28, 2018 at 17:49
  • 1
    $\begingroup$ 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. $\endgroup$ Commented May 29, 2018 at 2:46

1 Answer 1

4
$\begingroup$

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] &

enter image description here

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] &

enter image description here

$\endgroup$
2
  • $\begingroup$ not quite I think LogSumAllUpperPrime[] with 8 and 12 should start at Log[5] and Log[7] otherwise it includes partial sums < n/2 $\endgroup$
    – onepound
    Commented May 28, 2018 at 16:04
  • $\begingroup$ yes that's it, thanks. $\endgroup$
    – onepound
    Commented May 28, 2018 at 18:14

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.