Ȼ/05

Top / Ȼ / 05

βˤĤ

ž夲ϡ2 Ϳ줿ޤǤˤĤơ٤ǿɤȽꤷƽϤפȤǤä
褯ͤȡǿȽץϴΤäƤΤǤ뤫顤Ѥɤ
ĤޤꡤΤι¤ȤƤϰʲΤ褦ˤʤϤ

  1. inluce Math
  2.  
  3. last_n = ARGV[0].to_i
  4. for n in 2..last_n do
  5.  
  6. (n ǿɤȽꤷ print 롥ΥץϰΤΤȤ)
  7.  
  8. end

ơn ǿɤȽꤷ print ץץʬ

(n ͿƤȤơ
isPrime = " is a prime number."
upper = sqrt(n).floor
for i in 2..upper do
  if (n % i == 0) then
    isPrime = " is not a prime number."
  end
end 
print(n,isPrime,"\n")

Ȥä(p ǤνϤ轵μ˹碌 print ľƤ)
warning.png ʹߤϡ§Ȥ p ϻȤ鷺 print Ȥ

ǤΤǡȤϤ򲿤ͤ˾ι¤˥ԥڤǡδ롥

  1. inluce Math
  2.  
  3. last_n = ARGV[0].to_i
  4. for n in 2..last_n do
  5.  
  6.   isPrime = " is a prime number."
  7.   upper = sqrt(n).floor
  8.   for i in 2..upper do
  9.     if (n % i == 0) then
  10.       isPrime = " is not a prime number."
  11.     end
  12.   end 
  13.   print(n,isPrime,"\n")
  14.  
  15. end

ݥ

Τ褦ˡ

βϤ狼äƤߤдñ.

ºݤˤ϶줷¿ϡ

ץߥ󥰤ˤϡΤǽνޤǹ¤ȯۤɬ

Ǥ롥

ʿʹ֤ϤΤȤ¤ª뤳Ȥ˽ƤΤʤޤƤʤ⤤β꤬񤷤ȴϡ줫μȤǡΤ餪/ͤפȤռƥץߥ󥰤򤹤褦ˤ褦

ǥ(Ƭλ): ٤ƥ˥å

warning.png Υץǡforʸ if ʸȤŬڤˡֹƬ(ǥ)פƤ뤳Ȥܤ褦
֤ʸͭϰϡ򼨤٥ʥƥ˥åǤ롥
ơˤäơǥȤ򸫤ǥץι¤狼롥ƤСend ޤפȤ顼䡤֤ɤޤǤ for ϰϤפʤɤǺߤʤɤ̵ˤʤ롥Ĥޤꡤ

ŬڤʥǥȤޤäȤʥץߥ󥰤

ʤΤǤ롥

ǥȤ˼֤ΤǤ? Ȥ⤢顼Ǻ֤ĹͤСǥȤμ֤ʤΤ顤Τ褦ˤäƤ褤

鿴Ԥϥ顼ǺˤȥǥȤ٤.

ץεǽʲ

ơϥץεǽʤȤưפä򤷤褦
줬ǤʤȡץϤޤĹƤ狼ˤΤȤʤꡤ񤤥ȲƤޤ

ǽñ̤Ȥƴؿ: def

εǽ̤ʥץʬ򡤴ؿȤˡؤܤ
ʸˡϴñǡ


def ؿ̾ ( ѿ̾1, ѿ̾2, ... )
δؿư, ׻ʤ
return
end


Ȥʤ롥
ĻʬʬѤʬȻפɤ
warning.png ؿΡѿ̾פϤδؿǡ֤Τߡͭؤäݤ͡

notes.png Ⱦ¤ĹͿȤαѤϤץ񤯤Ȥͤ褦
ΤȤ def Ȥäơ

  1. Ѥؿˤ
  2. Ѥؿˤ

褦ˤƤߤ褦


ޤ狼䤹ι¤˹ͤȡʤȤ

  1. include Math
  2.  
  3. # Ⱦ¤äƱߤѤϤؿ
  4. def circle(radius)
  5.   ׻
  6.   return ߤ
  7. end
  8.  
  9. # Ⱦ¤ȹ⤵äƱѤϤؿ.
  10. def volume(radius,length) 
  11.   ȤꤢѤϾ circle ؿȤä circle(radius) ǵޤȤ
  12.   Ѥ᤿
  13.   return ᤿
  14. end
  15.  
  16. # ʲץ.
  17. r = ARGV[0].to_f
  18. l = ARGV[1].to_f
  19.  
  20. v = volume(r,l)  # volume ȤؿʬǾǺäȤ
  21.  
  22. print("Volume is ",v,"\n")

Ȥˤʤ
warning.png Τ褦ˡι¤ץषƤޤפǾߤڤǤ롥顤

Τ褦ˡ٤Ȥϸ󤷤ˤơ
ˤäι¤ʤĤȤ褤

warning.png ΥץǤ # ȤäƥץɤʹѤ˥Ȥ񤤤Ƥ롥ϡ

Ruby ϥץ # ʹ ޤǤ̵뤹

ȤѤƤ. Ρ֥ȡפϥץ񤯤ȤʬνˤʤΤǡѶŪѤ褦

ˡֱ = ⤵פǤΤǡ volume ؿ˥ץहȰʲΤ褦ˤʤ(ʬȴ)

def volume(radius,length) 
  return circle(radius)*length
end


ơǸ˻ĤѴؿ circle ֱߤ = * (Ⱦ¤)פʤΤǡץߥ󥰤(ʬȴ)

def circle(radius)
  return PI * (radius ** 2)
end

Ȥˤʤ

ơǥץΤΤǡ(ץե̾ cylinder.rb Ȥ)㤨

 ruby -w cylinder.rb 1.2 1.1

Ȥ

Volume is 4.97628276328623

ȤϤ֤äƤϤ

def Τ꤬ߤԥʤ? : ǿȽץľƤߤ褦

def Τ꤬ߤϥץεǽư狼䤹ʤȤˤΤñ㤹ơdef Τ꤬ߤ狼ʤ⤷ʤ(󥹤ͭͤϤ狼ɤ)
notes.png ǡž夲Ǥ2 Ϳ줿ޤǤˤĤơ٤ǿɤȽꤷƽϤפȤΥץߥ󥰤򤳤 def ȤäƤľƤߤ褦

ޤΥץɬפʵǽΤ褦ʤΤͤǫ˥ꥹȥåפƤߤȡ

  1. n i dzڤ뤫ɤΤؿ
  2. (εǽ򷫤֤Ȥä) n ǿɤȽꤹؿ

ɤʤȤʬȤ⤦
ǡؿˤ뤳ȤȤơΤͤƤߤȼΤ褦ʴˤʤ

  1. include Math
  2.  
  3. # n i äơn i dzڤ뤫ɤϤؿ.
  4. def divisible(n,i)
  5.   n  i äơn  i dzڤ뤫ɤȽꤹ롥
  6.   return n  i dzڤʤ yes, Ǥʤ no Ȥֻϡ
  7. end
  8.  
  9. # n äơn ǿɤȽꤷƽϤؿ.
  10. def is_prime(n)
  11.   n äơn ǿɤȽꤹ롥
  12.   return n ǿʤ yes, Ǥʤ no Ȥֻϡ
  13. end
  14.  
  15. # ʲץ.
  16. last_n = ARGV[0].to_i
  17.  
  18. for n in 2..last_n do
  19.   if is_prime(n) then
  20.       print(n," is a prime number.\n")
  21.     else
  22.       print(n," is not a prime number.\n")
  23.   end
  24. end

warning.png Υץʬ(15ܤ21)褯褦 ȤƤ狼䤹ʤäƤ뤳Ȥ˵Ť 줬ؿѤΰĤǤ(ʪʣزǤ)

ơΤޤޤȤưʤΤǡɬפʴؿ򤭤ȥץष褦
ޤn ǿɤȽꤹץץΰΥС(ˤ⤢뤬)

(n ͿƤȤơ
isPrime = " is a prime number."
upper = sqrt(n).floor
for i in 2..upper do
  if (n % i == 0) then
    isPrime = " is not a prime number."
  end
end 
print(n,isPrime,"\n")

ǤäΤǡ divisible Ȥ is_prime ؿʸˡ˥ޥå褦ľȼΤ褦ʴˤʤ(狼䤹褦˥Ȥ򤤤Ƥ)

# n äơn ǿɤȽꤷƽϤؿ.
def is_prime(n)

  # ޤn ǿȡפꤷƤ.
  answer = true

  # ڤ뤫Ĵ٤ϰϤ n ޤǤǤ.
  upper = sqrt(n).floor

  # 2  n ޤ i ѤĴ٤.
  for i in 2..upper do
    # n  i dzڤʤ顤n ǿǤϤʤפѹ.
    if divisible(n,i) then
      answer = false
    end
  end 

  # ؿͤȤƽϤ롥
  return answer
end

warning.png ץ true, false ̤ʰ̣äơ줾ͤΡֿסֵפɽ
οͤ if ʸȽʬͤȤư롥
ޤ

Ruby ǤϿ true ǡ false ɽ

ȳФƤɤ

ơ줬狼 divisible ؿñ˺롥
ޤǤʸˡ˱äϼľ˺ΤʤС㤨аʲΤ褦ˤʤ

# n  i äơn  i dzڤ뤫ɤϤؿ.
def divisible(n,i)
  if (n % i == 0) then
      return true
    else
      return false
  end
end

ä褯񤯤ʤ? Τ褦ʴˤʤ.

# n  i äơn  i dzڤ뤫ɤϤؿ.
def divisible(n,i)
  return (n % i == 0)
end

warning.png ʤ񤱤뤫ͤƤߤ褦

ǥץϴǰΤΤܤƤ

  1. include Math
  2.  
  3. # n i äơn i dzڤ뤫ɤϤؿ.
  4. def divisible(n,i)
  5.   if (n % i == 0) then
  6.       return true
  7.     else
  8.       return false
  9.   end
  10. end
  11.  
  12. # n äơn ǿɤȽꤷƽϤؿ.
  13. def is_prime(n)
  14.  
  15.   # ޤn ǿȡפꤷƤ.
  16.   answer = true
  17.  
  18.   # ڤ뤫Ĵ٤ϰϤ n ޤǤǤ.
  19.   upper = sqrt(n).floor
  20.  
  21.   # 2 n ޤ i ѤĴ٤.
  22.   for i in 2..upper do
  23.     # n i dzڤʤ顤n ǿǤϤʤפѹ.
  24.     if divisible(n,i) then
  25.       answer = false
  26.     end
  27.   end 
  28.  
  29.   # ؿͤȤƽϤ롥
  30.   return answer
  31. end
  32.  
  33. # ʲץ.
  34. last_n = ARGV[0].to_i
  35.  
  36. for n in 2..last_n do
  37.   if is_prime(n) then
  38.       print(n," is a prime number.\n")
  39.     else
  40.       print(n," is not a prime number.\n")
  41.   end
  42. end

warning.png ǫ˽񤭤ΤĹʤäƤ뤬ܼȤƤϤ狼䤹ʤäƤ롥ܤ褦 ϡֳزʣʬ䤹뤳Ȥǡ٤ͤʤɤפȤˤ롥 ʹ֤Ǿߤϰ٤¿ʣˤǤʤȤͤȡΡʣʬפ򤷤ʤȥץߥ󥰤ϤޤäȤˤǤʤȤȤˤʤ롥 Ĥޤꡤ

def Τϥץߥ󥰤(ĥ)

ΤǤ롥def ȤäƥץहʤȤˤĤ褦

ʤꡧȥ

def λȤʤϽפʤΤǡ¾äƤߤ褦
notes.png ꤷȥ򤪤ʤ碌ƥɽ΢ФϤץ񤳤

ȥɤ뤫ζŪʤȤϸǵͤȤơˤäΤץߥ󥰤Ƥޤ㤨мΤ褦ʴˤʤ

  1. include Math
  2.  
  3. # ȥԤäơɽɤϤؿ
  4. def is_toss_front 
  5.   ȥԤäơɽФ뤫΢Ф뤫Ĵ٤롥
  6.   return ɽФ true, Ǥʤä false ϡ
  7. end
  8.  
  9. # ʲץ.
  10. toss_n = ARGV[0].to_i
  11.  
  12. # ɽФǽϥ
  13. front_n = 0
  14.  
  15. # toss_n 󡤥ȥ򷫤֤
  16. for n in 1..toss_n do
  17.   if is_toss_front then
  18.     # ɽФ顤Ȥ 1 䤹
  19.     front_n += 1
  20.   end
  21. end
  22.  
  23. # ǽ̤Ϥ褦
  24. print("front: ", front_n, " back: ", toss_n - front_n, "\n")


ȥˤϡRuby Τؿ rand Ѥ褦
ϽϤ 0 ʾ 1 ̤ؿʤΤǡ
rand 0ʾ 0.5̤ʤХɽ0.5ʾ 1̤ʤ΢ФȲ᤹С
̾Υȥ򤷤ȤϤ.
ĤޤꡤΤ褦ʴˤʤ(ʬΤ)

# ȥԤäơɽɤϤؿ
def is_toss_front 
  if (rand < 0.5) then
      return true
    else
      return false
  end
end

å褯񤯤ʤ? Τ褦˽񤤤Ƥɤ

# ȥԤäơɽɤϤؿ
def is_toss_front 
  return (rand < 0.5)
end

warning.png rand ϡrand(n) ȤΰͿ 0 ʾ n ̤ȤƽϤ롥ѤƤɤ;ϤΤͤϤΤ꤫˽ľƤߤ褦

ơǥץΤΤǡ(ץե̾ coin.rb Ȥ)㤨

 ruby -w coin.rb 50

Ȥ

front: 26 back: 24

ʤɤȤϤ֤äƤϤޤǤäƤߤ褦

ž夲

äѤ餤Τä⤯ͻҤñ˥ߥ졼ȤƽϤץŪˤϡ

  1. ʤȤޤä˿ʤΨ 40%
  2. ʤȤФᱦ˿ʤΨϤ줾 30%
  3. 30ʬ餤ιƻϩο椫⤭Ϥ᤿
  4. (ջ)ޤ30ʬ餤ߥ졼Ȥ
  5. ⤴Ȥˡɤˤ뤫 print ʸǤäݤ?

ȤΤ
㤨С(ץե̾ walk.rb Ȥ)

 ruby -w walk.rb

Ȥ

----------------o--------------

----------------o--------------

---------------o---------------

--------------o----------------

-------------o-----------------

-------------o-----------------

-------------o-----------------

--------------o----------------

--------------o----------------

--------------o----------------

---------------o---------------

---------------o---------------

---------------o---------------

--------------o----------------

-------------o-----------------

-------------o-----------------

------------o------------------

------------o------------------

------------o------------------

------------o------------------

------------o------------------

------------o------------------

------------o------------------

-----------o-------------------

----------o--------------------

----------o--------------------

----------o--------------------

---------o---------------------

---------o---------------------

---------o---------------------

Τ褦ʽϤ֤äƤ褦ˤȤȤǤ(ΨäʤΤǥץ¹Ԥ٤˷̤ϰ㤦)
ȡ"o" äΤɽƤơ夫鲼ػ֤ФäƤȤȤˤʤ롥

warning.png ä󤬰ФᱦФẸΤɤؿʤफϥȥȻ褦ʴؿɤ
warning.png ʬȱȺ 3̤ˤʤΤǡϤɤ?
warning.png äξ print ؿȤ狼䤹
warning.png ˤäƥץΤ߷פƽ񤤤Ƥޤ

ݡ

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

Ƽ

  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.