Topic: Prime counting function

Hi Ivan,

Graph has some great functions, such as gamma and zeta, but none that directly relate to primes, such as:

pi(n):  You would need another name for the function since pi is used as a constant in Graph.

isprime(n): For small n there are some good ideas on efficiently storing a lookup table of small primes at http://www.rsok.com/~jrm/printprimes.html and for large n there are primality tests much faster than trial division by all primes to sqrt(n).

iscomposite(n) or notprime(n): This one could be implemented using the Miller–Rabin test.

Graphing the behavior of primes is highly desirable for any graphing program. I might be able to implement a simple sieve just using Graph's existing functions. Though I suspect your recommendation will be to use a Python script, having some functions for working with primes built in to Graph would be more convenient.  Remember, accuracy is only required to the limit of Graph's x-axis, not infinity.

Some other functions to consider implementing that would be useful to number theory graphs:

Riemann's prime-counting function
Logarithmic integral function
Exponential integral function
von Mangoldt function
Möbius function
Euler's totient (phi) function
sfn(n)  is n a square free number?
Lucas-Lehmer test

Re: Prime counting function

The functions you suggest do not seem widely used. I therefore think it would be best to have them as custom functions or implemented in a Python script. I might add a script with rare functions to one of the next versions, but meanwhile you can also do it yourself.

Re: Prime counting function

Including a script with the package would serve as a practical example of how to extend graph by using Python. It would assist those who are unfamiliar with either using Python scripts or how to program specialized functions. I think it would be valuable "documentation" for showing the power and versatility of adding Python scripts to Graph. It might even start a community of contributors sharing other scripted specialized functions and applications designed for Graph. I think it's a great idea. You have my two thumbs up for including some example scripts with a future version.

In the meantime, I'm in the category of being unfamiliar with the Python scripting language. Would it be possible to post or refer me to an example of a script that implements a function not native to graph and an example graph using it?

Re: Prime counting function

For learning Python I can recommend the tutorial in the help installed with Python. There are several example scripts installed with Graph but not one that shows how to add functions.

But here is an example. If you place the following code in a file called sinc.py in the Plugins directory and you have Python 3.2 installed, you will be able to use the function sinc in Graph like f(x)=sinc(x):

import Graph
import math

def sinc(x):
    if x == 0:
        return 1
    return math.sin(x) / x

Graph.CustomFunctions["sinc"] = sinc

Re: Prime counting function

So Graph can use Python math functions by just creating a file like mymath.py containing

Import Graph
Import math

Graph.CustomFunctions["pmf1"] = pmf1
Graph.CustomFunctions["pmf2"] = pmf2
Graph.CustomFunctions["pmf3"] = pmf3

or does each function need to be in a separate file?

Do you know if there is a significant overhead in calling Python functions? For example, sinc() could also be a custom function in Graph. Would graphing speed of a complicated function using sinc() be noticeably different between the Graph defined sinc() and the Python defined sinc()?

Re: Prime counting function

You can add as many functions in one file as you wish.

There is an overhead when calling into Python. I just made a quick test showing that sinc(x) is between 4 and 5 times as slow when implemented in Python compared to a custom function in Graph. But this may vary depending on the complexity of the function.