GATE / CS ARCHIVE

GATE CSE 1991 / QUESTION 3(viii)

Integer square-root loop

Algorithms · Correctness, Invariants & Design · Full GATE CSE 1991 paper

GATE 1991, Q3(viii) 2 marks · MSQ
Report issue

Correctness, Invariants & Design

Question 3(viii), GATE 1991: GFG initializes i=0 and repeats i:=i+1 while i*i≤N, then returns i. Forum instead prints i*i<N. Determine the return for positive N.
Question 3(viii), GATE 1991: GFG initializes i=0 and repeats i:=i+1 while i*i≤N, then returns i. Forum instead prints i*i<N. Determine the return for positive N.
Question 3(viii), GATE 1991: GFG initializes i=0 and repeats i:=i+1 while i*i≤N, then returns i. Forum instead prints i*i<N. Determine the return for positive N.
Tap to enlarge
Select all that apply
Answer & solution

Answer: B: floor(√N)+1 (no official key was published)

For the non-strict the guard, the loop exits at the smallest integer i whose square exceeds N. That is floor(√N)+1, including a perfect square N=k², which returns k+1. For nonsquares both expressions coincide. For example, N=4 distinguishes 3 from 2. The source change therefore matters and is not a harmless formatting variation. This arithmetic analysis assumes i*i and the increment do not overflow the Pascal integer range.

Original paper, page 3