Homesick Iguana
Homesick Iguana

Reputation: 103

Writing a program to calculate the distinct values of a function

$$f_n(x)=\pi(x)\pi(n-x)$$

I want to write a program that tells me the distinct values of $f_n(x)$ for each $n=2,3,4,...,N$ where $\pi(x)$ is the prime counting function. Here are the first few distinct values:

S={0,0,1,1,2,2,3,2,4,3,4,3,5,4,5,4,6,5,7,5,8,5,7,5,8,7,...}.

How can I write such a program?

I tried using Mathematica, wolfram alpha, and R but couldn't figure out how to make the software tell me the distinct values of $f_n(x).$

I have no idea how to solve this problem using a computer program so I manually put the first 70 values of n into wolfram alpha one at a time and counted the number of distinct values by looking at the plots. This is what I was able to get so far:

The original prime counting function is in light green and my function is in light blue: (here I've plotted n on the x-axis and the distinct values on the y-axis)

enter image description here

Here's an example of n=9 corresponding to the two horizontal lines that correspond to the two distinct values for the function:

enter image description here

Upvotes: 0

Views: 144

Answers (2)

Chris Degnen
Chris Degnen

Reputation: 8680

Further to your clarification

f[n_, x_] := PrimePi[x]*PrimePi[n - x]

green = {#, PrimePi[#]} & /@ Range[2, 70];

blue = Map[Function[n,
   {n, Length[DeleteCases[Union[f[n, #] & /@ Range[2, 70]], 0]]}],
  Range[2, 70]];

ListPlot[{blue, green}, PlotStyle -> {Blue, Green}, AxesLabel -> {"n", None},
 PlotMarkers -> {Automatic, Medium}, PlotRange -> {{0, 70}, {0, 20}},
 PlotRangePadding -> 0.6, AspectRatio -> 2/7, ImageSize -> 600]

enter image description here

Note there are differences from the OP's plot at n = 60, 61. Perhaps the OP miscounted.

Upvotes: 1

ThomasIsCoding
ThomasIsCoding

Reputation: 102770

In R, you need the package primes and then can define your prime-count function $\pi(x)$ as below:

PIx <- Vectorize(function(x) length(generate_primes(max = x)),vectorize.args = "x")

For instance, given an input vector v<-c(0,1,4.5,6.1,10), then PIx(v) gives:

> PIx(v)
[1] 0 0 2 3 4

Then for your function $f_n(x)$, it can be defined as:

PIx <- Vectorize(function(x) length(generate_primes(max = x)),vectorize.args = "x")
f_n <- function(n) unique(PIx(0:n)*PIx(n-(0:n)))

To see the distinct values when varying $n$, you apply the code Filter(length,sapply(2:n, function(k) f_n(k))), for example:

n <- 15
z <- unlist(Map(length,Filter(length,sapply(2:n, function(k) f_n(k)))))-1

which gives a list of distinct levels:

> z
 [1] 0 0 1 1 2 2 3 2 4 3 4 3 5 4

Upvotes: 2

Related Questions