June 12th, 2011, 12:13 PM
Join Date: May 2011
Time spent in forums: 2 h 13 m 44 sec
Reputation Power: 0
Hi all. This script reads a file that contains a different integer on each line and outputs the total number of sequences of two or more consecutive integers that could add up to that number. I'm trying to make the script as fast as possible, and was wondering if anyone had any advice on speeding up the following python script.
from sys import argv,stdout
from math import sqrt
nums = map(int, open(argv, 'r').read().strip().split('\n'))
for n in nums:
thesum, themax = 0, (int(sqrt(float(2*n+0.25))-0.5))+1
if n >= 3:
for i in range(2, themax):
tmp = n%i
if (i%2==1 and tmp==0) or (i%2==0 and tmp==i/2):
thesum += 1
My very limited understanding of how the python interpreter works means I have no idea if I should be initializing tmp or letting it be calculated both times, for example.