• 検索結果がありません。

Set 型の応用 —– 四則演算で 10 を作る遊び

ドキュメント内 if if-else if-elif-else (ページ 57-62)

(b⊛c)a : b, cを先に計算してから次にaとの演算を行う。

のように3つの計算の順番があります。

上のことに注意して3つの数a,b,cに対する2項演算から作られる数の集合を返す関数sankou(a,b,c) を定義してみましょう。

ファイル名:sankou1.py

1 def n i k o u ( a , b ):

2 if a !=0 and b ! = 0 :

3 r e t u r n set([ a + b , a - b , b - a , a * b , 1 . 0 * a / b , 1 . 0 * b / a ])

4 e l i f b == 0:

5 r e t u r n set([ a , - a , 0 ] )

6 e l s e:

7 r e t u r n set([ b , -b , 0])

8

9 def s a n k o u ( a , b , c ): # a , b , cの 四 則 演 算 で 作 ら れ る 集 合 を 返 す 10 R e s u l t S e t = set( [ ] ) # R e s u l t S e tを 空 の 集 合 と す る

11 for i in n i k o u ( a , b ): # a , bか ら 作 ら れ る 数iに 対 し て

12 R e s u l t S e t |= n i k o u ( i , c ) # i , cの 二 項 演 算 か ら 作 ら れ る 集 合 をR e s u l t S e tに 追 加 13 for i in n i k o u ( b , c ):

14 R e s u l t S e t |= n i k o u ( i , a ) 15 for i in n i k o u ( a , c ):

16 R e s u l t S e t |= n i k o u ( i , b )

17 r e t u r n R e s u l t S e t # R e s u l t S e tを 返 す 18

19 p r i n t s a n k o u (4 ,4 ,9) # 4 ,4 ,9を 1 回 ず つ 使 っ た 四 則 演 算 で 作 ら れ る 集 合

20 p r i n t ’ ’ # 改 行

21 p r i n t 10 in s a n k o u (4 ,4 ,9) # s a n k o u (4 ,4 ,9)に は1 0は 入 っ て い る か ?

実行結果の例 set([-1.25000000000000, 3.25000000000000, 1.25000000000000, 1,

8.00000000000000, 9, 10.0000000000000, -3.55555555555556, 144, 17, 20, 25, -0.800000000000000, 0.562500000000000, 0, 32, 0.888888888888889, 40, 7, -1.75000000000000, 52, 1.75000000000000, 1.12500000000000,

4.44444444444444, 72, 0.307692307692308, -7, -32, 0.800000000000000, 3.55555555555556, -20, 0.111111111111111, -9, -8.00000000000000, 1.77777777777778, -1, 6.25000000000000])

True

さて,これで3つの数字の組から10を作れるかどうかを判定する事が可能になりました。そこで三つの数 字0≤a≤b≤c≤9で四則演算によって10を作ることができるものをすべて列挙してみましょう:

ファイル名:sankou2.py

1 # * c o d i n g : utf 8 * -2 def n i k o u ( a , b ):

3 if a !=0 and b ! = 0 :

4 r e t u r n set([ a + b , a - b , b - a , a * b , 1 . 0 * a / b , 1 . 0 * b / a ])

5 e l i f b == 0:

6 r e t u r n set([ a , - a , 0 ] )

7 e l s e:

8 r e t u r n set([ b , -b , 0])

9

10 def s a n k o u ( a , b , c ): # a , b , cの 四 則 演 算 で 作 ら れ る 集 合 を 返 す 11 R e s u l t S e t = set( [ ] ) # R e s u l t S e tを 空 の 集 合 と す る

12 for i in n i k o u ( a , b ): # a , bか ら 作 ら れ る 数iに 対 し て

13 R e s u l t S e t |= n i k o u ( i , c ) # i , cの 二 項 演 算 で で き る 集 合 をR e s u l t S e tに 追 加

