ProgrammingVery hardCAT · MBA placement
Let phi(n) be the number of integers i with 1 <= i <= n that share no common factor with n except 1. The procedure below computes phi(36) by testing every i with gcd(i, n) = 1. What is printed?
Let phi(n) be the number of integers i with 1 <= i <= n that share no common factor with n except 1. The procedure below computes phi(36) by testing every i with gcd(i, n) = 1. What is printed?
count = 0
for i = 1 to n
if gcd(i, n) == 1 then count = count + 1
end for
Print count- A. 9
- B. 11
- C. 12
- D. 24
Correct answer: C. 12
The integers below 36 coprime to it are those not divisible by 2 or 3, giving 12. Counting divisors of 36 gives 9, starting the range at 2 misses the value 1 and gives 11, and counting the non-coprime integers gives 24.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Starting from n = 27, the Collatz step is applied: if n is even, n becomes n/2, otherwise n becomes 3n + 1. The procedure below counts the…
- Four numbers 4, 6, 10 and 15 are combined pairwise: the lcm of a and b is computed as ab div gcd(a, b), and the lcm of three numbers is…
- Count the integers n with 1 <= n <= 100 that are divisible by 3 or by 5, but NOT by both. The procedure below does this with a single…
- For N = 100 the procedure below counts how many times 5 divides the factorials, that is, the number of trailing zeros of 100!. The count…
- The procedure below reverses the digits of N = 120450 (trailing zeros disappear in the numeric result). What is printed? rev = 0; m = N…
- An Armstrong number of three digits equals the sum of the cubes of its digits. The procedure below counts how many three-digit numbers…