Problem p02574 (python)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))

def prime_decomposition(n):
  i = 2
  d = {}
  while i * i <= n:
    while n % i == 0:
      n //= i
      if i not in d:
          d[i] = 0
      d[i] += 1
    i += 1
  if n > 1:
    if n not in d:
        d[n] = 1
  return d

A.sort()
M = A[-1]
d = {}
ans = 0

for i in range(N):
    if A[i] not in d:
        primes = prime_decomposition(A[i])
        for p in primes:
            for j in range(1,M//p+1):
                d[j*p] = 0
    else:
        ans = 1
        break
if ans == 0:
    print('pairwise coprime')
else:
    def gcd(a, b):
    	while b:
    		a, b = b, a % b
    	return a
    a = A[0]
    for i in range(N):
        a = gcd(a,A[i])
    if a==1:
        print('setwise coprime')
    else:
        print('not coprime')

Buggy submission (verdict: Runtime Error):
N = int(input())

if N<=10**5:
    def eratosthenes(n):
        prime = []
        limit = n**0.5
        numbers = [i for i in range(2,n+1)]
        while True:
            p = numbers[0]
            if limit <= p:
                return prime + numbers
            prime.append(p)
            numbers = [i for i in numbers if i%p != 0]
        return prime

    def prime_decomposition(n):
      i = 2
      d = {}
      while i * i <= n:
        while n % i == 0:
          n //= i
          if i not in d:
              d[i] = 0
          d[i] += 1
        i += 1
      if n > 1:
        if n not in d:
            d[n] = 1
      return d

    pl = eratosthenes(10**6)
    pd = {}
    for p in pl:
        pd[p] = 0

    A = list(map(int,input().split()))

    A.sort()



    d = {}
    ans = 0
    for i in range(N):
        if A[i] not in pd:
            primes = prime_decomposition(A[i])
            for p in primes:
                if p not in d:
                    d[p] = 0
                else:
                    ans = 1
                    break
        else:
            if A[i] in d:
                ans = 1
                break
            d[A[i]] = 0

    if ans == 0:
        print('pairwise coprime')
    else:
        def gcd(a, b):
        	while b:
        		a, b = b, a % b
        	return a
        a = A[0]
        for i in range(N):
            a = gcd(a,A[i])
        if a==1:
            print('setwise coprime')
        else:
            print('not coprime')
else:
    def eratosthenes(n):
        prime = []
        limit = n**0.5
        numbers = [i for i in range(2,n+1)]
        while True:
            p = numbers[0]
            if limit <= p:
                return prime + numbers
            prime.append(p)
            numbers = [i for i in numbers if i%p != 0]
        return prime

    def prime_decomposition(n):
      i = 2
      d = {}
      while i * i <= n:
        while n % i == 0:
          n //= i
          if i not in d:
              d[i] = 0
          d[i] += 1
        i += 1
      if n > 1:
        if n not in d:
            d[n] = 1
      return d

    pl = eratosthenes(10**6)
    pd = {}
    for p in pl:
        pd[p] = 0

    N = int(input())
    A = list(map(int,input().split()))


    A.sort()

    d = {}
    ans = 0
    if len(set(A)) != N:
        ans = 1
    for i in range(N):
        if A[i] in d:
            ans = 1
            break
        if A[i] not in pd:
            primes = prime_decomposition(A[i])
            for p in primes:
                if p not in d:
                    for j in range(1,(10**6)//p+1):
                        d[j*p] = 0
                else:
                    ans = 1
                    break




    if ans == 0:
        print('pairwise coprime')
    else:
        def gcd(a, b):
        	while b:
        		a, b = b, a % b
        	return a
        a = A[0]
        for i in range(N):
            a = gcd(a,A[i])
        if a==1:
            print('setwise coprime')
        else:
            print('not coprime')

Buggy submission (verdict: Runtime Error):
def eratosthenes(n):
    prime = []
    limit = n**0.5
    numbers = [i for i in range(2,n+1)]
    while True:
        p = numbers[0]
        if limit <= p:
            return prime + numbers
        prime.append(p)
        numbers = [i for i in numbers if i%p != 0]
    return prime

def eratosthenes(n):
    prime = []
    limit = n**0.5
    numbers = [i for i in range(2,n+1)]
    while True:
        p = numbers[0]
        if limit <= p:
            return prime + numbers
        prime.append(p)
        numbers = [i for i in numbers if i%p != 0]
    return prime

pl = eratosthenes(10**6)
pd = {}
for p in pl:
    pd[p] = 0

N = int(input())
A = list(map(int,input().split()))

A.sort()
d = {}
ans = 0
for i in range(N):
    if A[i] not in pd:
        primes = prime_decomposition(A[i])
        for p in primes:
            if p not in d:
                d[p] = 0
            else:
                ans = 1

if ans == 0:
    print('pairwise coprime')
else:
    def gcd(a, b):
    	while b:
    		a, b = b, a % b
    	return a
    a = A[0]
    for i in range(N):
        a = gcd(a,A[i])
    if a==1:
        print('setwise coprime')
    else:
        print('not coprime')