***平方根 [#h1b5f416]

 def my_square(n):
	x = 1.0
	while 1:
		if x * x >= n:
			x=(x * x + (x - 1) * (x - 1)) / 2
			for i in range(0,10):
				j = n / x
				x = (j + x) / 2
			print '%.30f' % x
			break
		else:
			x = x + 1

↑小数点下30桁まで計算できる

↓誤差が出ないバージョン。小数点以下100桁まで。
 UNTIL = 100
 
 def f(n):
     xn = 0
     while xn * xn < n:
         xn += 1
     return len(str(xn))
 
 def g(s, p):
     return s[:p] + '.' + s[p:]
 
 def sq(n):
     a, b = 5*n, 5
     res = ''
     sz = 0
     cnt = 0
     while sz <= UNTIL:
         if a >= b:
             a, b = a-b, b+10
             cnt += 1
         else:
             a, b = a*100, (b/10)*100 + b%10
             res += str(cnt)
             sz += 1
             cnt = 0
     return g(res, f(n))
 
 print sq(2)
 print sq(10000)

# 初心者が書いたもの
 # coding:utf-8

 
 c = raw_input( "平方根を求めたい数値を入力:  " )
 x = int( c )
 a = float(0)
 t = 1
 n = 0
 while t > a:
	 k = float(10**n)
	 n += 1
	 while a**2 > x:
		 a = a - 1/k
	 while a**2 < x:
		 a = a +1/k
	 t = a + 1
	 print "+-" + str(a)
	 if a**2 == x:
		 break



トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS