C function in a Python context

castironpi at gmail.com castironpi at gmail.com
Sat Feb 9 20:48:51 CET 2008


To write quick C things that Python won't do up to speed.  So it's got
a redundancy.

import ext
extA= ext.Ext()
extA[ 'enumfactors' ]= r"""
    int enumfactors( int a, const char* sep ) {
        int ret= 0, i;
        for( i= 1; i<= a; i++ ) {
            if( a% i== 0 ) {
                ret+= 1;
                if( i> 1 ) {
                    printf( "%s", sep );
                }
                printf( "%i", i );
            }
        }
        printf( "\n" );
        return ret;
    }
    """, ("i","i","s")

factorsn= extA.enumfactors( 209677683, ', ' )
print( "%i factors total."% factorsn )

import sys
sys.exit()


1, 3, 23, 69, 131, 393, 3013, 9039, 23197, 69591, 533531, 1600593,
3038807, 9116
421, 69892561, 209677683
16 factors total.



More information about the Python-list mailing list