I have the following method defined in a py
file:
def leerPrimos(ruta):
inicio = time.clock()
cuenta = 0
archOut = 'C:/Users/esteban/Desktop/taller-primos-complejidad-algoritmica/python-files/PYsalida2.out';
with open(ruta) as fIn:
with open(archOut,'a') as fOut:
for linea in fIn:
if is_prime(int(linea)):
fOut.write(linea)
print(linea)
final = time.clock()
tiemp = final - inicio
tiempo(tiemp)
then i Call the method:
leerPrimos("C:/Users/esteban/Desktop/taller-primos-complejidad-algoritmica/dataset.in")
The first with open(ruta)
works fine, and print(linea)
is shown in IDLE
console but fOut.write(linea)
is not working at all, when the method finishes, there's nothing written in the fOut
file.
Am I doing something wrong ? I'm a java developer but was asked to do some homework in python
please help. :)
EDIT here's the complete file:
import time
def leerPrimos(ruta):
inicio = time.clock()
cuenta = 0
archOut = 'C:/Users/esteban/Desktop/taller-primos-complejidad-algoritmica/python-files/PYsalida2.out';
with open(ruta) as fIn:
with open(archOut,'a+') as fOut:
for linea in fIn:
if is_prime(int(linea)):
fOut.write(linea)
print(linea)
final = time.clock()
tiemp = final - inicio
tiempo(tiemp)
def tiempo(milis):
seg = (milis / 1000) % 60
mins = ((milis / (1000 * 60)) % 60)
horas = ((milis / (1000 * 60 * 60)) % 60)
t = str(seg) + " segundos, " + str(mins) + " minutos, " + str(horas) + " horas"
print (t)
return t
def findPrimes():
start = time.clock()
MAX = 10000000
count = 0
# se hizo el ejercicio con solo 10 000 000 porque toma más de 30 horas en correr
# el algoritmo con 1 000 000 000
with open("C:/Users/esteban/Desktop/taller-primos-complejidad-algoritmica/python-files/PYsalida1.out", "a") as myfile:
for i in range(1,MAX+1):
if is_prime(i):
count += 1
print (str(i) + "\n")
myfile.write(str(i) + "\n")
end = time.clock()
print ("Tiempo tomado: " + str((end-start)))
print ("Números primos encontrados: " + str(count))
#fuente: https://rosettacode.org/wiki/Miller%E2%80%93Rabin_primality_test#Python
def _try_composite(a, d, n, s):
if pow(a, d, n) == 1:
return False
for i in range(s):
if pow(a, 2**i * d, n) == n-1:
return False
return True # n is definitely composite
#fuente: https://rosettacode.org/wiki/Miller%E2%80%93Rabin_primality_test#Python
def is_prime(n, _precision_for_huge_n=16):
if n in _known_primes or n in (0, 1):
return True
if any((n % p) == 0 for p in _known_primes):
return False
d, s = n - 1, 0
while not d % 2:
d, s = d >> 1, s + 1
# Returns exact according to http://primes.utm.edu/prove/prove2_3.html
if n < 1373653:
return not any(_try_composite(a, d, n, s) for a in (2, 3))
if n < 25326001:
return not any(_try_composite(a, d, n, s) for a in (2, 3, 5))
if n < 118670087467:
if n == 3215031751:
return False
return not any(_try_composite(a, d, n, s) for a in (2, 3, 5, 7))
if n < 2152302898747:
return not any(_try_composite(a, d, n, s) for a in (2, 3, 5, 7, 11))
if n < 3474749660383:
return not any(_try_composite(a, d, n, s) for a in (2, 3, 5, 7, 11, 13))
if n < 341550071728321:
return not any(_try_composite(a, d, n, s) for a in (2, 3, 5, 7, 11, 13, 17))
# otherwise
return not any(_try_composite(a, d, n, s)
for a in _known_primes[:_precision_for_huge_n])
_known_primes = [2, 3]
_known_primes += [x for x in range(5, 1000, 2) if is_prime(x)]
#findPrimes()
leerPrimos("C:/Users/esteban/Desktop/taller-primos-complejidad-algoritmica/dataset.in")