ProgrammingVery hardTCS NQT · Accenture
The Josephus game: n = 7 people stand in a circle numbered 1 to 7, and every third person is removed (counting continues around the circle, with the count starting at 1 for person 1). The procedure below returns the survivor. What is printed?
The Josephus game: n = 7 people stand in a circle numbered 1 to 7, and every third person is removed (counting continues around the circle, with the count starting at 1 for person 1). The procedure below returns the survivor. What is printed?
people = [1..7]; idx = 0
while more than one person remains
idx = (idx + 2) mod count
remove people[idx] (the next index now refers to the following person)
end while
Print the survivor- A. 4
- B. 7
- C. 3
- D. 2
Correct answer: A. 4
Eliminating every third person from seven gives survivor 4 (order removed: 3, 6, 2, 7, 5, 1). Skipping one instead of two gives 7, printing the first person removed gives 3, and stepping three gives 2.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The procedure below sums the prime factors of N = 360 counted with multiplicity (2 counted three times, and so on) by trial division. What…
- A product of ten consecutive numbers 1 x 2 x ... x 10 is computed. The procedure below counts how many of the numbers 1 to 10 are…
- Smallest n such that n! has at least 5 trailing zeros is searched for by the procedure below, where z(n) is the number of factors 5 in n!…
- A number N = 1296 is reduced by the procedure below, which divides by the smallest prime factor each time it can and keeps a count of the…
- The procedure below counts the numbers from 1 to 1000 whose digit sum is exactly 5. It uses digit-sum by repeated division. What is…
- The procedure below finds how many integers x with 1 <= x <= 200 satisfy x^2 mod 13 == 1. What is printed? count = 0 for x = 1 to 200 if…