22.2 Set型の応用—–四則演算で10を作る遊び 22 SET

14 for i in n i k o u ( b , c ):

15 R e s u l t S e t |= n i k o u ( i , a ) 16 for i in n i k o u ( a , c ):

17 R e s u l t S e t |= n i k o u ( i , b )

18 r e t u r n R e s u l t S e t # R e s u l t S e tを 返 す 19

20 n u m b e r = 0 # n u m b e rと い う 変 数 を 作 り0に セ ッ ト す る 21

22 for i in r a n g e( 1 0 ) : 23 for j in r a n g e( i , 1 0 ) : 24 for k in r a n g e( j , 1 0 ) :

25 if 10 in s a n k o u ( i , j , k ): # も し1 0s a n k o u ( i , j , k )の 要 素 な ら

26 p r i n t i , j , k # i , j , kを 表 示 し て

27 n u m b e r = n u m b e r +1 # n u m b e r1増 や す

28

29 p r i n t ’ 10を 作 れ る 数 字 の 三 つ 組 み の 数 は’, number , ’

実行結果の例 0 1 9

0 2 5 ...

...

9 9 9

10を作れる数字の三つ組みの数は 75

22.2.3 4つの数字の四則演算

つぎに4つの数a,b,c,dから作れる数の集合を返す関数を作りたい。そのような演算には次の2通りある。

(A) 3つの数の演算を行った後に,残りの数との演算を行う場合

(B) 2つの数の計算をそれぞれ行って,次にそれらの演算を行う場合

例えば

(a+d*c)*b (a*d)+(b/c) の前者は(A)の場合で,後者が(B)の場合である。

(A)の手順で作られる数字の集合を返す関数をyonkouA(a,b,c,d)とすると,これは次のように定義すれ ばよい

1 def y o n k o u A ( a , b , c , d ):

2 R e s u l t S e t = set( [ ] )

3 for i in s a n k o u ( a , b , c ): # a , b , cか ら 作 ら れ る 数iに 対 し て

4 R e s u l t S e t |= n i k o u ( i , d ) # idの 二 項 演 算 か ら 作 ら れ る 数 をR e s u l t S e tに 追 加 5 for i in s a n k o u ( a , b , d ):

6 R e s u l t S e t |= n i k o u ( i , c ) 7 for i in s a n k o u ( a , c , d ):

8 R e s u l t S e t |= n i k o u ( i , b ) 9 for i in s a n k o u ( b , c , d ):

10 R e s u l t S e t |= n i k o u ( i , a ) 11 r e t u r n R e s u l t S e t

(A)の演算を用いて10を作ることが出来る4つの数をすべて列挙するプログラムは次のようになる。

ファイル名:yonkou1.py

1 # * c o d i n g : utf 8 *

-2 def n i k o u ( a , b ):

3 if a !=0 and b ! = 0 :

4 r e t u r n set([ a + b , a - b , b - a , a * b , 1 . 0 * a / b , 1 . 0 * b / a ])

5 el i f b == 0:

6 r e t u r n set([ a , - a , 0 ] )

7 el s e:

8 r e t u r n set([ b , -b , 0])

9

10 def s a n k o u ( a , b , c ): # a , b , cの 四 則 演 算 で 作 ら れ る 集 合 を 返 す 11 R e s u l t S e t = set( [ ] ) # R e s u l t S e tを 空 の リ ス ト と す る

12 for i in n i k o u ( a , b ): # a , bか ら 作 ら れ る 数iに 対 し て

13 R e s u l t S e t |= n i k o u ( i , c ) # i , cの 演 算 か ら 作 ら れ る 集 合 をR e s u l t S e tに 追 加 14 for i in n i k o u ( b , c ):

15 R e s u l t S e t |= n i k o u ( i , a ) 16 for i in n i k o u ( a , c ):

17 R e s u l t S e t |= n i k o u ( i , b )

