# python code for creating the percent error graph import math #check if a number n is a perfect square def perfect_sq(n): return n == int(math.sqrt(n)) * int(math.sqrt(n)) def closest_ps(n): # distance from perfect square that is greater than n p = 0. while perfect_sq(n+p) == False: p = p + 1 # distance from perfect square that is less than n m = 0. while perfect_sq(n-m) == False: m = m + 1 # n is closer to a perfect square larger than n if m <= p: d = n-m #here d is the closest perfect square to our n #apply binomial theorem where x = m/d return math.sqrt(d)*(1+.5*(m/d)) # or n is closer to a perfect square less than n else: d = n+p #apply binomial theorem where x = -m/d return math.sqrt(d)*(1-.5*(p/d)) i = 15 while (i < 1000): # calculate percent error using the error formula. i**.5 = i^.5 print (abs(closest_ps(i) - i**.5)/(i**.5))*100 i = i + 1
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