Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalpak Take authored Feb 11, 2017
1 parent 17e129e commit 5a047f1
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions square_root_algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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))), " "

0 comments on commit 5a047f1

Please sign in to comment.