自然言語処理第1回の解答と第2回に向けての資料

1.5 
問1
>>> sum([923746, 736427, 783644, 103847, 348582])
2896246

問2
>>> (3*(2+5)-1)*2
40

>>> 17/3*2
10

>>> 3*(2+3)**3
375

>>> 1+2**3
9

>>> -2**4+1
-15

問3
a = 7636272
b = 93838
>>> a+b
7730110
>>> a-b
7542434
>>> a/b
81
>>> a%b
35394
>>> float(a)/b
81.3771819518745

問4
x = 3.278 ;
y = 73.2 ;
>>> x*y
239.9496
>>> print("%e"%(x*y))
2.399496e+02

有効数字が高々3桁なので
	 2.40 * 10^2
と書くのが妥当

参考
>>> 3.2775*73.15
239.74912500000002
>>> 3.2785 * 73.25
240.150125

問5

for s in range(1,11):
  print "7**",s,"=",7**s," and the residual by 11 = ", 7**s % 11

  
7** 1 = 7  and the residual by 11 =  7
7** 2 = 49  and the residual by 11 =  5
7** 3 = 343  and the residual by 11 =  2
7** 4 = 2401  and the residual by 11 =  3
7** 5 = 16807  and the residual by 11 =  10
7** 6 = 117649  and the residual by 11 =  4
7** 7 = 823543  and the residual by 11 =  6
7** 8 = 5764801  and the residual by 11 =  9
7** 9 = 40353607  and the residual by 11 =  8
7** 10 = 282475249  and the residual by 11 =  1


for z in range(2,100):
  w = 2**z
  print 2,"**",z-1," = ",w/2," and residual by ",z, " = ",(w/2)%z

  
