

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: NP
incomplete
2: Traveling Salesman Problem
incomplete
3: Verify TSP
incomplete
4: TSP Review
incomplete
5: NP-Complete
incomplete
6: Verifying Solutions
incomplete
7: Does P Equal NP?
incomplete
8: The Negative Case
incomplete
9: NP-Hard
incomplete
10: Prime Factorization
incomplete
11: Prime Factoring Review
incomplete
12: Prime Factoring Review
incomplete
13: Subset Sum Problem
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Let us denote n as the integer input, and s as the size of n in bits. s = log2(n)
Notice that our first loop iterates log(n) times and the second loop iterates sqrt(n) times. The Big O with respect to n is O(sqrt(n))! That's fast! That's polynomial complexity which would lead us to believe the problem is in P
The problem is that, by definition, when computer scientists talk about this problem, they are talking about the length of n in bits. What we will call s. For example, the integer 255 only takes 8 bits.
241 = 11110001 in binary
Since s = log2(n), we know that 2^s = n. Therefore, a complexity of O(sqrt(n)) is equivalent to O(sqrt(2^s))
The complexity in respect to the number of bits is exponential.
def prime_factors(n: int) -> list[int]:
prime_factors = []
while n % 2 == 0:
n /= 2
prime_factors.append(2)
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
n /= i
prime_factors.append(i)
if n > 2:
prime_factors.append(int(n))
return prime_factors