forked from kal179/Beginners_Python_Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsquare_root_algorithm.py
More file actions
38 lines (34 loc) · 768 Bytes
/
square_root_algorithm.py
File metadata and controls
38 lines (34 loc) · 768 Bytes
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
""" Function uses prime factorisation method
to find square root of number """
def prime_factors(c):
pos = 2
factors = []
while (pos <= c):
if (c % pos == 0):
c = c // pos
factors.append(pos)
continue
else:
pos = pos + 1
return factors
def extract_common(li):
final = []
if (len(li) % 2 != 0):
return "Number is not perfect root."
else:
pre = len(li) - 1
for n in range(0, pre, 2):
a = li[n]
b = li[n + 1]
if (a == b):
final.append(b)
else:
return "Number is not perfect root."
return final
def square_root(take_in):
res = 1
for c in take_in:
res *= c
return res
get_num = int(raw_input("\nNumber : "))
print square_root(extract_common(prime_factors(get_num))), " "