Question 31
MediumWhat is the time complexity of Euclid's GCD algorithm for a and b?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the time complexity of Euclid's GCD algorithm for a and b?
O(log min(a, b))
O(√a)
O(a × b)
O(min(a, b))
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
n, f, out = 60, 2, []
while f * f <= n:
while n % f == 0:
out.append(f)
n //= f
f += 1
if n > 1:
out.append(n)
print(out)
[4, 3, 5]
[2, 3, 5]
Sign up free to see the answer and a step-by-step explanation.
How many trailing zeros does 100! have?
10
24
25
20
Sign up free to see the answer and a step-by-step explanation.
Why do programming problems often ask for answers 'modulo 10⁹ + 7'?
It is the fastest number to divide by
It makes answers smaller to print
It is a large prime that keeps numbers within 64-bit range and allows modular inverses
It is required by every language
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def sieve(n):
p = [True] * (n + 1)
p[0] = p[1] = False
for i in range(2, int(n ** 0.5) + 1):
if p[i]:
for j in range(i * i, n + 1, i):
p[j] = False
return [i for i in range(n + 1) if p[i]]
r = sieve(50)
print(len(r), r[-1])
15 47
16 50
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def power(a, b, m):
r = 1
a %= m
while b:
if b & 1:
r = r * a % m
a = a * a % m
b >>= 1
return r
print(power(3, 200, 13))
9
1
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def egcd(a, b):
if b == 0:
return a, 1, 0
g, x, y = egcd(b, a % b)
return g, y, x - (a // b) * y
g, x, y = egcd(30, 12)
print(g, 30 * x + 12 * y)
6 0
3 6
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def factors(n):
f, d = [], 2
while d * d <= n:
while n % d == 0:
f.append(d); n //= d
d += 1
if n > 1:
f.append(n)
return f
print(factors(360), factors(97))
[2, 2, 2, 3, 3, 5] [97]
[8, 9, 5] [97]
Sign up free to see the answer and a step-by-step explanation.
What is the LCM of 12, 18 and 30?
180
90
60
360
Sign up free to see the answer and a step-by-step explanation.
What is the smallest n for which n! has at least 6 trailing zeros?
30
25
20
24
Sign up free to see the answer and a step-by-step explanation.
[60]
[2, 2, 3, 5]
14 47
15 49
12
3
6 6
12 6
[2, 3, 5] [97]
[2, 2, 2, 3, 3, 5] []