18 r e t u r n R e s u l t S e t # R e s u l t S e tを 返 す 19

20 def y o n k o u A ( a , b , c , d ):

21 R e s u l t S e t = set( [ ] )

22 for i in s a n k o u ( a , b , c ): # a , b , cか ら 作 ら れ る 数iに 対 し て

23 R e s u l t S e t |= n i k o u ( i , d ) # idの 演 算 か ら 作 ら れ る 数 をR e s u l t S e tに 追 加 24 for i in s a n k o u ( a , b , d ):

25 R e s u l t S e t |= n i k o u ( i , c ) 26 for i in s a n k o u ( a , c , d ):

27 R e s u l t S e t |= n i k o u ( i , b ) 28 for i in s a n k o u ( b , c , d ):

29 R e s u l t S e t |= n i k o u ( i , a ) 30 r e t u r n R e s u l t S e t

31

32 n u m b e r = 0 33

34 for i in r a n g e( 1 0 ) : 35 for j in r a n g e( i , 1 0 ) : 36 for k in r a n g e( j , 1 0 ) :

37 for l in r a n g e( k , 1 0 ) :

38 if 10 in y o n k o u A ( i , j , k , l ):

39 p r i n t i , j , k , l

40 n u m b e r = n u m b e r +1

41

42 p r i n t ’ 10を 作 れ る 数 字 の 三 つ 組 み の 数 は’, number , ’

実行結果の例 0 0 1 9

0 0 2 5 ...

...

8 9 9 9 9 9 9 9

10を作れる数字の4つ組みの数は 545

さて,これで(A)の演算で10を作ることが出来る数字の4つ組みを確定することが出来た。しかし上の実行 結果には手順(B)で10を作ることができる数字が含まれていない。例えば1114は(1+1)*(1+4)=10のよ うに10を作ることができるが上の実行結果にはない。

22.2 Set型の応用—–四則演算で10を作る遊び 22 SET

そこで次に,計算(B)の計算で作られる数の集合を返す関数yonkouB(a,b,c,d)を定義し,4つの数字か ら四則演算で作られるリストをすべて列挙し,その個数を求めたい。yonkouB(a,b,c,d)は次のように定義 すればよい:

1 def y o n k o u B ( a , b , c , d ):

2 R e s u l t S e t = set( [ ] ) # R e s u l t S e tを 空 の リ ス ト と す る

3 for i in n i k o u ( a , b ): # abか ら 作 ら れ る 数 をiと す る

4 for j in n i k o u ( c , d ): # cdか ら 作 ら れ る 数 をjと す る

5 R e s u l t S e t |= n i k o u ( i , j ) # ijか ら 作 ら れ る 数 の 集 合 をR e s u l t S e tに 加 え る 6 for i in n i k o u ( a , c ):

7 for j in n i k o u ( b , d ):

8 R e s u l t S e t |= n i k o u ( i , j ) 9 for i in n i k o u ( a , d ):

10 for j in n i k o u ( b , c ):

11 R e s u l t S e t |= n i k o u ( i , j )

12 r e t u r n R e s u l t S e t # R e s u l t S e tを 返 す

結局,0から9までをとる4つの数字a, b, c, da≤b≤c≤dかつこれらの四則演算で10を作ることが できる組をすべて列挙するプログラムを作成するには次のようにすればよい:

ファイル名:make10.py

1 # * c o d i n g : utf 8 * -2 def n i k o u ( a , b ):

3 if a !=0 and b ! = 0 :

4 r e t u r n set([ a + b , a - b , b - a , a * b , 1 . 0 * a / b , 1 . 0 * b / a ])

5 el i f b == 0:

6 r e t u r n set([ a , - a , 0 ] )

7 el s e:

8 r e t u r n set([ b , -b , 0])

9

10 def s a n k o u ( a , b , c ): # a , b , cの 四 則 演 算 で 作 ら れ る 集 合 を 返 す 11 R e s u l t S e t = set( [ ] ) # R e s u l t S e tを 空 の リ ス ト と す る

