練習問題/解答例/平方根/Python
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
***平方根 [#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
↓分数を使ったもの
#!/usr/bin/python
# -*- coding: utf-8 -*-
from fractions import Fraction
from decimal import Decimal, getcontext
from operator import truediv
from functools import reduce
getcontext().prec = 50
ZERO = Fraction('0/1')
ONE = Fraction('1/1')
HALF = Fraction('1/2')
def my_sqrt(num):
'''
ニュートン法で平方根を求める。
'''
if not 0 < num <= 10000:
return 'Not implemented...'
p = Fraction(num).limit_denominator()
x0 = ZERO
x1 = HALF * (ONE + p)
for _ in range(12):
x0 = x1
x1 = HALF * (x0 + p / x0)
x = str(x1)
if '/' in x:
return reduce(truediv, map(Decimal, x.split('/')))
else:
return '{0:30}'.format(x)
print(my_sqrt(0))
print(my_sqrt(1))
print(my_sqrt(2))
print(my_sqrt(3))
print(my_sqrt(10))
print(my_sqrt(100))
print(my_sqrt(0.0001))
print(my_sqrt(10000))
終了行:
***平方根 [#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
↓分数を使ったもの
#!/usr/bin/python
# -*- coding: utf-8 -*-
from fractions import Fraction
from decimal import Decimal, getcontext
from operator import truediv
from functools import reduce
getcontext().prec = 50
ZERO = Fraction('0/1')
ONE = Fraction('1/1')
HALF = Fraction('1/2')
def my_sqrt(num):
'''
ニュートン法で平方根を求める。
'''
if not 0 < num <= 10000:
return 'Not implemented...'
p = Fraction(num).limit_denominator()
x0 = ZERO
x1 = HALF * (ONE + p)
for _ in range(12):
x0 = x1
x1 = HALF * (x0 + p / x0)
x = str(x1)
if '/' in x:
return reduce(truediv, map(Decimal, x.split('/')))
else:
return '{0:30}'.format(x)
print(my_sqrt(0))
print(my_sqrt(1))
print(my_sqrt(2))
print(my_sqrt(3))
print(my_sqrt(10))
print(my_sqrt(100))
print(my_sqrt(0.0001))
print(my_sqrt(10000))
ページ名: