Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

The problem I'm solving is:

given a length of an arithmetic progression, and a limit to the terms (see below), find all progressions that work.

All terms of the progressions must be of the form a2+b2, and 0 ≤ a2+b2 ≤ limit.

I have the following code:

from math import ceil
with open('ariprog.in') as fin:
 ariLen = int(fin.readline().strip())
 ariLim = int(fin.readline().strip())
def generate(bound):
 max_len=((bound**2)*2)+1
 parity = [0]*max_len
 for i in range(bound+1):
 for j in range(bound+1):
 parity[i**2+j**2] = 1
 return parity
parity = generate(ariLim)
lenpar = len(parity)
big_mama_ar = []
# print(lenpar)
for a in range(lenpar-1):
 if parity[a] == 1:
 for d in range(1, ceil((lenpar-a)/(ariLen-1))):
 for n in range(1, ariLen):
 # print('a:', a)
 # print('d:', d)
 # print('n:', n)
 if parity[a+n*d] != 1:
 break
 else:
 big_mama_ar.append((a,d))
 pass
big_mama_ar.sort(key=lambda x: x[1])
with open('ariprog.out', 'w') as fout:
 if big_mama_ar == []:
 fout.write('NONE\n')
 else:
 for i in big_mama_ar:
 fout.write(str(i[0])+' '+str(i[1])+'\n')

This code times out on my grader when ariLen is 21 and ariLim is 200. The time limit is 5 seconds, and on my computer, it takes 22 seconds. ariprog.in is

21

200

The problem I'm solving is:

given a length of an arithmetic progression, and a limit to the terms (see below), find all progressions that work.

All terms of the progressions must be of the form a2+b2, and 0 ≤ a2+b2 ≤ limit.

I have the following code:

from math import ceil
with open('ariprog.in') as fin:
 ariLen = int(fin.readline().strip())
 ariLim = int(fin.readline().strip())
def generate(bound):
 max_len=((bound**2)*2)+1
 parity = [0]*max_len
 for i in range(bound+1):
 for j in range(bound+1):
 parity[i**2+j**2] = 1
 return parity
parity = generate(ariLim)
lenpar = len(parity)
big_mama_ar = []
# print(lenpar)
for a in range(lenpar-1):
 if parity[a] == 1:
 for d in range(1, ceil((lenpar-a)/(ariLen-1))):
 for n in range(1, ariLen):
 # print('a:', a)
 # print('d:', d)
 # print('n:', n)
 if parity[a+n*d] != 1:
 break
 else:
 big_mama_ar.append((a,d))
 pass
big_mama_ar.sort(key=lambda x: x[1])
with open('ariprog.out', 'w') as fout:
 if big_mama_ar == []:
 fout.write('NONE\n')
 else:
 for i in big_mama_ar:
 fout.write(str(i[0])+' '+str(i[1])+'\n')

This code times out on my grader when ariLen is 21 and ariLim is 200. The time limit is 5 seconds, and on my computer, it takes 22 seconds. ariprog.in is

21

200

The problem I'm solving is:

given a length of an arithmetic progression, and a limit to the terms (see below), find all progressions that work.

All terms of the progressions must be of the form a2+b2, and 0 ≤ a2+b2 ≤ limit.

I have the following code:

from math import ceil
with open('ariprog.in') as fin:
 ariLen = int(fin.readline().strip())
 ariLim = int(fin.readline().strip())
def generate(bound):
 max_len=((bound**2)*2)+1
 parity = [0]*max_len
 for i in range(bound+1):
 for j in range(bound+1):
 parity[i**2+j**2] = 1
 return parity
parity = generate(ariLim)
lenpar = len(parity)
big_mama_ar = []
# print(lenpar)
for a in range(lenpar-1):
 if parity[a] == 1:
 for d in range(1, ceil((lenpar-a)/(ariLen-1))):
 for n in range(1, ariLen):
 # print('a:', a)
 # print('d:', d)
 # print('n:', n)
 if parity[a+n*d] != 1:
 break
 else:
 big_mama_ar.append((a,d))
 pass
big_mama_ar.sort(key=lambda x: x[1])
with open('ariprog.out', 'w') as fout:
 if big_mama_ar == []:
 fout.write('NONE\n')
 else:
 for i in big_mama_ar:
 fout.write(str(i[0])+' '+str(i[1])+'\n')

This code times out on my grader when ariLen is 21 and ariLim is 200. The time limit is 5 seconds, and on my computer, it takes 22 seconds. ariprog.in is

21

200

Tweeted twitter.com/StackCodeReview/status/1264345459331665927
added 32 characters in body
Source Link

The problem I'm solving is:

