I assume you are looking for a C function because the python code is too slow. If that is the case you can use cython to convert the python script to C. In sage you can do that just by adding %cython in the beginning of the notebook. You can also get a library file (.so) which can be called from python. In order to do that just write the code in python with the extention pyx and compile it with following commands.
cython filename.pyx
gcc -O3 -I/usr/include/python2.6/ -I/Library/Python/2.6/site-packages/numpy-2.0.0.dev_f72c605_20110113-py2.6-macosx-10.6-universal.egg/numpy/core/include/ -c -o filename.o filename.c
gcc -dynamiclib -undefined dynamic_lookup -single_module -o filename.so filename.o
for Mac. For Linux use
cython -a filename.pyx
gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.5 -o filename.so filename.c
You may find this link useful.
http://www.sagemath.org/doc/developer/coding_in_cython.html
This way the code runs almost as fast as a C code.