I think Python is capable of executing a compiled C or FORTRAN program,
Sure, if it was compiled to an .exe file.
and maybe even getting some parameters passed back.
Sure, if the program prints to stdout.
Does anyone have a
example of how this might be done? I'm running under Win XP Pro.
Here's one. The test program is factor.exe (included in
the MIRACL library). I recompiled it (factor!.exe) to
produce consitent output.
Instead of stupidly saying
C:\factor!\miracl\source>factor 31
31
this number is prime!
I now have it print
C:\factor!\miracl\source>factor! 31
PRIME_FACTOR 31
I now call it from Python and have Python fix its mistakes.
## Mistakes? - numbers reported as COMPOSITE may still
## be factorable. Composites are not returned to the
## start of the algorithm for re-factoring. But that's ok,
## if I don't like it, I'm free to fix it myself since
## I have the source code (so says the author).
##
## Hey, I won't look a gift horse in the mouth, but you
## are advised that when you see...
##
## C:\factor!\miracl\source>factor! 5081842980034330599
## 30221143303110332712493139579190463526792062
## 62204589342623811236647989889145173098650749
##
## PRIME_FACTOR 37
## PRIME_FACTOR 43
## PRIME_FACTOR 167
## COMPOSITE_FACTOR 507787751
## PRIME_FACTOR 69847
## PRIME_FACTOR 30697
## PRIME_FACTOR 89017
## PRIME_FACTOR 3478697
## PRIME_FACTOR 434593
## PRIME_FACTOR 49998841
## PRIME_FACTOR 161610704597143
## PRIME_FACTOR 14064370273
## COMPOSITE_FACTOR 963039394703598565337297
## PRIME_FACTOR 11927295803
##
## ...you should follow that with...
##
## C:\factor!\miracl\source>factor! 507787751
## PRIME_FACTOR 29819
## PRIME_FACTOR 17029
##
## C:\factor!\miracl\source>factor! 963039394703598565337297
## PRIME_FACTOR 518069464441
## PRIME_FACTOR 1858900129817
import os
import random
import time
factor_program = 'factor! -d200 '
the_composites =
[['COMPOSITE_FACTOR','50818429800343305993022114330311033271249313957919046352679206262204589342623811236647989889145173098650749']]
##random_digits = [str(random.randint(0,9)) for d in range(30)]
##test_number = ''.join(random_digits)
##the_composites = [['COMPOSITE_FACTOR',test_number]]
the_primes = []
the_intractables = []
phase = 1
the_times = []
while the_composites:
print "="*40
print 'Phase',phase
the_comp = the_composites.pop(0)
print the_comp
print
the_times.append(time.time()) # time how long it takes to run
factor!.exe
the_output = os.popen(factor_program+the_comp[1]).readlines()
the_times.append(time.time())
new_factors = [i.split() for i in the_output]
for i in new_factors: print i
print
if len(new_factors) == 1:
# it's prime or intractable
if new_factors[0][0] == 'PRIME_FACTOR':
the_primes.append([new_factors[0][0],long(new_factors[0][1])])
else:
the_intractables.append([new_factors[0][0],long(new_factors[0]
[1])])
new_factors.pop()
while new_factors:
j = new_factors.pop(0)
if j[0] == 'PRIME_FACTOR':
the_primes.append([j[0],long(j[1])])
else:
the_composites.append(j)
print the_times[phase] - the_times[phase-1],'seconds'
phase += 1
print "="*40
print
print 'Factoring complete'
print
the_primes.sort()
the_intractables.sort()
the_primes.extend(the_intractables)
for i in the_primes:
print i[0],i[1]
print
print "="*40
## ========================================
## Phase 1
## ['COMPOSITE_FACTOR',
'50818429800343305993022114330311033271249313957919046352679206262204589342623811236647989889145173098650749']
##
## ['PRIME_FACTOR', '37']
## ['PRIME_FACTOR', '43']
## ['PRIME_FACTOR', '167']
## ['COMPOSITE_FACTOR', '507787751']
## ['PRIME_FACTOR', '69847']
## ['PRIME_FACTOR', '30697']
## ['PRIME_FACTOR', '89017']
## ['PRIME_FACTOR', '3478697']
## ['PRIME_FACTOR', '434593']
## ['PRIME_FACTOR', '49998841']
## ['PRIME_FACTOR', '161610704597143']
## ['PRIME_FACTOR', '14064370273']
## ['COMPOSITE_FACTOR', '963039394703598565337297']
## ['PRIME_FACTOR', '11927295803']
##
## ========================================
## Phase 2
## ['COMPOSITE_FACTOR', '507787751']
##
## ['PRIME_FACTOR', '29819']
## ['PRIME_FACTOR', '17029']
##
## ========================================
## Phase 3
## ['COMPOSITE_FACTOR', '963039394703598565337297']
##
## ['PRIME_FACTOR', '518069464441']
## ['PRIME_FACTOR', '1858900129817']
##
## ========================================
##
## Factoring complete
##
## PRIME_FACTOR 37
## PRIME_FACTOR 43
## PRIME_FACTOR 167
## PRIME_FACTOR 17029
## PRIME_FACTOR 29819
## PRIME_FACTOR 30697
## PRIME_FACTOR 69847
## PRIME_FACTOR 89017
## PRIME_FACTOR 434593
## PRIME_FACTOR 3478697
## PRIME_FACTOR 49998841
## PRIME_FACTOR 11927295803
## PRIME_FACTOR 14064370273
## PRIME_FACTOR 518069464441
## PRIME_FACTOR 1858900129817
## PRIME_FACTOR 161610704597143
##
## ========================================