given a length of an arithmetic progression, and a limit to the terms (see below), find all progressions that work.

All terms of the progressions must be of the form a2+b2, and 0 ≤ a2+b2 ≤ limit.

I have the following code:

from math import ceil
with open('ariprog.in') as fin:
 ariLen = int(fin.readline().strip())
 ariLim = int(fin.readline().strip())
def generate(bound):
 max_len=((bound**2)*2)+1
 parity = [0]*max_len
 for i in range(bound+1):
 for j in range(bound+1):
 parity[i**2+j**2] = 1
 return parity
parity = generate(ariLim)
lenpar = len(parity)
big_mama_ar = []
# print(lenpar)
for a in range(lenpar-1):
 if parity[a] == 1:
 for d in range(1, ceil((lenpar-a)/(ariLen-1))):
 for n in range(1, ariLen):
 # print('a:', a)
 # print('d:', d)
 # print('n:', n)
 if parity[a+n*d] != 1:
 break
 else:
 big_mama_ar.append((a,d))
 pass
big_mama_ar.sort(key=lambda x: x[1])
with open('ariprog.out', 'w') as fout:
 if big_mama_ar == []:
 fout.write('NONE\n')
 else:
 for i in big_mama_ar:
 fout.write(str(i[0])+' '+str(i[1])+'\n')

This code times out on my grader when ariLen is 21 and ariLim is 200. The time limit is 5 seconds, and on my computer, it takes 22 seconds. ariprog.in is

21

200

The problem I'm solving is:

given a length of an arithmetic progression, and a limit to the terms (see below), find all progressions that work.

All terms of the progressions must be of the form a2+b2, and 0 ≤ a2+b2 ≤ limit.

I have the following code:

from math import ceil
with open('ariprog.in') as fin:
 ariLen = int(fin.readline().strip())
 ariLim = int(fin.readline().strip())
def generate(bound):
 max_len=((bound**2)*2)+1
 parity = [0]*max_len
 for i in range(bound+1):
 for j in range(bound+1):
 parity[i**2+j**2] = 1
 return parity
parity = generate(ariLim)
lenpar = len(parity)
big_mama_ar = []
# print(lenpar)
for a in range(lenpar-1):
 if parity[a] == 1:
 for d in range(1, ceil((lenpar-a)/(ariLen-1))):
 for n in range(1, ariLen):
 # print('a:', a)
 # print('d:', d)
 # print('n:', n)
 if parity[a+n*d] != 1:
 break
 else:
 big_mama_ar.append((a,d))
 pass
big_mama_ar.sort(key=lambda x: x[1])
with open('ariprog.out', 'w') as fout:
 if big_mama_ar == []:
 fout.write('NONE\n')
 else:
 for i in big_mama_ar:
 fout.write(str(i[0])+' '+str(i[1])+'\n')

This code times out on my grader when ariLen is 21 and ariLim is 200. The time limit is 5 seconds, and on my computer, it takes 22 seconds.

The problem I'm solving is:

given a length of an arithmetic progression, and a limit to the terms (see below), find all progressions that work.

All terms of the progressions must be of the form a2+b2, and 0 ≤ a2+b2 ≤ limit.

I have the following code:

from math import ceil
with open('ariprog.in') as fin:
 ariLen = int(fin.readline().strip())
 ariLim = int(fin.readline().strip())
def generate(bound):
 max_len=((bound**2)*2)+1
 parity = [0]*max_len
 for i in range(bound+1):
 for j in range(bound+1):
 parity[i**2+j**2] = 1
 return parity
parity = generate(ariLim)
lenpar = len(parity)
big_mama_ar = []
# print(lenpar)
for a in range(lenpar-1):
 if parity[a] == 1:
 for d in range(1, ceil((lenpar-a)/(ariLen-1))):
 for n in range(1, ariLen):
 # print('a:', a)
 # print('d:', d)
 # print('n:', n)
 if parity[a+n*d] != 1:
 break
 else:
 big_mama_ar.append((a,d))
 pass
big_mama_ar.sort(key=lambda x: x[1])
with open('ariprog.out', 'w') as fout:
 if big_mama_ar == []:
 fout.write('NONE\n')
 else:
 for i in big_mama_ar:
 fout.write(str(i[0])+' '+str(i[1])+'\n')

This code times out on my grader when ariLen is 21 and ariLim is 200. The time limit is 5 seconds, and on my computer, it takes 22 seconds. ariprog.in is

21

200

Add TLE tag, and present the problem statement before the code
Source Link
Toby Speight
  • 87.3k
  • 14
  • 104
  • 322

The problem I'm solving is:

