Ȼ/07

Top / Ȼ / 07

βˤĤ

βϰʲΤ褦ʤΤǤä

ȥ٥ȥѡȹѤ׻ؿץߥ

  1. Τ߰ȤƤ
  2. ΰ㤤ΥåϤʤƤ褤(󤷤Ƥ褤)
  3. (Ը) inner_product ؿѤƼ

ơ4 x 4 ιŬġ4٥ȥŬ˰ѰդơФѤη׻ޤȤǧ补

warning.png ʸϤɤ¤ ̤ n x n 򰷤ʤȤʤ (4 x 4 ϤޤǤưȤγǧ) ΤǡǰƬ˥ץ񤤤Ƥ
warning.png 顤ƤʤΤ 4 x 4 ѤΥץϼ Ǥ롥ǰɤ͡


ι¤ͤ

ĤΤ褦ι¤ͤ롥
ŪʵǽȤƹƱΤѤȹȥ٥ȥѤĤɬפǡȤƤǥ󥹥ȥ졼󤹤ʸȤȤˤʤΤǡޤϰʲΤ褦ˤʤ ǽ餫 4 x 4 򰷤ΤݤʤΤǡޤ 2 x 2 ʤɤΡְ־פǻ褦˽񤤤Ƥ

include Math

# ƱΤ: Ĥ
def mtx_product_mtx(m_a, m_b)
  # ϤϹ󡥡֤Ȥꤢư褦ˡ׺ǽΰ֤Ƥ
  return m_a
end

# ȥ٥ȥ: ȥ٥ȥ뤬
def mtx_product_vec(m_a, v_x)
  # Ϥϥ٥ȥ롥֤Ȥꤢư褦ˡװ٥ȥ֤Ƥ
  return v_x
end

# ʹѤ˽Ϥ
def print_mtx(m_a)
end

# ٥ȥʹѤ˽Ϥ
def print_vec(v_x)
end

# ʲץ

# ץ 2 x 2 ѥǡ
a = [[1,2],
     [3,4]]

b = [[5,6],
     [7,8]]

x = [10,11]

# ƱΤѤ׻
c = mtx_product_mtx(a,b)

# ȥ٥ȥѤ׻
y = mtx_product_vec(a,x)

# ̤˽
print_mtx(c)
print_vec(y)


ǽ䤷Ƥ: ̤򤹤åǤ褦ˡɽǽ˼Ƥߤ

βϹźΥʤɤΤ˴ְ㤤ȯ䤹Ǿ¤ޤ뤿ˤɽǽ˼ȳڤ褽ǡprint_ ؿ˼褦
ޤϡ٥ȥɽդΤñ㤨аʲΤ褦ˤʤ

def print_vec(v_x)
  print("[ ")
  for i in 1..v_x.size do
    print(v_x[i-1]," ")
  end
  print("]\n")
end

δؿǤСɽˤϤѤΤʿʹ֤λ׹ͤȤΤʲΤ褦ˤʤ

def print_mtx(m_a)
  for i in 1..m_a.size do
    print_vec(m_a[i-1])
  end
end

Ǥ print_vec ؿȤ print_mtx ؿǤϤۤȤ٥μ򤷤ʤɤ

warning.pngΤ褦ˡִ˥ץषƤ뤳ȤѤƥץहפȤ˽פǤ롥 ʤʤ顤

  1. Ʊ褦ʤȤ٤񤫤ʤɤΤǡߥΨ
  2. ˡս꤬ɬ׺Ǿ¤ǺѤ
  3. ץ٤夬ꡤ꤭ȤΤˤʤǽޤ

