def linear_sieve(n):
lpf = list(range(n + 1))
lpf[2::2] = [2] * (n // 2)
primes = []
for i in range(3, n + 1, 2):
if lpf[i] == i:
primes.append(i)
for p in primes:
if i * p > n or p > lpf[i]:
break
lpf[i * p] = p
return [2] + primes, lpf
def least_prime_factor(n):
lpf = list(range(n + 1))
lpf[2::2] = [2] * (n // 2)
primes = []
for i in range(3, n // 3 + 1, 2):
if lpf[i] == i:
primes.append(i)
for p in primes:
if i * p > n or p > lpf[i]:
break
lpf[i * p] = p
return lpf