2 ** 1  =  2  and residual by  2  =  0
2 ** 2  =  4  and residual by  3  =  1
2 ** 3  =  8  and residual by  4  =  0
2 ** 4  =  16  and residual by  5  =  1
2 ** 5  =  32  and residual by  6  =  2
2 ** 6  =  64  and residual by  7  =  1
2 ** 7  =  128  and residual by  8  =  0
2 ** 8  =  256  and residual by  9  =  4
2 ** 9  =  512  and residual by  10  =  2
2 ** 10  =  1024  and residual by  11  =  1
2 ** 11  =  2048  and residual by  12  =  8
2 ** 12  =  4096  and residual by  13  =  1
2 ** 13  =  8192  and residual by  14  =  2
2 ** 14  =  16384  and residual by  15  =  4
2 ** 15  =  32768  and residual by  16  =  0
2 ** 16  =  65536  and residual by  17  =  1
2 ** 17  =  131072  and residual by  18  =  14
2 ** 18  =  262144  and residual by  19  =  1
2 ** 19  =  524288  and residual by  20  =  8
2 ** 20  =  1048576  and residual by  21  =  4
2 ** 21  =  2097152  and residual by  22  =  2
2 ** 22  =  4194304  and residual by  23  =  1
2 ** 23  =  8388608  and residual by  24  =  8
2 ** 24  =  16777216  and residual by  25  =  16
2 ** 25  =  33554432  and residual by  26  =  2
2 ** 26  =  67108864  and residual by  27  =  13
2 ** 27  =  134217728  and residual by  28  =  8
2 ** 28  =  268435456  and residual by  29  =  1
2 ** 29  =  536870912  and residual by  30  =  2
2 ** 30  =  1073741824  and residual by  31  =  1
2 ** 31  =  2147483648  and residual by  32  =  0
2 ** 32  =  4294967296  and residual by  33  =  4
2 ** 33  =  8589934592  and residual by  34  =  2
2 ** 34  =  17179869184  and residual by  35  =  9
2 ** 35  =  34359738368  and residual by  36  =  32
2 ** 36  =  68719476736  and residual by  37  =  1
2 ** 37  =  137438953472  and residual by  38  =  2
2 ** 38  =  274877906944  and residual by  39  =  4
2 ** 39  =  549755813888  and residual by  40  =  8
2 ** 40  =  1099511627776  and residual by  41  =  1
2 ** 41  =  2199023255552  and residual by  42  =  32
2 ** 42  =  4398046511104  and residual by  43  =  1
2 ** 43  =  8796093022208  and residual by  44  =  8
2 ** 44  =  17592186044416  and residual by  45  =  31
2 ** 45  =  35184372088832  and residual by  46  =  2
2 ** 46  =  70368744177664  and residual by  47  =  1
2 ** 47  =  140737488355328  and residual by  48  =  32
2 ** 48  =  281474976710656  and residual by  49  =  15
2 ** 49  =  562949953421312  and residual by  50  =  12
2 ** 50  =  1125899906842624  and residual by  51  =  4
2 ** 51  =  2251799813685248  and residual by  52  =  8
2 ** 52  =  4503599627370496  and residual by  53  =  1
2 ** 53  =  9007199254740992  and residual by  54  =  14
2 ** 54  =  18014398509481984  and residual by  55  =  49
2 ** 55  =  36028797018963968  and residual by  56  =  16
2 ** 56  =  72057594037927936  and residual by  57  =  4
2 ** 57  =  144115188075855872  and residual by  58  =  2
2 ** 58  =  288230376151711744  and residual by  59  =  1
2 ** 59  =  576460752303423488  and residual by  60  =  8
2 ** 60  =  1152921504606846976  and residual by  61  =  1
2 ** 61  =  2305843009213693952  and residual by  62  =  2
2 ** 62  =  4611686018427387904  and residual by  63  =  4
2 ** 63  =  9223372036854775808  and residual by  64  =  0
2 ** 64  =  18446744073709551616  and residual by  65  =  16
2 ** 65  =  36893488147419103232  and residual by  66  =  32
2 ** 66  =  73786976294838206464  and residual by  67  =  1
2 ** 67  =  147573952589676412928  and residual by  68  =  8
2 ** 68  =  295147905179352825856  and residual by  69  =  4
2 ** 69  =  590295810358705651712  and residual by  70  =  22
2 ** 70  =  1180591620717411303424  and residual by  71  =  1
2 ** 71  =  2361183241434822606848  and residual by  72  =  32
2 ** 72  =  4722366482869645213696  and residual by  73  =  1
2 ** 73  =  9444732965739290427392  and residual by  74  =  2
2 ** 74  =  18889465931478580854784  and residual by  75  =  34
2 ** 75  =  37778931862957161709568  and residual by  76  =  8
2 ** 76  =  75557863725914323419136  and residual by  77  =  9
2 ** 77  =  151115727451828646838272  and residual by  78  =  32
2 ** 78  =  302231454903657293676544  and residual by  79  =  1
2 ** 79  =  604462909807314587353088  and residual by  80  =  48
2 ** 80  =  1208925819614629174706176  and residual by  81  =  40
2 ** 81  =  2417851639229258349412352  and residual by  82  =  2
2 ** 82  =  4835703278458516698824704  and residual by  83  =  1
2 ** 83  =  9671406556917033397649408  and residual by  84  =  32
2 ** 84  =  19342813113834066795298816  and residual by  85  =  16
2 ** 85  =  38685626227668133590597632  and residual by  86  =  2
2 ** 86  =  77371252455336267181195264  and residual by  87  =  4
2 ** 87  =  154742504910672534362390528  and residual by  88  =  40
2 ** 88  =  309485009821345068724781056  and residual by  89  =  1
2 ** 89  =  618970019642690137449562112  and residual by  90  =  32
2 ** 90  =  1237940039285380274899124224  and residual by  91  =  64
2 ** 91  =  2475880078570760549798248448  and residual by  92  =  8
2 ** 92  =  4951760157141521099596496896  and residual by  93  =  4
2 ** 93  =  9903520314283042199192993792  and residual by  94  =  2
2 ** 94  =  19807040628566084398385987584  and residual by  95  =  54
2 ** 95  =  39614081257132168796771975168  and residual by  96  =  32
2 ** 96  =  79228162514264337593543950336  and residual by  97  =  1
2 ** 97  =  158456325028528675187087900672  and residual by  98  =  58
2 ** 98  =  316912650057057350374175801344  and residual by  99  =  58
 
for z in range(2,100):
  w = 2**(z-1)
  u = w % z
  if (u == 1):
    print 2,"**",z," % 2 = 1"

    