Ǥ(Don't Repeat Yourself = DRY §˶ᤤ)ޤñ˸С

ץǤƱνʣƽ񤯤Ȥϡְ

Ȼפɤ


ǽ䤷Ƥ: ȥ٥ȥ

˴ñʤΤϡȥ٥ȥѤμѤȤ褦ˤдñ
줿Υ٥ȥΡ֤ҤʤפѰդˡ狼ʤȤ⤢顤ؿȤơ֤Ȥꤢǹͤפ褦ˤСΤ褦ˤʤ
warning.png ʹ֤ǾߤϤٰʾʣнǤʤΤǡΤ褦ˡָǹͤʬʬΥǽϡפ˽פޤǿȤˤĤ褦
⶯ĴƤ

񤷤ʬϴؿˤɤФơָǹͤפ٤

ǡʣʬ򤹤뤫ץߥ󥰤Ρ֥פǤ롥

# ٥ȥѡΥԡ
def inner_product(v_a, v_b)
  sum = 0.0
  for i in 1..v_a.size do
    sum += v_a[i-1]*v_b[i-1]
  end
  return sum
end

# n ٥ȥ
def make_vec(n)
  # ɤ?
end

# ȥ٥ȥ: ȥ٥ȥ뤬
def mtx_product_vec(m_a, v_x)
  v_y = make_vec(m_a.size)

  for i in 1..m_a.size do
    v_y[i-1] = inner_product(m_a[i-1], v_x)
  end

  return v_y
end

ơ줿Υ٥ȥΡ֤ҤʤפѰդؿ make_vec ϤޤǤμʤаʲΤ褦ˤʤ

# n ٥ȥ
def make_vec(n)
  # ޤǤҤȤĤʤֶפ롥
  a = []
  #  0.0  nɲä롥
  n.times { a.push(0.0) }
  # 
  return a
end

warning.png (Ը) Ruby εǽ򤭤ȻȤäƽ񤯤ʤ

# n ٥ȥ
def make_vec(n)
  return Array.new(n){0.0}
end

Ȥˤʤ

ơ뤳Ȥǹȥ٥ȥѤϤȷ׻ǤϤǤ뤫顤ºݤưƤߤȡ

[ 1 2 ]

[ 3 4 ]

[ 32.0 74.0 ]

ȤϤ졤Τ 2 x 2 ȥ٥ȥѤ׻Ƥ뤳ȤǧǤ롥


ǽ䤷Ƥ: ȹ

ơǸϹȹѤǤ롥⡤٥ȥνޤȻפ񤷤ʤ
Ȥϡ󤫤ԥ٥ȥ롤٥ȥȴФ򤷤äԤСѤȤ롥
顤㤨мΤ褦ˤʤ

# ٥ȥȴФ
def col_vec(m_a, k)
  v_y = make_vec(m_a.size)
  # ŪʵǽϤȤǼ
  return v_y
end

# n x m 
def make_mtx(n,m)
  # ɤ?
end

# ƱΤ: Ĥ
def mtx_product_mtx(m_a, m_b)
  m_c = make_mtx(m_a.size,m_a.size)

  # ѤȤСȤϿؤ̤ꡥ
  for i in 1..m_a.size do
    for j in 1..m_a.size do
      m_c[i-1][j-1] = inner_product(m_a[i-1],col_vec(m_b,j))
    end
  end

  return m_c
end

ǡɬפˤʤäĤδؿεǽ롥
ޤϡֹ󤫤٥ȥȴФ׵ǽϴñǡΤ褦ˤɤ

def col_vec(m_a, k)
  v_y = make_vec(m_a.size)

  for i in 1..m_a.size do
    v_y[i-1] = m_a[i-1][k-1]
  end

  return v_y
end

ĤäֹΤҤʤ׵ǽϡ٥ȥ뵡ǽȤä

  1. # n x m
  2. def make_mtx(n,m)
  3.   a = []
  4.   n.times { a.push( make_vec(m) ) }
  5.   return a
  6. end

ȤФ褤ñϼˤƤ뵤ʬͭ뤫⤷ʤɤ⡥
warning.png (Ը) Ruby εǽ򤭤ȻȤäƽ񤯤ʤ

# n x m 
def make_mtx(n,m)
  return Array.new(n){ Array.new(m){0.0} }
end

Ȥˤʤ

ơǤεǽⴰưƤߤȡ

[ 19.0 22.0 ]

[ 43.0 50.0 ]

[ 32.0 74.0 ]

ȤʤꡤΤưƤ뤳Ȥʬ롥


礭ѹ: ǽʳ

ơưƤ褦˸Τǡؼ줿 4 x 4 ˼괹褦
ޤϤ⾯ɤߤ䤹
ȤȤǡ㤨аʲΤ褦ˤʤ

  1. include Math
  2.  
  3. # ٥ȥѡΥԡ
  4. def inner_product(v_a, v_b)
  5.   sum = 0.0
  6.   for i in 1..v_a.size do
  7.     sum += v_a[i-1]*v_b[i-1]
  8.   end
  9.   return sum
  10. end
  11.  
  12. # n ٥ȥ
  13. def make_vec(n)
  14.   a = []
  15.   n.times { a.push(0.0) }
  16.   return a
  17. end
  18.  
  19. # n x m
  20. def make_mtx(n,m)
  21.   a = []
  22.   n.times { a.push( make_vec(m) ) }
  23.   return a
  24. end
  25.  
  26. # ٥ȥȴФ
  27. def col_vec(m_a, k)
  28.   v_y = make_vec(m_a.size)
  29.  
  30.   for i in 1..m_a.size do
  31.     v_y[i-1] = m_a[i-1][k-1]
  32.   end
  33.  
  34.   return v_y
  35. end
  36.  
  37. # ƱΤ: Ĥ
  38. def mtx_product_mtx(m_a, m_b)
  39.   m_c = make_mtx(m_a.size, m_a.size)
  40.  
  41.   for i in 1..m_a.size do
  42.     for j in 1..m_a.size do
  43.       m_c[i-1][j-1] = inner_product(m_a[i-1],col_vec(m_b,j))
  44.     end
  45.   end
  46.  
  47.   return m_c
  48. end
  49.  
  50. # ȥ٥ȥ: ȥ٥ȥ뤬
  51. def mtx_product_vec(m_a, v_x)
  52.   v_y = make_vec(m_a.size)
  53.  
  54.   for i in 1..m_a.size do
  55.     v_y[i-1] = inner_product(m_a[i-1], v_x)
  56.   end
  57.  
  58.   return v_y
  59. end
  60.  
  61. # ʹѤ˽Ϥ
  62. def print_mtx(m_a)
  63.   for i in 1..m_a.size do
  64.     print_vec(m_a[i-1])
  65.   end
  66. end
  67.  
  68. # ٥ȥʹѤ˽Ϥ
  69. def print_vec(v_x)
  70.   print("[ ")
  71.   for i in 1..v_x.size do
  72.     print(v_x[i-1]," ")
  73.   end
  74.   print("]\n")
  75. end
  76.  
  77. # ʲץ
  78.  
  79. a = [[1,2,3,4],
  80.      [5,6,7,8],
  81.      [9,10,11,12],
  82.      [13,14,15,16]]
  83.  
  84. b = [[17,18,19,20],
  85.      [21,22,23,24],
  86.      [25,26,27,28],
  87.      [29,30,31,32]]
  88.  
  89. x = [3,4,5,6]
  90.  
  91. # ƱΤѤ׻
  92. c = mtx_product_mtx(a,b)
  93.  
  94. # ȥ٥ȥѤ׻
  95. y = mtx_product_vec(a,x)
  96.  
  97. # ʲ̤˽
  98. print("A = \n")
  99. print_mtx(a)
  100. print("\n")
  101.  
  102. print("B = \n")
  103. print_mtx(b)
  104. print("\n")
  105.  
  106. print("A B = \n")
  107. print_mtx(c)
  108. print("\n")
  109.  
  110. print("x = \n")
  111. print_vec(x)
  112. print("\n")
  113.  
  114. print("A x = \n")
  115. print_vec(y)

ơưȼΤ褦ˤʤ롥
׻ʤɤǡΤ̤Ǥ뤳ȤΤ롥

A =

[ 1 2 3 4 ]

[ 5 6 7 8 ]

[ 9 10 11 12 ]

[ 13 14 15 16 ]


B =

[ 17 18 19 20 ]

[ 21 22 23 24 ]

[ 25 26 27 28 ]

[ 29 30 31 32 ]


A B =

[ 250.0 260.0 270.0 280.0 ]

[ 618.0 644.0 670.0 696.0 ]

[ 986.0 1028.0 1070.0 1112.0 ]

[ 1354.0 1412.0 1470.0 1528.0 ]


x =

[ 3 4 5 6 ]

 

A x =

[ 50.0 122.0 194.0 266.0 ]

warning.png ץĹǽΩƤǺ񤷤Ȥϲ̵
β꤬񤷤Ȼפäͤϡι¤ͤˤʤȤʤץߥ󥰤褦ȤΤǤϤʤΤ褦ʳϡֻפĤʤäפȯ뤳Ȥ¿ϰʲˤƤ⤽ְͤäƤ롥

  1. ʣʤΤФн򤤤ʤֻפĤפ̤ۤοʹ֤ǾߤɤƤʤˤؤ餺ʬϤǤȻפǤȤ̣ Ǥ.
  2. ץϡоݤʬ򤷤Ƶǽ򵭽Ҥ򷫤̡֤פʤΤˤηֻ̤פĤפȤߤʤΤϡhow ܤְäƤȤ̣ǡ֤⤬ߤäƤΤԤĤȤΤƱפ

Ϥ餯ʣʬ򤹤뤳Ȥܼ򤷤ƤʤפȤΤȤϷڻ뤷ƤϤʤʤʤȤʤΤɸŪ礭񤤤Ƥ

ץߥ󥰤Ǥϥƥ˥ʵǽܤå줬
षɤʬ򤹤뤫פϤ٤Ǥ롥

warning.png 񤷤äסֻפĤʤäפȥݡȤ˽񤤤ͤϡʬ˲­ʤΤ⤦٤äͤƤߤ褦


ץƱȤ򷫤֤ʤ

˽񤤤Ʊͤνʤɤ񤫤ˡִ˥ץषƤ뤳ȤѤƥץहפȤϽפǤ롥
warning.png ºݤˡΥץǤϡǤݤ­Ƥפսˤ̵Ȥܤ褦ơʬν񤤤ץǤϤս˽񤫤Ƥ뤫åƤߤ褦


郎Ƥ֤ϥ롼פ : while ̿

郎Ƥ֤ϥ롼פȤؤܤ
ޤʸˡ񤤤Ƥ

while () do
        ξ郎줿¹Ԥ
        
        # ޤ褿顤⤦پäƾ郎å
end

warning.png for if Ȥ߹碌ƵŪ˼¸뤳ȤǤ.

notes.png while ̿Ȥäơ1 + 2 + 3 + ... Ȥ׻Ļꤵ줿Ķ뤫Ĵ٤ץ񤤤Ƥߤ褦вϤ褦ˤ褦


ξϡ㤨мΤ褦ʥץˤʤ

  1. include Math
  2.  
  3. # Ķ뤫ɤĴ٤ϵưˤ餦
  4. n = ARGV[0].to_i
  5.  
  6. # פνͤϤ󥼥
  7. sum = 0
  8.  
  9. # ­Ƥ ǽ 1 ʳǤ sum ФƤϤޤ 0 ­ƤʤΤǡ
  10. # ˤƤΤŪ
  11. i = 0
  12.  
  13. # n ĶƤʤ while Ȥ¹Ԥ졤Ƥ n ĶƤʤåĤ򷫤֤
  14. while (sum <= n) do
  15.   print("i = ", i, ", sum = ", sum, " <= ", n, "\n") 
  16.   # ˽. ʳʤμ¤ sum <= n.
  17.   # while Ǥϡ̤ФȽǤϤϾåΤȤ˹ԤΤڡ
  18.  
  19.   i += 1     # i 1䤹
  20.   sum += i   # i פ롥
  21. end
  22.  
  23. # i 䤷Ƥ i sum ­!
  24. # sum ­Ƥ i 䤹ȡǸˤĤĤޤʤʤ补
  25. # 褯ʬʤͤϡsum ­Ƥ i 䤹褦ˤƥץưƤߤ褦.
  26.  
  27. # Ǹ˷̤ϡ
  28. print("i = ", i, ", sum = ", sum, " > ", n, "\n")

 ruby -w ./over.rb 100

ʤɤȤƵưȡ

i = 0, sum = 0 <= 100

i = 1, sum = 1 <= 100

i = 2, sum = 3 <= 100

i = 3, sum = 6 <= 100

i = 4, sum = 10 <= 100

i = 5, sum = 15 <= 100

i = 6, sum = 21 <= 100

i = 7, sum = 28 <= 100

i = 8, sum = 36 <= 100

i = 9, sum = 45 <= 100

i = 10, sum = 55 <= 100

i = 11, sum = 66 <= 100

i = 12, sum = 78 <= 100

i = 13, sum = 91 <= 100

i = 14, sum = 105 > 100

Ȥ褦˷̤롥ɤʬǤǤ



warning.png ɤͤϤ⤦ŤƤwhile ܼ̿񤤤Ƥȡ

while = æȽĤ̵¥롼פΰ

Ǥ롥


Fizz Buzz ĩ

Fizz Buzz ȤϸͷӤΰǡ1, 2,... Ƚ˿Ҥ٤Ƥˡ

  • 3 dzڤȤϤο Fizz ȸ
  • 5 dzڤȤϤο Buzz ȸ
  • 3 Ǥ 5 ǤڤȤ̤ˤο Fizz Buzz ȸ

ȤΤǤ롥ǡʲΤ褦˥ץष褦

  • ץ൯ưˤ礭ʿ n (100 餤?) Ϳ롥
  • 1,2,... ȿΥ롼ǽϤƤäơFizz, Buzz, Fizz Buzz νиιפ n ˤʤäߤ롥

Ūˤϡ㤨

 ruby -w fb.rb 10

Ȥ 10ĤΡפФƤޤǤϤȡ

1

2

Fizz

4

Buzz

Fizz

7

8

Fizz

Buzz

11

Fizz

13

14

Fizz Buzz

16

17

Fizz

19

Buzz

Fizz

Ȥʤ롥ơߤʤϤޤǤ
warning.png äݤưƤ¿ƤϤʤȻΥץƱϤˤʤäƤ뤫ѤʤȤǴְ㤨 15 Buzz ʤɤȤʤäƤʤտΤ褦


(Ը) 3ܿ3ΤĤΤȤ

3ܿ3ΤĤΤȤۤˤʤפȤƾƱȤĩ路Ƥߤ褦
3ΤĤפȤʸɬפǡ϶ϰϤäƤʤμɬפʤΤǼʬĴ٤ɬפ롥


Collatz ͽۤĴ٤Ƥߤ褦

Collatz ͽ Ȥͽۤ롥 while ȤäƾΤƤߤ褦
ޤͽۤˤĤƵҤ褦ͽۤȤϡ n Фؿ f

  • n ξ: f(n) = n / 2
  • n ξ: f(n) = (3 * n) + 1

Ȥȡ֤ʤФƤ⤳ f 򷫤֤ŬѤȤɬ 1 ˤʤפȤΤǤ롥

notes.png ͽۤͿ줿ˤĤΩĤɤΤ뤿ˡϤ f ŬѤ 1 ˤʤޤǤηаޤϤץ񤳤
Ūˤϡ㤨

 ruby -w collatz.rb 100

Ȥƺǽ "100" 饹Ȥå

100 -> 50 -> 25 -> 76 -> 38 -> 19 -> 58 -> 29 -> 88 ->

44 -> 22 -> 11 -> 34 -> 17 -> 52 -> 26 -> 13 -> 40

-> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 1.

Ȥ褦ʽϤФ褦ˤ褦
warning.png f η̤ 1 Ǥʤ֤ while Ȥ¹ԤפȤȡ꡼ˤʤϤ


ž夲

Collatz ͽۤʲΤ褦Ĵ٤ץ񤤤ơ̤եˤȤäƤƤŬ˥դˤ褦
ϰʲ̤Ǥ롥

  • k ФơCollatz δؿ f 򲿲ŬѤ 1 ˤʤ뤫Ϥؿ num_col(k) def ȤäƤ롥
  • ץ൯ưˤ礭ʿ n (100餤) Ϳ롥
  • 1 Ϥơn ޤ num_col η̤Ϥ롥

warning.png num_col(1) 3 ˤʤäꤷʤ褦դ褦

Ūˤϡ㤨

 ruby -w col_num.rb 30

Ȥ 30 ޤǤĴ٤

1, 0

2, 1

3, 7

4, 2

5, 5

6, 8

7, 16

8, 3

9, 19

10, 6

11, 14

12, 9

13, 9

14, 17

15, 17

16, 4

17, 12

18, 20

19, 20

20, 7

21, 7

22, 15

23, 15

24, 10

25, 23

26, 10

27, 111

28, 18

29, 18

30, 18

Ȥ褦ʽϤФ褦ˤȤȤǤ롥
ơνϥǡե¸ƤŬʥġǥդˤ(ɽġ϶ػ)
collatz.png

Ȥ褦ʤΤȤȤˤʤ롥

Ǥ 30ޤǤ 100 餤ͤǼ¹Ԥơǡȥդ줿鴰ȤȤˤʤ롥


ݡ

ֵӹԤä½ˤĤơñˤޤȤ᤿򤻤.
ޤž夲ʬǼʬǽ񤤤ץ𤻤.

Ƽ

  1. °(ز)
  2. ֹ
  3. ǯ
  4. ̾
  5. οΥݡ(θȤˤĤƵŤ)

񤯤Τ˺ʤ褦.

about Icons, ClipArts

Some icons in this page are downloadable at ICONFINDER.

The "note" icon notes.png designed by Marco Martin is distributed with the LGPL licence,
the "warning" icon warning.png designed by Alexandre Moore with the GPL licence
and the "triangle" icon JNorth_arrow-right-sm.png designed by Joseph North is distributed with the Creative Commons (Attribution-Noncommercial-Share Alike 3.0 Unported) licence.

Some clip arts used in this page are downloadable at Open Clip Art Library.
We deeply appreciate their superb works. With licence, they describe that "the actual clipart content on open clipart library is Public domain" in the web.


źեե: filecollatz.png 160 [ܺ]