6 / 8
May 2007

j=1
v=0
n=int(raw_input())
for v in range (0,n):
a=int(raw_input())
s=0
for j in range(1,a):
if (a%j/2==0):
s+=j
j=j+1
print s

I am getting TLE..help me plz

  • created

    Feb '07
  • last reply

    May '18
  • 7

    replies

  • 1.5k

    views

  • 5

    users

  • 1

    link

It is not feasible to do this quickly in Python, because the language is partially interpreted. You should try C/C++/Pascal for this question...

3 months later

Actually, it it possible to solve it in Python.

There are two problems with your approach: one with the speed of I/O and the other with the speed of algorithm.

While int(raw_input()) is fast, it is not fast enough for this problem. You may want to take a look at my solution for INTEST.

Your brute force approach is also very, very slow. There are much better solutions with brute force and they might work.
But I suggest to also use google for "divisor summation" or similar terms.

Hello Crististm,

I am very interested to know how you can speedup your I/O.
For DIVSUM I am using sys.stdin.readline to read input and sys.stdout.write to output result.
But it is not efficent enough.
Can you help me as I see that you have succeed in that problem with Python?

Very thanx in advance

Thank you very much.
Yesterday I have used your code. It was a success.
But I don't understand why on my computer the instruction readlines(100000) gets an error. But on SPOJ server it is OK.

I'm glad you found the code useful.

readlines(100000) should not give an error even if the file is less than 100000 bytes. On my PC I don't get an error for any of the test cases I tried, small or large.

10 years later

My code:
int main()
{
int i,j,n,m;
scanf("%d",&n);
for(i=0;i<n;i++)
{
int sum=0;
scanf("%d",&m);
for(j=1;j<m;j++)
{

		if(m%j==0)
		{
			sum=sum+j;
		}
	}
	printf("%d\n",sum);
}
return 0;

}
“int sum” you NEED placing it in “for()” function, if you place it above for(), “sum” will sum=sum+all result