2 ** 3  % 2 = 1
2 ** 5  % 2 = 1
2 ** 7  % 2 = 1
2 ** 11  % 2 = 1
2 ** 13  % 2 = 1
2 ** 17  % 2 = 1
2 ** 19  % 2 = 1
2 ** 23  % 2 = 1
2 ** 29  % 2 = 1
2 ** 31  % 2 = 1
2 ** 37  % 2 = 1
2 ** 41  % 2 = 1
2 ** 43  % 2 = 1
2 ** 47  % 2 = 1
2 ** 53  % 2 = 1
2 ** 59  % 2 = 1
2 ** 61  % 2 = 1
2 ** 67  % 2 = 1
2 ** 71  % 2 = 1
2 ** 73  % 2 = 1
2 ** 79  % 2 = 1
2 ** 83  % 2 = 1
2 ** 89  % 2 = 1
2 ** 97  % 2 = 1
>>> 

注: Fermatの小定理は、 xとmが互いに素であることを要求している

for z in range(8,100):         # 1000までの範囲で探す
  w = 2**(z-1)%z; u = 3**(z-1)%z; v = 7**(z-1)%z
  kw = 1; ku = 1; kv = 1
  if (w == 1): kw = 0
  if (u == 1): ku = 0
  if (v == 1): kv = 0
  k = ku+kw+kv
  if (k >0) and (k < 3):	# すべてが1でなければ素数でないはず
    print w, u, v, "\t",z

16 6 1   25
64 1 77   91
56 1 23   121
252 1 21   286
66 131 1   325
1 56 56   341
1 375 1   561
1 36 436   645

あきらかに645は素数でない。また,561も3の倍数、341は11の倍数。

========================================================

2.3 節

譜2.1 プログラム1.  cal.py

wday=("sun","mon","tue","wed","thu","fri","sat")

def cal0(n,m):
  # cal0(n,m) は1 つの月のカレンダーを作成する。
  # n は月の開始日の曜日を表す数字(0 から6) であり、
  # 0 は日曜日を意味する。
  # m はその月の日数である。例えば1 月はm=31 である。
  # 従って2000 年1 月のカレンダーはcal0(6,31) で表示される。
  for x in wday: print " ",x,
  print
  for x in range(0, n): print "  ---",
  for x in range(1, m+1):
    print "%5d"%x,
    if (x+n)%7==0 : print
  print


譜3.5 7 の倍数で改行するプログラム
for x in range(1,32):
  print x,
  if x%7 == 0:
     print
     print


譜3.7 cal0(n,m) の定義
def cal0(n,m):
  for x in ("sun","mon","tue","wed","thu","fri","sat"):
    print "%5s"%x,
    print
  for x in range(0,n):
    print "%5s"%"---",
  for x in range(1,m+1):
    print "%5d"%n,
    if (n+x)%7 == 0:
      print
      print

cal0(4,31)


譜3.10 Zellerの公式を使ったカレンダープログラム

def h(y,m,d):
  # h(y,m,d): day of week (Zeller's formula)
  # 奥村晴彦「コンピュータアルゴリズム辞典」(技術評論社,1989)
  # y: year, 0,1,2,..
  # m: month, 1,2,..,12
  # d: day of month, 1,2,..31
  # return: day of week, 0,1,2,..,6
  # example: h(2000,1,1) -> 6 # Saturday
  if m<3: y=y-1; m=m+12
  return (y+y/4-y/100+y/400+(13*m+8)/5+d)%7

def cal0(n,m):
  # cal0(n,m)は1つの月のカレンダーを作成する。
  # nは月の開始日の曜日を表す数字(0,1,..,6)であり、0が日曜日を意味する
  # mはその月の日数である。例えば1月はm=31である
  #従って2000年1月のカレンダーはcal0(6,31)で印刷される
  for x in ("sun","mon","tue","wed","thu","fri","sat"): print " ",x,
  print
  for x in range(0, n): print "  ---",
  for x in range(1, m+1):
    print "%5d"%x,
    if (x+n)%7==0 : print
  print

def cal(y,m):
  # y: year, 0,1,2,..
  # m: month, 1,2,..,12
  n=h(y,m,1)
  n1=h(y,m+1,1)
  r=(n1-n)%7
  d=28+r
  cal0(n,d)

cal(2000,1)