The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+9 votes

Choose the correct alternatives (more than one may be correct) and write the corresponding letters only:

Consider the following Pascal function:

Function X(M:integer):integer;
Var i:integer;
    i := 0;
    while i*i < M
    do i:= i+1
    X := i

The function call $X(N)$, if $N$ is positive, will return

  1. $\lfloor\sqrt N \rfloor$
  2. $\lfloor\sqrt N \rfloor +1$
  3. $\lceil \sqrt N \rceil$
  4. $\lceil \sqrt N \rceil +1$
  5. None of the above
asked in Algorithms by Veteran (68.8k points) | 542 views
Correct option should be either (B) or (C).
Option (B) is incorrect for the perfect squares.
if we consider option (B), for 4 answer will be 3, for 25 answer will be 6.

While option(C) gives correct answer always.

2 Answers

+9 votes
Best answer

For $N=9$, it returns $3$.

For $N=10$ it returns $4$.

For $N=16$ it returns $4$.

For $N=17$ it returns $5$.

So answer should be C.

answered by Loyal (4.2k points)
edited by
I think answer is B.

check for n=1 answer is 2
I think it returns 1.
yes i missed it and assumed it to be greater than and equal to.

What is the difference between ceil(sqrt(N)) and floor(sqrt(N)) + 1 that is (B) and (C) ?

N=10 it returns 4.

sqrt(10) = 3.16

floor(3.16) + 1 = 4 (B) as well as ceil(3.16) = 4 (C)

I'm confused.

ceil(X) will return x+1 when x is a decimal and not integer.

floor(x)+1 will add 1 even in case of integers, hence for N=4 it will give answer as 2+1=3. So wrong choice

so u mean ceil(4) will return 5???

obviously ceil(sqrt()) will return 4 because ceil( will return 4. so what's wrong have i said
I misinterpreted.. Thanks, point cleared and noted! :D
+2 votes
The function returns $\lfloor \sqrt{N} \rfloor +1$. For $17$ it returns $5$, and for $16$ also it returns $5$.
answered by Veteran (35.9k points)
It will return integer part of sqrt(N)  I don't whether [sqrtN]  mean same
It should be for floor. Typo in question.
For N=9, it returns 3.

So it should Option C I think
for 16, it will return 4 not 5

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

32,620 questions
39,267 answers
36,656 users