2
$\begingroup$

I'd like to evaluate

$$\prod_p\left(1-\frac1p\right)$$

over the $k$ largest previous primes $p\le n/2$, where $n$ is the parameter. E.g., if $n=20$ and $k=3$, I'd like the product to be over the 3 previous primes less than or equal to $20/2=10$: namely, $7,5,3$.

The code for the previous prime is

PreviousPrime[n_Integer] := Block[{i = n}, While[! PrimeQ[-i]]; i]
SetAttributes[PreviousPrime, Listable];

How can I take a product over $k$ previous primes?

$\endgroup$
4
$\begingroup$

You can use NextPrime to find previous primes too, so you don't need your PreviousPrime function.

To get your product, you can use

primeprod[n_, k_] := Product[1 - 1/i, {i, NextPrime[n/2, Range[-k, -1]]}]

Then

primeprod[20, 3]

(* 16/35 *)

You can also generalise it by adding the product function as an argument:

primeprod2[n_, k_, f_] := Product[f[i], {i, NextPrime[n/2, Range[-k, -1]]}]

In which case

primeprod2[20, 3, 1 - 1/# &]

(* 16/35 *)
$\endgroup$
0
$\begingroup$

One can of course use PrimePi[] + Prime[] instead:

primeprod[n_, k_, f_] :=
With[{p = PrimePi[n/2]}, Apply[Times, f[Prime[Range[p - k + 1, p]]]]]

primeprod[20, 3, 1 - 1/# &]
   16/35
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.