Problem p02660 (python)

Fixed submission:
N = int(input())

# import math
#
# def sieve_of_eratosthenes(n):
#     candidate = [i for i in range(2, n+1)]
#     prime = []
#     limit = math.sqrt(n) + 1
#
#     while True:
#         p = min(candidate)
#         if limit <= p:
#             prime.extend(candidate)
#             break
#         prime.append(p)
#
#         candidate = [i for i in candidate if i % p != 0]
#
#     return prime
#
#
# if N == 1:
#     print(0)
#     exit()
#
# rn = math.ceil(math.sqrt(N))
# L = sieve_of_eratosthenes(rn)
# print('hoge')
#
# n = N
# c = 0
# used = {}
#
# for l in L:
#     if n % l == 0:
#         i = 1
#         l2 = l
#         while n % l2 == 0:
#             c += 1
#             i += 1
#             used[l2] = True
#             n = n // l2
#             l2 = l ** i
#
# if c == 0:
#     print(1)
# else:
#     if n != 1:
#         # x = sieve_of_eratosthenes(n)
#         if n not in used:
#             c += 1
#     print(c)

# if N == 1:
#     print(0)
#     exit()
#
# ans = 0
# used = {}
#
# n = N
# i = 1
# p = 2
# while n % p == 0:
#     ans += 1
#     n = n // p
#     used[p] = True
#     i += 1
#     p = 2**i
#
#
# i = 1
# p = 3
# k = p
# while p * p <= N:
#     while n % k == 0:
#         if k in used:
#             i += 1
#             k = k ** i
#         else:
#             ans += 1
#             i += 1
#             n = n // k
#             used[k] = True
#             k = k ** i
#     i = 1
#     p += 2
#     k = p
#
# if n not in used and n != 1:
#     ans += 1
# print(ans)

def factorize(n):
    a = []
    # 2で割り切れるまでnを割り続ける
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    # 3からはじめて各奇数で割り切れるまでnを割り続ける
    # nの最大約数は√nなので、f*fで計算
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

fs = factorize(N)

from collections import Counter
c = Counter(fs)

ans = 0
i = 1
for k, v in c.items():
    k2, v2 = k,v
    while v2 >= i:
        ans += 1
        v2 -= i
        i += 1
    i = 1

print(ans)

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

import math

def sieve_of_eratosthenes(n):
    candidate = [i for i in range(2, n+1)]
    prime = []
    limit = math.sqrt(n) + 1

    while True:
        p = min(candidate)
        if limit <= p:
            prime.extend(candidate)
            break
        prime.append(p)

        candidate = [i for i in candidate if i % p != 0]

    return prime


if N == 1:
    print(0)
    exit()

rn = math.ceil(math.sqrt(N))
L = sieve_of_eratosthenes(rn)

n = N
c = 0
used = []

for l in L:
    if n % l == 0:
        i = 1
        l2 = l
        while n % l2 == 0:
            c += 1
            i += 1
            used.append(l2)
            n = n // l2
            l2 = l ** i

if c == 0:
    print(1)
else:
    if n != 1:
        x = sieve_of_eratosthenes(n)
        if n == x[-1] and n not in used:
            c += 1
    print(c)

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

import math

def sieve_of_eratosthenes(n):
    candidate = [i for i in range(2, n+1)]
    prime = []
    limit = math.sqrt(n) + 1

    while True:
        p = min(candidate)
        if limit <= p:
            prime.extend(candidate)
            break
        prime.append(p)

        candidate = [i for i in candidate if i % p != 0]

    return prime


if N == 1:
    print(0)
    exit()

rn = math.ceil(math.sqrt(N))
L = sieve_of_eratosthenes(rn)

n = N
c = 0
used = []

for l in L:
    if n % l == 0:
        i = 1
        l2 = l
        while n % l2 == 0:
            c += 1
            i += 1
            used.append(l2)
            n = n // l2
            l2 = l ** i

if c == 0:
    print(1)
else:
    if n != 1:
        x = sieve_of_eratosthenes(n)
        if n in x and not n in used:
            c += 1
    print(c)