Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: What is the largest prime factor of 2^20 - 1? Run the code.
Problem: What is the largest prime factor of 2^20 - 1? Run the code.
n = 2 ** 20 - 1
f = 2
last = 1
while f * f <= n:
while n % f == 0:
last = f
n //= f
f += 1
if n > 1:
last = n
print(last)What is printed?- A. 31
- B. 5
- C. 41
- D. 1048575
Correct answer: C. 41
2²⁰ − 1 = 3 · 5² · 11 · 31 · 41. Trial division stops when f² exceeds the reduced n, so forgetting the final if n > 1 step returns 31; 5 is the repeated factor, and 1048575 is n itself.
More Coding Interview Problems questions
- Problem: How many numbers in [1, 500] are not divisible by 2, 3 or 5? Run the code. python print(sum(1 for x in range(1, 501) if x % 2 and…
- Problem: What is the number of inversions in the array [1, 3, 2, 5, 4, 6]? Run the code. python a = [1, 3, 2, 5, 4, 6] print(sum(1 for i…
- Problem: What is the maximum sum of a subarray with at most one element deleted from [1, -2, 3, -4, 5]? Run the code. python a = [1, -2…
- Problem: How many three-letter words over {a, b, c} contain every letter at least once? Run the code. python from itertools import product…
- Problem: What is the length of the longest subsequence of [3, 1, 4, 1, 5, 9, 2, 6] whose elements are strictly increasing? Run the code…
- Problem: What is the number of trailing zeros in 30!? Run the code. python n = 30 c = 0 p = 5 while p <= n: c += n // p p = 5 print(c)…