How to get REALLY fast python over a simple loop
- by totallymike
I'm working on a spoj problem, INTEST. The goal is to specify the number of test cases (n) and a divisor (k), then feed your program n numbers. The program will accept each number on a newline of stdin and after receiving the nth number, will tell you how many were divisible by k.
The only challenge in this problem is getting your code to be FAST because it k can be anything up to 10^7 and the test cases can be as high as 10^9.
I'm trying to write it in python and having trouble speeding it up. Any ideas?
import sys
first_in = raw_input()
thing = first_in.split()
n = int(thing[0])
k = int(thing[1])
total = 0
i = 0
for line in sys.stdin:
t = int(line)
if t % k == 0:
total += 1
print total