-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
73 lines (63 loc) · 2.63 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# This script computes and returns the square root of integer A as
# floor(sqrt(A)) where 1<=A<=10^9
#
# This script is a part of the Easy Python project which creates a number
# sample python scripts to answer simple programming questions. The
# entire project is accessible at https://github.com/okany/easypython.
# Copyright (c) 2021 Okan Yilmaz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
def sqrt(num):
# shift num + 1 to cover num = 1 case as well
start = (num+1) >> 1
# find the number to start the search
while start * start > num:
# print("start = {}".format(start))
start = start >> 1
# start is our default solution
test = start
end = (start << 1) - 1
# print ("start = {} end = {}".format(start, end))
while end > start:
# print ("start = {} end = {}".format(start, end))
# now do binary search between start and end
test = ((end - start) >> 1) + start + 1
sqr = test * test
if sqr > num:
# set end and test to test -1 as test cannot be a solution
test = end = test-1
elif sqr < num:
# set start to test as test can be a solution
start = test
else:
# bingo - we found the number
break
return test
if __name__=="__main__":
num = 12340050
print("TEST#1 - square root of {} is {}".format(num, sqrt(num)))
num = 10 ** 9 # this is the max in the problem definition
print("TEST#2 - square root of {} is {}".format(num, sqrt(num)))
# test 3 consecutive numbers to verify the sensitivity of the sort function
num = 999950884
print("TEST#3 - square root of {} is {}".format(num, sqrt(num)))
num = 999950883
print("TEST#4 - square root of {} is {}".format(num, sqrt(num)))
num = 999950885
print("TEST#5 - square root of {} is {}".format(num, sqrt(num)))
num = 1 # this is an important corner case to cover
print("TEST#6 - square root of {} is {}".format(num, sqrt(num)))
num = 2
print("TEST#7 - square root of {} is {}".format(num, sqrt(num)))