12 for i in n i k o u ( a , b ): # a , bか ら 作 ら れ る 数iに 対 し て

13 R e s u l t S e t |= n i k o u ( i , c ) # i , cの 二 項 演 算 か ら 作 ら れ る 集 合 をR e s u l t S e tに 追 加 14 for i in n i k o u ( b , c ):

15 R e s u l t S e t |= n i k o u ( i , a ) 16 for i in n i k o u ( a , c ):

17 R e s u l t S e t |= n i k o u ( i , b )

18 r e t u r n R e s u l t S e t # R e s u l t S e tを 返 す 19

20 def y o n k o u A ( a , b , c , d ):

21 R e s u l t S e t = set( [ ] )

22 for i in s a n k o u ( a , b , c ): # a , b , cか ら 作 ら れ る 数iに 対 し て

23 R e s u l t S e t |= n i k o u ( i , d ) # idの 二 項 演 算 か ら 作 ら れ る 数 をR e s u l t S e tに 追 加 24 for i in s a n k o u ( a , b , d ):

25 R e s u l t S e t |= n i k o u ( i , c ) 26 for i in s a n k o u ( a , c , d ):

27 R e s u l t S e t |= n i k o u ( i , b ) 28 for i in s a n k o u ( b , c , d ):

29 R e s u l t S e t |= n i k o u ( i , a ) 30 r e t u r n R e s u l t S e t

31

32 def y o n k o u B ( a , b , c , d ):

33 R e s u l t S e t = set( [ ] ) # R e s u l t S e tを 空 の リ ス ト と す る

34 for i in n i k o u ( a , b ): # abか ら 作 ら れ る 数 をiと す る

35 for j in n i k o u ( c , d ): # cdか ら 作 ら れ る 数 をjと す る

36 R e s u l t S e t |= n i k o u ( i , j ) # ijか ら 作 ら れ る 数 の 集 合 をR e s u l t S e tに 加 え る 37 for i in n i k o u ( a , c ):

38 for j in n i k o u ( b , d ):

39 R e s u l t S e t |= n i k o u ( i , j ) 40 for i in n i k o u ( a , d ):

41 for j in n i k o u ( b , c ):

42 R e s u l t S e t |= n i k o u ( i , j )

43 r e t u r n R e s u l t S e t # R e s u l t S e tを 返 す

44

45 def y o n k o u ( a , b , c , d ):

46 R e s u l t S e t = set( [ ] )

47 R e s u l t S e t |= y o n k o u A ( a , b , c , d ) 48 R e s u l t S e t |= y o n k o u B ( a , b , c , d ) 49 r e t u r n R e s u l t S e t

50

51 c o u n t e r = 0 52

53 for i in r a n g e( 1 0 ) : 54 for j in r a n g e( i , 1 0 ) : 55 for k in r a n g e( j , 1 0 ) :

56 for l in r a n g e( k , 1 0 ) :

57 if 10 in y o n k o u ( i , j , k , l ):

58 p r i n t i , j , k , l

59 c o u n t e r = c o u n t e r +1

60

61 p r i n t ’ 10を 作 れ る 数 字 の4つ 組 み の 数 は’, counter , ’

実行結果の例 ...

...

...

8 8 9 9 9 9 9 9

10を作ることができる4つの数字の組の数は 552 個

上のプログラムは結果的にはうまく動いていますが,本来なら桁落ちに注意が必要です。それは,二項演算を 浮動小数点で行っているために,演算の結果が正確には10のはずなのに,誤差により10ではなくなっている 可能性があるためです。ただ,10に非常に近い数は10であると認識されます:

1 > > > 0 . 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 == 1

2 T r u e

3 > > > 0 . 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 == 1

4 F a l s e

ドキュメント内 if if-else if-elif-else (ページ 57-62)