The Gateway to Computer Science Excellence
+10 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
in Algorithms by | 1.1k 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

+14 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.

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


line is important ,if it will be. $\leq$ ns will be B)

+2 votes
The function returns $\lfloor \sqrt{N} \rfloor +1$. For $17$ it returns $5$, and for $16$ also it returns $5$.
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
No, suppose N = 16, condition will become false for i=4 as 4*4 is equal to 16, so the function will return 4, but according to your answer it'll be 5. only one option is correct (C)

Related questions

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
52,345 questions
60,497 answers
95,318 users