å¹³æ¹æ°ã®å¤å®ãé«éã«ãããã
ããæ°ãå¹³æ¹æ°ãã©ãã(æ´æ°ã2ä¹ããæ´æ°ãã©ãã)å¤å®ãã¦ãå¹³æ¹æ°ãªããã®å¹³æ¹æ ¹ãå¾ããã
ãããã大ããªæ°(ä¾ãã°ã3^256)ã§ãæ£ããå¤å®ã§ãããã¨ãæ¡ä»¶ã
ã¨ãã観ç¹ã§è©¦è¡é¯èª¤ããã®ã§ã¡ã¢ã
èããå¾ã§æã£ããã©PARI/GPã¨ãã«ãªãçµã¿è¾¼ã¿ã§è¶ é«éãªå¤å®ãå®è£ ããã¦ããã»ã»ã»
æ¹æ³1. ç´ æ´ãªå®è£
nã®å¹³æ¹æ ¹ã®æ´æ°é¨åãæµ®åå°æ°ç¹æ°ã§æ±ãã¦ã2ä¹ãããnã«ãªããã©ãã調ã¹ãã
- âç°¡å
- âéã
- Ã大ããªæ°ã«ã¯ä½¿ããªã(doubleã®ç²¾åº¦ã®åé¡)ãä¾ãã°17003146110244618067556674657992186608423729 ( 4123487129874982169223^2 ) ã®å¤å®ã«ã¯å¤±æãã
#! /usr/bin/python2.7 def try_square_root_naive(n): m = int(n**.5) return m if abs(m*m - n) < 1e-6 else None
æ¹æ³2. 2é²å10é²æ°ã使ã£ãä»»æ精度è¨ç®ã§æ¹æ³1ã¨åãäºããã
- âæµ®åå°æ°ç¹ã®ç²¾åº¦ãä¸ãããã¨ã§æ¹æ³1.ãã大ããªå¤ã¾ã§æ£ç¢ºã«å¤å®ã§ãã
- Ãã©ãã ãã®ç²¾åº¦ãã¨ãã¹ããããããªããæ°å¤ã®æ¡æ°*2åã®ç²¾åº¦ã§è¨ç®ãã¦ãããç¨åº¦æ°å¤ã大ãããªãã¨èª¤å¤å®ãèµ·ãã¦ãã¾ãããã
- Ãé«ã精度ã§è¨ç®ããã¨é常ã«é ãã
import decimal a = decimal.Decimal('.5') def try_square_root_decimal(n): decimal.getcontext().prec = len(str(n)) * 2 # é©å½ã«ç²¾åº¦ãè¨å® dn = decimal.Decimal(n) pdn = pow(dn, a) if pdn - int(pdn) < 1e-6: return pdn
2åæ¢ç´¢
[0,n]ã®ç¯å²ã§ãm*m = nã«ãªãmã2åæ¢ç´¢ã§è¦ã¤ããã
pythonæ¨æºã©ã¤ãã©ãªã®bisectã§2åæ¢ç´¢ãã§ããã®ã§ããã使ãã
- âæ°å¤ã大ãããªã£ã¦ã対æ°æéã§è¨ç®ã§ããã¯ã
- â大ããªæ°ãæ±ãã»ã»ã»ç¡ãï¼
â bisectã¯ã·ã¼ã±ã³ã¹ããå¤ãæ¢ç´¢ããç¨éãæ³å®ããã¦ãããããintãè¶
ããç¯å²ã®æ°å¤ã¯OverflowErrorãèµ·ããï¼ã£ã½ãï¼
(pythonã®å®è£
ä¸ã®åé¡ã)
class SquareRoot(object): def __init__(self, n): self.n = n def __getitem__(self, index): return index * index def __len__(self): return self.n def try_square_root(n2): n = bisect.bisect_left(SquareRoot(n2), n2) return n if n*n == n2 else None """ >>> try_square_root(144) # 12^2 12 >>> try_square_root(65536) # 256^2 256 >>> try_square_root(17003146110244618067556674657992186608423729) # 4123487129874982169223^2 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in try_square_root OverflowError: long int too large to convert to int """
2åæ¢ç´¢(æ¹è¯ç)
大ããªæ°ã§ã使ãã2åæ¢ç´¢ã
# 2åæ¢ç´¢ã§ä»¥ä¸ã®æ¡ä»¶ãæºããmãè¿ãã # è¦ã¤ãããªãå ´åã¯Noneãè¿ã # f(m) = n, m < [0, n] # 巨大ãªnã«ã対å¿ãããã¯bisectã§ã¯ã§ããªãã def bisearch(f, n): l = 0 r = n fl = f(l) fr = f(r) while l <= r: m = (l + r) // 2 fm = f(m) #print l,m,r,fl,fm,fr,n if fm == n: return m elif fm > n: r = m - 1 fr = f(r) else: l = m + 1 fl = f(l) def try_square_root(n2): return bisearch(lambda n:n*n, n2)
ãããªããããªã
- ä½è : Bill Karwin,åç°å人,åç°çäº,å 島修
- åºç社/ã¡ã¼ã«ã¼: ãªã©ã¤ãªã¼ã¸ã£ãã³
- çºå£²æ¥: 2013/01/26
- ã¡ãã£ã¢: 大åæ¬
- è³¼å ¥: 9人 ã¯ãªãã¯: 698å
- ãã®ååãå«ãããã° (46件) ãè¦ã