from math import sqrt def perfsq(n): # Returns mylist of perfect squares less than or equal to n mylist = [1] i, x = 0, 3 while mylist[-1]+x <= n: mylist.append(mylist[i]+x) # Add x to get next perfect square i += 1 x = 2*i+3 return mylist def redsqrt(n): # Prints most reduced form of square root of n if n < 0: print('Negative input') return if sqrt(n).is_integer(): print(int(sqrt(n))) # Square root is an integer return # Find perfect squares that are factors of n l = [i for i in perfsq(n/2) if n%i==0 and i>1] if len(l)==0: print('radical',n) # Square root is irreducible else: a = int(sqrt(max(l))) # Coefficient b = int(n/max(l)) # Argument of the square root print(a,'radical',b) # Reduced square root redsqrt(3*308*4)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question