given a length of an arithmetic progression, and a limit to the terms (see below), find all progressions that work.

All terms of the progressions must be of the form a2+b2, and 0 ≤ a2+b2 ≤ limit.

I have the following code:

from math import ceil
with open('ariprog.in') as fin:
 ariLen = int(fin.readline().strip())
 ariLim = int(fin.readline().strip())
def generate(bound):
 max_len=((bound**2)*2)+1
 parity = [0]*max_len
 for i in range(bound+1):
 for j in range(bound+1):
 parity[i**2+j**2] = 1
 return parity
parity = generate(ariLim)
lenpar = len(parity)
big_mama_ar = []
# print(lenpar)
for a in range(lenpar-1):
 if parity[a] == 1:
 for d in range(1, ceil((lenpar-a)/(ariLen-1))):
 for n in range(1, ariLen):
 # print('a:', a)
 # print('d:', d)
 # print('n:', n)
 if parity[a+n*d] != 1:
 break
 else:
 big_mama_ar.append((a,d))
 pass
big_mama_ar.sort(key=lambda x: x[1])
with open('ariprog.out', 'w') as fout:
 if big_mama_ar == []:
 fout.write('NONE\n')
 else:
 for i in big_mama_ar:
 fout.write(str(i[0])+' '+str(i[1])+'\n')

However, This code times out on my grader when ariLen =ariLen is 21 and ariLim =ariLim is 200. The time limit is 5 seconds, and on my computer, it takes 22 seconds. The problem is as so: given a length of an arithmetic progression, and a limit to the terms(see below), find all progressions that work. All terms of the progressions must be of the form a^2+b^2, and 0<=a^2+b^2<=limit

I need help optimizing it

I have the following code:

from math import ceil
with open('ariprog.in') as fin:
 ariLen = int(fin.readline().strip())
 ariLim = int(fin.readline().strip())
def generate(bound):
 max_len=((bound**2)*2)+1
 parity = [0]*max_len
 for i in range(bound+1):
 for j in range(bound+1):
 parity[i**2+j**2] = 1
 return parity
parity = generate(ariLim)
lenpar = len(parity)
big_mama_ar = []
# print(lenpar)
for a in range(lenpar-1):
 if parity[a] == 1:
 for d in range(1, ceil((lenpar-a)/(ariLen-1))):
 for n in range(1, ariLen):
 # print('a:', a)
 # print('d:', d)
 # print('n:', n)
 if parity[a+n*d] != 1:
 break
 else:
 big_mama_ar.append((a,d))
 pass
big_mama_ar.sort(key=lambda x: x[1])
with open('ariprog.out', 'w') as fout:
 if big_mama_ar == []:
 fout.write('NONE\n')
 else:
 for i in big_mama_ar:
 fout.write(str(i[0])+' '+str(i[1])+'\n')

However, This code times out on my grader when ariLen = 21 and ariLim = 200. The time limit is 5 seconds, and on my computer, it takes 22 seconds. The problem is as so: given a length of an arithmetic progression, and a limit to the terms(see below), find all progressions that work. All terms of the progressions must be of the form a^2+b^2, and 0<=a^2+b^2<=limit

I need help optimizing it

The problem I'm solving is:

given a length of an arithmetic progression, and a limit to the terms (see below), find all progressions that work.

All terms of the progressions must be of the form a2+b2, and 0 ≤ a2+b2 ≤ limit.

I have the following code:

from math import ceil
with open('ariprog.in') as fin:
 ariLen = int(fin.readline().strip())
 ariLim = int(fin.readline().strip())
def generate(bound):
 max_len=((bound**2)*2)+1
 parity = [0]*max_len
 for i in range(bound+1):
 for j in range(bound+1):
 parity[i**2+j**2] = 1
 return parity
parity = generate(ariLim)
lenpar = len(parity)
big_mama_ar = []
# print(lenpar)
for a in range(lenpar-1):
 if parity[a] == 1:
 for d in range(1, ceil((lenpar-a)/(ariLen-1))):
 for n in range(1, ariLen):
 # print('a:', a)
 # print('d:', d)
 # print('n:', n)
 if parity[a+n*d] != 1:
 break
 else:
 big_mama_ar.append((a,d))
 pass
big_mama_ar.sort(key=lambda x: x[1])
with open('ariprog.out', 'w') as fout:
 if big_mama_ar == []:
 fout.write('NONE\n')
 else:
 for i in big_mama_ar:
 fout.write(str(i[0])+' '+str(i[1])+'\n')

This code times out on my grader when ariLen is 21 and ariLim is 200. The time limit is 5 seconds, and on my computer, it takes 22 seconds.

Source Link
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /