Ȼ/04

Top / Ȼ / 04

βˤĤ

ϡͿ줿ǿɤȽꤹץפ񤯤Τä
ˤĤơĤ򼨤Ʋ򤷤Ƥ

ǤѤʥץ

ޤϡǿ˽ä 2 (ο-1) ޤǤdz뤫ɤåȤɥå̤ʹפŤ餵˥ץ򤷤(ΤˡԤֹĤɽƤ).
ϡǽ n ǿȲꤷơ n õƤȤ르ꥺǤ롥

  1. include Math
  2.  
  3. n = 133
  4. isPrime = "is a prime number."
  5.  
  6. for i in 2..(n-1) do
  7.   if (n % i == 0) then
  8.     isPrime = "is not a prime number."
  9.   end
  10. end
  11.  
  12. p n 
  13. p isPrime

warning.png for ʸ 2..(n-1) ȤʤäƤΤϡi 2 (n-1) ޤǤϰϤѲȤ̣Ǥ롥
warning.png for ʸ if ʸäƤ롥ȤϤƤǽʤΤǡפĤƤߤ褦
warning.png if ʸ else᤬άƤ롥ȤǽʤȤ¿ΤǡϤפĤƤߤ褦

̯? ץ

ܼѤʤΤ褦ʤΤ⤢롥

  1. include Math
  2.  
  3. n = 133
  4. isPrime = 1
  5.  
  6. for i in 2..(n-1) do
  7.   isPrime *= (n % i)
  8. end
  9.  
  10. p n
  11. if (isPrime == 0) then
  12.     p "is not a prime number."
  13.   else
  14.     p "is a prime number."
  15. end

n i dzڤ줿Ȥ n % i ˤʤ뤳ȤѤơ(n % i) ɤɤݤƤäơˤʤäƤ뤫ɤåȤˡǤ롥
warning.png *= ʬϡֺդѿ˱դ򤫤ƤפȤ̣Ǥ롥 isPrime = isPrime * (n % i) Ƚ񤤤Ƥɤ

ϰϤ򾯤ʤ르ꥺ

ɤͤƤߤȡn ꤭뤫ɤĴ٤ i n ʲޤǤߤнʬǤ롥
(㤨С21 = 3 * 7 = 7 * 3 3 dz뤳Ȥ狼нʬ)
äơõϰϡפϤäȾ롥ιͤDzɤΤΥץˤʤ롥

  1. include Math
  2.  
  3. n = 133
  4. isPrime = "is a prime number."
  5.  
  6. upper = sqrt(n).floor
  7. for i in 2..upper do
  8.   if (n % i == 0) then
  9.     isPrime = "is not a prime number."
  10.   end
  11. end
  12.  
  13. p n 
  14. p isPrime

ϰϤäȾʤ르ꥺ

ͤƤߤȡΥץǤ 2 dz뤫ɤΤ᤿ȡ4 Ǥ뤫ɤåʤɡ餫̵̤ʥåפäƤ(4dzʤ 2 Ǥ뤸).
ͤƤߤȡǿ򾮤󤷤ƤäơȤäƥåᤤĤʤɡñˡˤɤ;ϤϤ롥

¾

ǿɤȽɤԤΥ르ꥺˤĤƤϼ¤¿θ椬롥
̣Τͤ(google scholar ʤɤ "primality test" Ȥñ)õƤߤȤ褤

ʤ

ɤޤưץʬǽ񤱤
ޤʤäͤϡޤϡֿפȤǤ褤ΤǡĽϤ褦

ץå()

ơץब־׵ǽ̤ˤϡϵǽʤȤޤ
ޤǤϤ٤ǡץ˽񤭡Ϥϲ̤˽ФƤΤޤޡȤˡǤǤؤ롥

ǡޤϥץåȡʤץ˥ǡϤˡˤĤƳؤܤ
ץåȤԤˡϡ绨Ĥˤäơ

  • ư˥ѥ᡼ȤͿ
  • ɸϡפȤ̤ޤäˡб
  • ե뤫ɤ߹
  • ץबưƤ˥󥿥饯ƥ֤˥ǡͿ

ʤɡ͡ˡꡤ줾˸Ը롥

ʤꤿؤ֤Ⱥ𤹤顤ϰĤؤܤ

ư˥ѥ᡼Ȥƥǡ: ARGV ѿ

ư˥ѥ᡼ȤƥǡץͿ뤳ȤǤ롥
ΤȤץ¦Ǥ

ѿ ARGV[0] ǰܡARGV[1] , (ʲƱ)
εưѥ᡼.

notes.png ȤäƤ⡤̣褯ʬʤͤ⤤顤ץưƤߤ褦
ޤΥץ㤨 sample.rb Ȥե̾¸Ȥơ

  1. include Math
  2.  
  3. p "The first parameter is "
  4. p ARGV[0]
  5.  
  6. p "The second parameter is "
  7. p ARGV[1]

Ȥơ

 ruby -w sample.rb aaa bbb

ȤƥץưƤߤ褦
ϡΥץưȤ 1ܤΥѥ᡼ aaa , 2ܤΥѥ᡼ bbb ͿȤˤʤ롥
ȡΤ褦ʽϤϤ

"The first parameter is "

"aaa"

"The second parameter is "

"bbb"

򸫤ȡץ¦ aaa bbb Ƥ뤳Ȥ狼롥
warning.png ruby Ǥ " (֥륯ơ)ǰϤäʬ ʸ Ǥ.
warning.png ֥륯ơǰϤޤƤ뤳η򸫤Ȥ狼뤬ruby Ǥ

ARGV[0], ... ʸȤư롥

顤ǡȤͿȤϡץ¦н褷ʤȤʤ
Τ˥ץ򸫤Ƥ餪
Ϳѥ᡼3Ĥ­ץsample.rb Ȥ̾Ȳꤷ)

  1. include Math
  2.  
  3. a = ARGV[0].to_i
  4. b = ARGV[1].to_i
  5. c = ARGV[2].to_i
  6.  
  7. p a+b+c

Ȥơ

 ruby -w sample.rb 3 8 2

ȤƥץưƤߤȡ 13 Ϥ

warning.png ΥץΡ.to_i ȤΤϡѿѴ뵡ǽ
warning.png Ʊͤˡ.to_f Ȥȼ¿Ѵ롥
Τ褦ˡ

ä ARGV[0], ... ΤĤʤ .to_i
¿ΤĤʤ .to_f ĤѴƤ.



notes.png ǤϡͿ줿ǿɤȽꤹץɤơȽꤹ٤ư˥ѥ᡼ȤͿ褦ˤ褦.
Ūˤϡ㤨С

 ruby -w sample.rb 133

Ȥ

133

"is not a prime number."

ʤɤȽϤ褦ˤ褦ȤȤǤ롥

ơͿѥ᡼οޤäƤʤ礬롥
Ȥϡץ¦Ǽäѥ᡼θĿΤ뤳Ȥн褹롥
Ūˤϡ

ARGV.size ǥѥ᡼θĿ狼.

㤨С

  1. include Math
  2.  
  3. p ARGV.size

Ȥ

 ruby -w sample.rb a b c

ȤƥץưƤߤȡ3 ȤϤϤ



notes.png Ǥϡѥ᡼ȤͿʣοʿѤϤץ񤳤
Ūˤϡ㤨С

 ruby -w sample.rb 3 23 4 8 9

Ȥ

9.4

ʤɤȽϤ褦ˤ褦ȤȤǤ롥
warning.png νϤ 9 ˤʤäƤޤͤϤɤΤǡֲƤΤפ褯ͤ褦
warning.png ͿΤ褦5ĤȤϸ¤ʤ6ĤǤ3ĤǤפʤ褦˥ץष褦

ȥץå()

ǡ̤ˤФޤؤǤʤǤϾȤϤؤ
ǡĥȥץåȤˤĤƤؤǤ

()ʽϤ: print ̿

ޤǽϤϰñ p ̿ΤߤǹԤäƤϽϤܤƤʤΤɤߤˤ̤¿(¤ p ̿ϥץޤˤʵǽʤΤ)
ǡϷ̤⤦ɤߤ䤹 print ̿ˤĤƳؤܤ
Ȥϴñǡ

print() ˽ϤΤ򥫥ޤǶڤä¤٤

Ǥäɤߤ䤹ɽ롥
warning.png \n ȤüʸϤȤϤԤʤ

ϤԤȤ print ̿ʸ \n 褦

ޤʲΥץ򸫤Ƥߤ褦

  1. include Math
  2.  
  3. a = 3.0
  4. b = 8.0
  5.  
  6. print("a is ",a," and b is ",b,".\n")
  7. print("Average is ", (a+b)/2, ".\n")

Υץưȡ

a is 3.0 and b is 8.0.

Average is 5.5.

ȤʤϤǤ롥
warning.png \ ȤϡˤäƤ ɽΤդ褦ƱΤ

ʤߤˡΥץ

  1. include Math
  2.  
  3. a = 3.0
  4. b = 8.0
  5.  
  6. print("a is ")
  7. print(a)
  8. print(" and b is ")
  9. print(b)
  10. print(".")
  11. print("\n")
  12.  
  13. print("Average is ")
  14. print((a+b)/2)
  15. print(".")
  16. print("\n")

Ƚ񤤤ƤƱϤˤʤ롥Ȥ狼ˤΤ褦 print ̿ʬ䤷ץष䤹Ȥ⤢롥



notes.png DzɤֵưͿѥ᡼ʿѤץפprint ̿ȤäƤ˲ɤơϤɤߤ䤹褦
Ūˤϡ㤨С

 ruby -w sample.rb 3 23 4 8 9

Ȥ

Input: 3.0 23.0 4.0 8.0 9.0

Average: 9.4

ʤɤȽϤ褦ˤǤФ֤褤

ϤȤäƤˡ: ѥ & tee

ΤȤϤϲ̤ήǡ츫ʤʤäƤޤ
ե˽ϤץεǽʤɤˤĤƤϸdzؤ֤ΤǡǤ ưˤäȹפ ̤ȥեƱ˽Ϥñ ˡˤĤƳؤܤ

ϴñǡ

ץεư
| tee ̤ؤνϤϿƤե̾
ǸˤäĤ

Ǥ褤
ޤ򸫤Ƥߤ褦㤨С

 ruby -w sample.rb 3 23 4 8 9

Ȥ

Input: 3.0 23.0 4.0 8.0 9.0

Average: 9.4

ȽϤץФơoutput.dat Ȥ̾Υե˷̤ϿƤȤ롥
ξϡ

 ruby -w sample.rb 3 23 4 8 9 | tee output.dat

Ȥǡ̤ˤ⡤output.dat Ȥեˤ

Input: 3.0 23.0 4.0 8.0 9.0

Average: 9.4

Ȥ̤Ϥ뤳Ȥˤʤ롥
warning.png | tee ϽϤ˻ȤեȤޤäˤƽϤƤޤ(񤭤)ᡤ¸ΥեФƻȤȤϵĤ褦
warning.png ʤߤˡ | tee -a ե̾ Ȥ -a ĤƻȤȡեȤ˽ϤɲáפƤ롥 ˻Ȥʬ褦

ž夲

ޤǤ˳ؤμȤäơΤ褦ʥץꡤ̤ output.dat Ȥ̾Υե˵Ͽ褦
ΥץȤϡξΤǤ롥

  1. ư˥ѥ᡼Ȥ n Ϳ(礭 100 300٤ꤷ褦).
  2. 2 n Τ٤ƤޤǡǿɤȽꤹ롥
  3. Ƚ̤֤ˤ٤ƽϤ롥

Ūˤϡsample.rb ȤץեȤơ㤨

 ruby -w sample.rb 133

Ȥ

2 is a prime number.

3 is a prime number.

4 is not a prime number.

ά

131 is a prime number.

132 is not a prime number.

133 is not a prime number.

ȤϤΤȤȤǤ롥

ơνϷ̤ output.dat Ȥե˵ϿƤȤȤˤʤ롥

ݡ

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

Ƽ

  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.