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

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;
Begin
    i := 0;
    while i*i < M
    do i:= i+1
    X := i
 end

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 (59.4k points) | 600 views
0
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 Active (3.5k points)
edited by
0
I think answer is B.

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

Thanks
0
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.
0

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

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

obviously ceil(sqrt()) will return 4 because ceil(3.xxx) will return 4. so what's wrong have i said
+1
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 Boss (34.2k points)
0
It will return integer part of sqrt(N)  I don't whether [sqrtN]  mean same
0
It should be for floor. Typo in question.
+1
For N=9, it returns 3.

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

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

34,775 questions
41,742 answers
118,910 comments
41,388 users