# Refactoring of enumerating prime numbers

Yuguiã§ã™ã€‚

ã‚’ææ¡ˆã—ã¾ã™ã€‚ç›´ã—ãŸã„ã®ã¯3ç‚¹ã§ã™ã€‚

1. mathn.rbã«å…¥ã£ã¦ã„ã‚‹
2. ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ã‚’ç”Ÿæˆã™ã‚‹å¿…è¦ãŒã‚ã‚‹

== mathn.rbã«å…¥ã£ã¦ã„ã‚‹
ç´ æ•°åˆ—æŒ™ã¯æ•´æ•°ã«é–‰ã˜ãŸè¨ˆç®—ã§ã‚‚å¿…è¦ã«ãªã‚‹ã‚‚ã®ãªã®ã§ã€mathn.rbãŒã‚‚ãŸã‚‰ã™
int / int => rational
ã¯å¬‰ã—ããªã„ã“ã¨ãŒã‚ã‚Šã¾ã™ã€‚

æ·»ä»˜ã®ãƒ‘ãƒƒãƒã§ã¯ä»®ã«lib/math/prime.rbã¨ã„ã†ãƒ•ã‚¡ã‚¤ãƒ«ã«è¿½ã„å‡ºã—ã¦ã¿ã¾ã—ãŸã€‚

== ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ã‚’ç”Ÿæˆã™ã‚‹
Prime.new.each do |prime|

# do something

end
è¦ã™ã‚‹ã«ã€Primeã‚ªãƒ–ã‚¸ã‚§ã‚¯ãƒˆã¯ç´ æ•°åˆ—æŒ™ã«å¯¾ã™ã‚‹å¤–éƒ¨ã‚¤ãƒ†ãƒ¬ãƒ¼ã‚¿ã§ã™ã€‚
ã“ã‚Œã¯å°‘ã—å¥‡å¦™ã«æ€ãˆã¾ã™ã€‚æ¬¡ã®ã‚ˆã†ã«æ›¸ããŸã„ã§ã™ã€‚
Prime.each do |prime|

# do something

end

• å¤–éƒ¨ã‚¤ãƒ†ãƒ¬ãƒ¼ã‚¿ã¨ã—ã¦Prime#succã‚’å®Ÿè£…
• ãã‚Œã‚’ç”¨ã„ã¦å†…éƒ¨ã‚¤ãƒ†ãƒ¬ãƒ¼ã‚¿Prime#eachã‚’å®Ÿè£…
• æ›´ã«ã€Prime#eachã«ãƒ–ãƒ­ãƒƒã‚¯ãŒä¸Žãˆã‚‰ã‚Œãªã„å ´åˆã¯Enumeratorã‚’ç”Ÿæˆ
ã¨ã„ã†å¥‡å¦™ãªã“ã¨ã«ãªã£ã¦ã„ã¾ã™ã€‚

ã‚¢ãƒ«ã‚´ãƒªã‚ºãƒ ã¨ã—ã¦æ ¼åˆ¥ã®ç†ç”±ãŒã‚ã‚‹ãªã‚‰ã°ã¨ã‚‚ã‹ãã€Primeã®å ´åˆã¯ç‰¹ã«å¤–éƒ¨
ã‚¤ãƒ†ãƒ¬ãƒ¼ã‚¿ã‚’å„ªå…ˆã™ã‚‹ç†ç”±ã¯ãªã„ã‚ˆã†ã«æ€ãˆã¾ã™ã€‚

ä»Šã¯Enumerable::EnumeratorãŒã‚ã‚‹ã®ã§ã™ã‹ã‚‰ã€å¤–éƒ¨ã‚¤ãƒ†ãƒ¬ãƒ¼ã‚¿ç”Ÿæˆã®ãŸã‚ã ã‘
éƒ¨ã‚¤ãƒ†ãƒ¬ãƒ¼ã‚¿ã‚’ã¾ãšæä¾›ã™ã‚‹ã®ãŒã‚ˆã‚Šè‡ªç„¶ã§ã¯ãªã„ã§ã—ã‚‡ã†ã‹ã€‚

Integer.each_prime do |prime|

# do something

end

å¤–éƒ¨ã‚¤ãƒ†ãƒ¬ãƒ¼ã‚¿ã‚’æä¾›ã™ã‚‹ãŸã‚ã«ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ã‚’ç”Ÿæˆã™ã‚‹å¿…è¦ãŒã‚ã£ãŸã®ã ã¨æ€
ä»˜åŠ ã™ã‚‹ã®ãŒè‡ªç„¶ã ã¨æ€ã„ã¾ã™ã€‚

ã¨ã„ã†ã‚ã‘ã§ã€ãã®ã‚ˆã†ã«ã™ã‚‹ãƒ‘ãƒƒãƒã‚’æ›¸ã„ã¦ã¿ã¾ã—ãŸã€‚æ·»ä»˜ã—ã¾ã™ã€‚

e\$B\$1\$\$\$8\$e!w\$\$\$7\$D\$+\$G\$9e(B.

In [ruby-dev :35863 ] the message: "[ruby-dev:35863] Refactoring of
enumerating prime numbers ", on Aug/16 14:11(JST) “Yugui (Yuki
Sonoda)” writes:

Yuguie\$B\$G\$9!#e(B

mathn.rbe\$B\$K<}O?\$5\$l\$F\$\$\$ke(BPrimee\$B%/%i%9\$N;H\$\$>!<j\$,\$\$\$^\$\$\$ANI\$/\$J\$\$\$N\$G2~A1e(B
e\$B\$rDs0F\$7\$^\$9!#D>\$7\$?\$\$\$N\$Oe(B3e\$BE@\$G\$9!#e(B

1. mathn.rbe\$B\$KF~\$C\$F\$\$\$ke(B
2. e\$B%\$%s%9%?%s%9\$r@8@.\$9\$kI,MW\$,\$"\$ke(B
3. e\$B\$=\$b\$=\$bFH<+\$N%/%i%9\$,I,MW\$+e(B?

e\$B%\$%s%?!<%U%’!<%9\$NB&LLe(B, e\$B5!G=\$NB&LLe(B,
e\$B<B8=\$NB&LL\$+\$i8+\$F\$_\$?\$\$\$H;W\$\$\$^\$9e(B.

e\$B\$^\$:e(B, e\$B%\$%s%?!<%U%’!<%9>e\$NB&LL\$+\$ie(B.

== mathn.rbe\$B\$KF~\$C\$F\$\$\$ke(B
e\$BAG?tNs5s\$O@0?t\$KJD\$8\$?7W;;\$G\$bI,MW\$K\$J\$k\$b\$N\$J\$N\$G!“e(Bmathn.rbe\$B\$,\$b\$?\$i\$9e(B
int / int => rational
e\$B\$O4r\$7\$/\$J\$\$\$3\$H\$,\$”\$j\$^\$9!#e(B

e\$BJ,\$+\$i\$J\$\$\$3\$H\$O\$J\$\$\$G\$9e(B.

e\$B\$?\$@e(B, e\$B\$=\$b\$=\$be(B, Prime e\$B\$Oe(B e\$B85!9e(B,
e\$BAG0x?tJ,2r\$N\$?\$a\$Ke(B mathn.rb e\$B\$KF~\$C\$F\$\$e(B
e\$B\$k\$b\$N\$Ge(B, mathn.rb
e\$B\$KF1:-\$5\$l\$F\$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$B\$K4qL/\$J?6\$kIq\$\$\$r\$5\$;\$kI,MW\$O\$”\$j\$^\$;\$s!#\$^\$?!"e(BRubye\$B%i%\$%V%i%j\$H\$7\$F\$OFbe(B
e\$BIt%\$%F%l!<%?\$r\$^\$:Ds6!\$9\$k\$N\$,\$h\$j<+A3\$G\$O\$J\$\$\$G\$7\$g\$&\$+!#e(B

e\$B\$H\$"\$j\$^\$9\$,e(B,

e\$B8=9T\$Ne(BPrimee\$B\$+\$iFbIt%\$%F%l!<%?\$r<B8=\$9\$kJ}K!\$He(BYuguie\$B\$5\$s\$NFbIt%\$%F%l!<%?e(B
e\$B\$+\$ie(BEnumertore\$B\$rMxMQ\$9\$kJ}K!\$H\$I\$A\$i\$,<+A3\$+\$H\$\$\$(\$Pe(B,
e\$B30It%\$%F%l!<%?\$re(B
e\$B<B8=\$9\$k\$?\$a\$Ke(BFibere\$B\$rMQ\$\$\$F\$\$\$kJ}\$,\$h\$C\$]\$IIT<+A3\$@\$H;W\$\$\$^\$9e(B.
e\$B5mEa\$Ce(B
e\$B\$F46\$8\$,\$7\$^\$9e(B. e\$B\$5\$i\$Ke(B,
e\$B>e5-\$G=R\$Y\$?\$h\$&\$K5!G=E*\$KNt\$k\$b\$N\$KCV\$-49\$(\$ke(B
e\$BM}M3\$O\$J\$\$\$H9M\$(\$^\$9e(B.

e\$B;d\$N7kO@\$H\$7\$F\$Oe(B:

• mathn.rb e\$B\$+\$ie(B e\$BJ,N%FHN)\$5\$;\$ke(B => e\$B;[email protected](B

• Prime.each e\$B\$NF3F~e(B => e\$B;[email protected](B

• Integer.each_prime e\$B\$NF3F~e(B => e\$BI,MW@-\$O\$"\$^\$j46\$8\$J\$\$e(B

• e\$B<BAu\$K4X\$7\$Fe(B => e\$B8=>u\$N\$^\$^\$G\$h\$\$e(B.

e\$B\$G\$9e(B.

__
---------------------------------------------------->> e\$B@PDMe(B
e\$B7=<ye(B <<—
---------------------------------->> e-mail: [email protected] <<—

Yuguiã§ã™ã€‚

ã„ã—ã¥ã‹ã•ã‚“ã€ã‚ã‚ŠãŒã¨ã†ã”ã–ã„ã¾ã™ã€‚

keiju ISHITSUKA ã•ã‚“ã¯æ›¸ãã¾ã—ãŸ:

== mathn.rbã«å…¥ã£ã¦ã„ã‚‹
ç´ æ•°åˆ—æŒ™ã¯æ•´æ•°ã«é–‰ã˜ãŸè¨ˆç®—ã§ã‚‚å¿…è¦ã«ãªã‚‹ã‚‚ã®ãªã®ã§ã€mathn.rbãŒã‚‚ãŸã‚‰ã™
int / int => rational
ã¯å¬‰ã—ããªã„ã“ã¨ãŒã‚ã‚Šã¾ã™ã€‚

åˆ†ã‹ã‚‰ãªã„ã“ã¨ã¯ãªã„ã§ã™.
(snip)
ãã®ã‚ãŸã‚ŠãŒ, lib/prime.rb ã§ãªã lib/math/prime.rb ã¨ã„ã†é æ…®ã«ãªã£ã¦
ã„ã‚‹ã‚ˆã†ãªæ°—ãŒã—ã¾ã™.

ã‚‚ã®ã‚’æ€ã„ã¤ã‹ãªã‹ã£ãŸã®ã§lib/math/prime.rbã«ã—ãŸã¾ã§ã§ã™ãŒã€åˆ†ã‘ã¦ã„ãŸ

== ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ã‚’ç”Ÿæˆã™ã‚‹
è¦ã™ã‚‹ã«ã€Primeã‚ªãƒ–ã‚¸ã‚§ã‚¯ãƒˆã¯ç´ æ•°åˆ—æŒ™ã«å¯¾ã™ã‚‹å¤–éƒ¨ã‚¤ãƒ†ãƒ¬ãƒ¼ã‚¿ã§ã™ã€‚
ã“ã‚Œã¯å°‘ã—å¥‡å¦™ã«æ€ãˆã¾ã™ã€‚

å…¨ç„¶å¥‡å¦™ã§ã‚‚ãªã„ã¨æ€ã„ã¾ã™ãŒ? æ¦‚å¿µçš„ã«ã¯, Primeã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ãŒç´ æ•°ã®é›†
åˆã‚’è¡¨ã—ã¦ã„ã‚‹ã¨è€ƒãˆã¦ã„ã‚‹ã‹ã‚‰ã§ã™. ãã®ç´ æ•°é›†åˆã‹ã‚‰æ•°ãˆä¸Šã’ã¦ã„ã‚‹ã¨è€ƒ
ãˆã¦ã„ã‚‹ã®ã§ã“ã®ã‚ˆã†ãªã‚¤ãƒ³ã‚¿ãƒ¼ãƒ•ã‚§ãƒ¼ã‚¹ã«ãªã£ã¦ã„ã¾ã™.

ç´ æ•°ã®å…¨ä½“ã®ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ãŒè¤‡æ•°å­˜åœ¨ã—å¾—ã‚‹ã®ã¯ãŠã‹ã—ã„ã§ã™ã€‚æ•´æ•°ã®å…¨ä½“ã«å¯¾
å¿œã™ã‚‹ã‚‚ã®ãŒIntegerã§ã‚ã‚‹ã‚ˆã†ã«ã€ç´ æ•°ã®å…¨ä½“ã«å¯¾å¿œã™ã‚‹ã®ã¯Primeã§ã‚ã‚‹ã»ã†
ãŒè‡ªç„¶ã ã¨æ„Ÿã˜ã¾ã—ãŸã€‚

è¦‹è§£ã®ç›¸é•ã«å¸°çµã™ã‚‹ã®ã§ã—ã‚‡ã†ãŒã€

for prime in Prime

# …

end

ã£ã¦æ›¸ãã¨ãŠã‹ã—ã„æ°—ãŒã™ã‚‹ã®ã§,

ç§ã«ã¯ãŠã‹ã—ããªãã€ã“ã¡ã‚‰ã®ã»ã†ãŒè¡¨è¨˜ã¨ã—ã¦è‡ªç„¶ã ã¨æ„Ÿã˜ã‚‰ã‚Œã¾ã™ã€‚

ç´ æ•°å…¨ä½“ã¨ã„ã†ã‚‚ã®ã®Rubyè¡¨ç¾ãŒè¤‡æ•°å­˜åœ¨ã—å¾—ã‚‹ã‚ˆã†ã«ãªã£ã¦ã„ã‚‹ã®ã¯ã€ã‚„ã¯ã‚Š

ãã—ã¦ã€å®Ÿè£…ä¸Šã®æŒ‡æ‘˜3ç‚¹ã«ã¤ã„ã¦ã€
(1)

ç¾è¡Œã®Primeã‹ã‚‰å†…éƒ¨ã‚¤ãƒ†ãƒ¬ãƒ¼ã‚¿ã‚’å®Ÿç¾ã™ã‚‹æ–¹æ³•ã¨Yuguiã•ã‚“ã®å†…éƒ¨ã‚¤ãƒ†ãƒ¬ãƒ¼ã‚¿
ã¦æ„Ÿã˜ãŒã—ã¾ã™.

ç‰›åˆ€ã¨ã„ãˆã°ãã†ã‹ã‚‚ã—ã‚Œã¾ã›ã‚“ã€‚

(2)

Enumeratorã¨å¤–éƒ¨ã‚¤ãƒ†ãƒ¬ãƒ¼ã‚¿ã¨ã—ã¦ã®Primeã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ã§ã™ãŒ, Enumerator
ã¯ã‚¹ãƒ¬ãƒƒãƒ‰ã®å¢ƒç•Œã‚’è¶Šãˆã‚‰ã‚Œãªã„ã¨ã„ã†åˆ¶ç´„ãŒã‚ã‚Šã¾ã™ã®ã§, ãã®ã‚ˆã†ãªåˆ¶ç´„
ã®ãªã„Primeã‹ã‚‰ã‚ã–ã‚ã–ãƒ€ã‚¦ã‚°ãƒ¬ãƒ¼ãƒ‰ã™ã‚‹å¿…è¦ã¯ãªã„ã§ã™.

ç¢ºã‹ã«ã€‚

(3)

@@primes
@@next_to_check
@@ulticheck_index
@@ulticheck_next_squared

ã‚¹å¤‰æ•°ã¨ã—ã¦ã¯ãµã•ã‚ã—ããªã„ã¨æ€ã„ã¾ã™.

ã“ã‚Œã‚‚ç¢ºã‹ã«ã€‚

2. Prime.each ã¯ Prime::Generator ã‚ªãƒ–ã‚¸ã‚§ã‚¯ãƒˆã‚’å‘¼ã³å‡ºã—ã¦åˆ—æŒ™
3. (optional) Integer.each_primeã¯Prime.eachã«è»¢é€

ã“ã®ã‚ˆã†ã«å¤‰æ›´ã—ã¦ã¿ãŸãƒ‘ãƒƒãƒã‚’æ·»ä»˜ã—ã¾ã™ã€‚

ç†ç”±ã¨ã—ã¦ã¯ã€

ãƒ¡ãƒªãƒƒãƒˆã¯ç´å¾—ã§ãã‚‹ä¸€æ–¹ã§ã€ã‚„ã¯ã‚Šç´ æ•°å…¨ä½“ã‚’è¡¨ã™ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ãŒè¤‡æ•°å­˜åœ¨ã™
ã‚‹ã®ã¯é•å’Œæ„Ÿã‚’æŒã¤ãŸã‚ã§ã™ã€‚Primeã®ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ãŒå¤–éƒ¨ã‚¤ãƒ†ãƒ¬ãƒ¼ã‚¿ã§ã‚ã‚‹ã“
ã¨ã‚’é¿ã‘ã‚‰ã‚Œã‚Œã°ã€ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹åŒ–ã¯obsoleteã«ã§ãã¾ã™ã€‚

ã‘ã§ã¯ãªãã€Œç´ æ•°å…¨ä½“ã€ã‚’ã‚ã‚‰ã‚ã™ã®ãŒPrimeå®šæ•°ã€ã¤ã¾ã‚Šå”¯ä¸€ã§ã‚ã£ã¦ã»ã—ã„
ã‹ã‚‰ã§ã™ã€‚

æ–¹ãŒè‡ªç„¶ã§ã‚ã‚‹ã¨æ„Ÿã˜ã¾ã™ã€‚

e\$B\$1\$\$\$8\$e!w\$\$\$7\$D\$+\$G\$9e(B.

In [ruby-dev :35867 ] the message: "[ruby-dev:35867] Re: Refactoring
of enumerating prime numbers ", on Aug/17 23:13(JST) “Yugui (Yuki
Sonoda)” writes:

Yuguie\$B\$G\$9!#e(B
e\$B\$\$\$7\$E\$+\$5\$s!"\$"\$j\$,\$H\$&\$4\$6\$\$\$^\$9!#e(B

e\$B\$\$\$(\$\$\$(e(B. e\$B\$I\$b\$G\$9e(B.

e\$B\$^\$:e(B, e\$B:G=i\$Ke(B, e\$B:#2s\$N0F\$O\$J\$+\$J\$+\$9\$P\$i\$7\$\$\$H;W\$\$\$^\$9e(B.
e\$B:#\$N<BAu\$h\$j\$b@0e(B
e\$BM}\$5\$l\$F\$\$\$k5\$\$,\$7\$^\$9e(B. e\$B\$?\$@\$7e(B, e\$B\$\$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e\$B\$H\$\$\$&AG?tNs5s\$N30It%\$%F%l!<%?%/%i%9\$r:[email protected](B
1.1. e\$B8_49@-\$N\$?\$a!"\$^\$?FbIt%\$%F%l!<%?\$NDs6!<T\$H\$7\$Fe(BPrimee\$B%/%i%9\$r29B8e(B
2. Prime.each e\$B\$Oe(B Prime::Generator e\$B%*%V%8%’%/%H\$r8F\$S=P\$7\$FNs5se(B
3. (optional) Integer.each_primee\$B\$Oe(BPrime.eache\$B\$KE>Awe(B

e\$B%=!<%9\$r8+\$?46\$8\$J\$+\$J\$+\$h\$\$46\$8\$G\$9e(B.

e\$B\$"\$H\$Oe(B, e\$B>e=R\$Ne(B Singleton e\$B\$N0F\$bF~\$l\$Fe(B,
e\$B\$\$\$?\$@\$1\$k\$H\$&\$l\$7\$\$\$J\$He(B.

e\$B\$G\$Oe(B, e\$B?70FBT\$C\$F\$\$\$^\$9e(B(^^;

__
---------------------------------------------------->> e\$B@PDMe(B
e\$B7=<ye(B <<—
---------------------------------->> e-mail: [email protected] <<—

e\$BK-J!\$G\$9!#e(B

on Aug/16 14:11(JST) “Yugui (Yuki S.)” writes:

mathn.rbe\$B\$K<}O?\$5\$l\$F\$\$\$ke(BPrimee\$B%/%i%9\$N;H\$\$>!<j\$,\$\$\$^\$\$\$ANI\$/\$J\$\$\$N\$G2~A1e(B
e\$B\$rDs0F\$7\$^\$9!#D>\$7\$?\$\$\$N\$Oe(B3e\$BE@\$G\$9!#e(B

e\$B\$3\$N5!2q\$Ke(B
[ruby-dev:30932] Re: Integer#prime_division e\$B\$He(B Prime
[ruby-dev:30937] Re: Integer#prime_division e\$B\$He(B Prime

## e\$B\$bF~\$l\$F\$b\$i\$(\$^\$;\$s\$+!#e(B

e\$B\$J\$+\$@\$G\$9!#e(B

At Tue, 19 Aug 2008 12:43:04 +0900,
keiju ISHITSUKA wrote in [ruby-dev:35875]:

e\$B;d\$H\$7\$F\$O!"\$=\$N5!G=\$rH4\$1\$PAG?tA4BN\$OM#0l\$G\$"\$k\$Y\$-\$G\$"\$k\$H9M\$(\$^\$9!#e(B

e\$B\$H\$\$\$&\$3\$H\$Ge(B, e\$B\$3\$&\$\$\$&\$H\$-\$K\$Oe(B singleton e\$B%Q%?!<%s\$rE,MQ\$9\$k\$N\$,\$U\$5\$oe(B
e\$B\$7\$\$\$H;W\$&\$N\$G\$9\$,e(B, e\$B\$\$\$+\$,\$G\$7\$g\$&e(B?

e\$B\$=\$&\$9\$l\$Pe(B, e\$BM#0l@-\$bJ]\$?\$l\$D\$De(B, e\$B\$A\$c\$s\$H%\$%s%9%?%s%9\$G<B8=\$G\$-\$^\$9e(B.

Prime.instance.eache\$B\$H\$\$\$&\$N\$O>iD9\$K46\$8\$^\$9!#\$I\$&\$;\$J\$i\$=\$N%\$%se(B
e\$B%9%?%s%9<+BN\$re(BPrimee\$B\$H\$\$\$&Dj?t\$K\$7\$F\$O\$I\$&\$G\$7\$g\$&\$+!#e(B

e\$B\$G\$Oe(B, e\$B?70FBT\$C\$F\$\$\$^\$9e(B(^^;

Prime.eache\$B\$rDI2C\$9\$k\$@\$1\$G\$\$\$\$\$h\$&\$J5\$\$b\$7\$J\$/\$O\$J\$\$\$G\$9!#e(B

def Prime.each(&block)
ps = new
if block
ps.each(&block)
else
ps
end
end

e\$B\$1\$\$\$8\$e!w\$\$\$7\$D\$+\$G\$9e(B.

In [ruby-dev :35877 ] the message: "[ruby-dev:35877] Re: Refactoring
of enumerating prime numbers ", on Aug/19 15:21(JST) Nobuyoshi N.
writes:

e\$B\$J\$+\$@\$G\$9!#e(B

Prime.instance.eache\$B\$H\$\$\$&\$N\$O>iD9\$K46\$8\$^\$9!#\$I\$&\$;\$J\$i\$=\$N%\$%se(B
e\$B%9%?%s%9<+BN\$re(BPrimee\$B\$H\$\$\$&Dj?t\$K\$7\$F\$O\$I\$&\$G\$7\$g\$&\$+!#e(B

e\$B;d\$b\$=\$&\$J\$N\$+\$J\$!e(B? e\$B\$H;W\$\$\$D\$D\$be(B, e\$B\$G\$Oe(B,
e\$B85\$N%/%i%9\$NL>A0\$O\$I\$&\$9\$k\$s\$@e(B?
e\$B\$H;W\$C\$?\$j\$b\$7\$F\$^\$9e(B.

Prime.eache\$B\$rDI2C\$9\$k\$@\$1\$G\$\$\$\$\$h\$&\$J5\$\$b\$7\$J\$/\$O\$J\$\$\$G\$9!#e(B

def Prime.each(&block)
ps = new
if block
ps.each(&block)
else
ps
end
end

e\$B\$3\$l\$C\$Fe(B, Yugui e\$B\$5\$s\$NBh#20Fe(B:

+class Prime

• class << self
• include Enumerable
• # Return the prime cache.

• def cache
• `````` Generator.cache
``````
• end
• alias primes cache
• alias primes_so_far cache
• def each(&proc)
• `````` Generator.new.each(&proc)
``````
• end
• end

e\$B\$H6a\$\$\$G\$9\$,e(B, include Enumerable e\$B\$,F~\$C\$F\$\$\$^\$;\$se(B. Enuerable
e\$B\$G\$"\$kI,e(B
e\$BMW\$O\$J\$/e(B each e\$B\$@\$1\$"\$l\$P\$h\$\$\$C\$F\$3\$H\$G\$9e(B?

e\$B\$"\$He(B, e\$BJL7o\$G5\$\$K\$J\$C\$F\$\$\$k\$3\$H\$,e(B…
e\$B8=9T\$G\$b\$=\$&\$G\$9\$,e(B, e\$B7k9==EMW\$J%/e(B
e\$B%i%9JQ?t\$N;H\$o\$lJ}\$r\$7\$F\$\$\$^\$9e(B.
e\$B\$3\$l\$i\$N%/%i%9JQ?t\$NAH\$r35G0E*\$K<h\$j\$^e(B
e\$B\$H\$a\$?%/%i%9\$H\$\$\$&\$b\$N\$b9M\$(\$i\$l\$=\$&\$H8@\$&5\$\$,\$7\$F\$\$\$^\$9e(B.

e\$B\$^\$?e(B, e\$BHyL/\$K%9%l%C%I%;!<%U\$G\$J\$\$5\$\$b\$7\$?\$j\$b\$7\$F\$\$\$k\$N\$Ge(B,
e\$B\$=\$3\$b2r>C\$Ge(B
e\$B\$-\$l\$P\$J\$!e(B. e\$B\$J\$I\$H;W\$C\$?\$j\$7\$F\$\$\$^\$9e(B

__
---------------------------------------------------->> e\$B@PDMe(B
e\$B7=<ye(B <<—
---------------------------------->> e-mail: [email protected] <<—

e\$B\$1\$\$\$8\$e!w\$\$\$7\$D\$+\$G\$9e(B.

e\$B2?\$+%a%\$%k\$,\$?\$^\$C\$F\$7\$^\$C\$Fe(B…
e\$B7h\$7\$FK:\$l\$F\$\$\$k\$o\$1\$G\$O\$"\$j\$^\$;\$s\$N\$Ge(B, > e\$BBT\$C\$F\$\$\$kJ}e(B

In [ruby-dev :35878 ] the message: "[ruby-dev:35878] Re: Refactoring
of enumerating prime numbers ", on Aug/19 17:39(JST) TOYOFUKU
Chikanobu writes:

e\$BK-J!\$G\$9!#e(B

e\$B\$4L5:;BA\$7\$F\$\$\$^\$9e(B.

e\$B\$3\$N5!2q\$Ke(B
[ruby-dev:30932] Re: Integer#prime_division e\$B\$He(B Prime
[ruby-dev:30937] Re: Integer#prime_division e\$B\$He(B Prime

e\$B\$bF~\$l\$F\$b\$i\$(\$^\$;\$s\$+!#e(B

e\$B\$*!<e(B. e\$B\$9\$C\$+\$jK:\$l\$F\$\$\$^\$7\$?e(B.

e\$B\$7\$+\$7e(B,
e\$B\$Me(B(^^;

e\$B\$G\$be(B, e\$B\$3\$l\$O7k9==EMW\$J\$3\$H\$b8@\$C\$F\$\$\$k5\$\$,\$7\$^\$9\$Me(B.
e\$BNc\$(\$Pe(B, e\$BAG?t\$+\$I\$&e(B
e\$B\$+H=Dj\$9\$k%a%=%C%I\$b\$3\$A\$i\$NJ}K!\$r;H\$C\$?J}\$,\$h\$\$\$H8@\$C\$F\$\$\$^\$9\$7e(B…

__
---------------------------------------------------->> e\$B@PDMe(B
e\$B7=<ye(B <<—
---------------------------------->> e-mail: [email protected] <<—

e\$B\$J\$+\$@\$G\$9!#e(B

At Wed, 20 Aug 2008 11:52:55 +0900,
e\$B@PDM7=<ye(B wrote in [ruby-dev:35882]:

Prime.instance.eache\$B\$H\$\$\$&\$N\$O>iD9\$K46\$8\$^\$9!#\$I\$&\$;\$J\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\$B\$=\$l\$,e(BPrimee\$B<+?H\$NLr3d\$J\$s\$8\$c\$J\$\$\$+\$J\$!\$H!#e(B

e\$B\$^\$?e(B, e\$BHyL/\$K%9%l%C%I%;!<%U\$G\$J\$\$5\$\$b\$7\$?\$j\$b\$7\$F\$\$\$k\$N\$Ge(B, e\$B\$=\$3\$b2r>C\$Ge(B
e\$B\$-\$l\$P\$J\$!e(B. e\$B\$J\$I\$H;W\$C\$?\$j\$7\$F\$\$\$^\$9e(B

e\$B\$=\$3\$OLdBj\$G\$9\$M!#e(BPrime#succe\$B\$N%k!<%W\$re(Bsynchronizee\$B\$5\$;\$l\$P\$\$\$\$\$Ge(B
e\$B\$7\$g\$&\$+!#e(B

e\$B\$R\$H\$^\$:!"e(BPrimee\$B4X78\$rJL%U%!%\$%k\$K@Z\$jJ,\$1\$k\$H\$3\$m\$^\$G\$d\$C\$F\$7\$^\$Ce(B
e\$B\$F\$O\$I\$&\$G\$7\$g\$&\$+!#e(B

# Index: lib/mathn.rb

— lib/mathn.rb (revision 18717)
+++ lib/mathn.rb (working copy)
@@ -60,4 +60,5 @@ class Prime
# n < Math.sqrt(@@next_to_check)
})
@@ulticheck_next_squared = 121 # @@primes[@@ulticheck_index + 1] **
2

• @@lock = Mutex.new

class << self
@@ -82,4 +83,6 @@ class Prime
def succ
@index += 1

• if @index >= @@primes.length

• `````` @@lock.synchronize do
``````

while @index >= @@primes.length
# Only check for prime factors up to the square root of the
potential primes,
@@ -97,4 +100,6 @@ class Prime
@@next_to_check += 2
end

• `````` end
``````
• end
return @@primes[@index]
end

Yuguiã§ã™ã€‚

keiju ISHITSUKA ã•ã‚“ã¯æ›¸ãã¾ã—ãŸ:

ã§ã¯, æ–°æ¡ˆå¾…ã£ã¦ã„ã¾ã™(^^;

æ–°ã—ã„æ¡ˆã‚’æ›¸ã„ã¦ã¿ã¾ã—ãŸã€‚ãƒ‘ãƒƒãƒã‚’æ·»ä»˜ã—ã¾ã™ã€‚

2. Prime.each ã¯ Prime::Generator ã‚ªãƒ–ã‚¸ã‚§ã‚¯ãƒˆã‚’å‘¼ã³å‡ºã—ã¦åˆ—æŒ™
3. (optional) Integer.each_primeã¯Prime.eachã«è»¢é€

â†‘ã“ã®ã‚ãŸã‚Šã¯ç¬¬2æ¡ˆã®ã¾ã¾ã§ã™ã€‚ãã—ã¦ã€

• ãƒ•ã‚¡ã‚¤ãƒ«åã‚’lib/prime.rbã«ã—ã¾ã—ãŸ

• Prime.eachãŒã‚ã‚Šã¾ã™ãŒã€

ã§ã™. ã¤ã¾ã‚Š, ç´ æ•°å…¨ä½“ã‚’è¡¨ã™ã‚‚ã®ã‚‚ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ã§ã‚ã£ã¦æ¬²ã—ã„ã§ã™.

ã‚’å—ã‘ã¦ã€ä¸­ç”°ã•ã‚“æ¡ˆã®ã‚ˆã†ã«ç‰¹ç•°ãƒ¡ã‚½ãƒƒãƒ‰ã‚’æŒã¤ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ã«ã—ã¾ã—ãŸã€‚

• äº’æ›æ€§ã®ãŸã‚Prime.newãŒå¯èƒ½ã§ã€ã“ã‚Œã¯å¤–éƒ¨ã‚¤ãƒ†ãƒ¬ãƒ¼ã‚¿ã‚’è¿”ã—ã¾ã™ã€‚ä¸”ã¤ã€
Prime === Prime.new ã§ã™ã€‚

• ä¸­ç”°ã•ã‚“ã®ãƒ‘ãƒƒãƒã‚’å–ã‚Šè¾¼ã‚“ã§ã€ç´ æ•°è¡¨ã®æ‹¡å¤§ã‚’ã‚¹ãƒ¬ãƒƒãƒ‰ã‚»ãƒ¼ãƒ•ã«ã—ã¾ã—ãŸã€‚

• ã¤ã„ã§ã«test/test_prime.rb ã‚’è¶³ã—ã¾ã—ãŸã€‚

ã”æ¤œè¨Žã‚’ã€ä½•å’ã‚ˆã‚ã—ããŠé¡˜ã„ã—ã¾ã™ã€‚

e\$B\$1\$\$\$8\$e!w\$\$\$7\$D\$+\$G\$9e(B.

e\$B\$J\$s\$+e(B, e\$BAw\$C\$?%a%\$%k\$,%9%Q%`07\$\$\$K\$J\$C\$?\$h\$&\$J\$N\$Ge(B,
e\$B:FAw\$7\$^\$9e(B.

yuguie\$B\$5\$s\$N\$H\$O0U?^\$7\$F\$\$\$k\$H\$3\$m\$OBgBNF1\$8\$@\$H;W\$\$\$^\$9\$,e(B…

In [ruby-dev :35883 ] the message: "[ruby-dev:35883] Re: Refactoring
of enumerating prime numbers ", on Aug/20 12:13(JST) Nobuyoshi N.
writes:

e\$B\$J\$+\$@\$G\$9!#e(B

e\$B;d\$b\$=\$&\$J\$N\$+\$J\$!e(B? e\$B\$H;W\$\$\$D\$D\$be(B, e\$B\$G\$Oe(B, e\$B85\$N%/%i%9\$NL>A0\$O\$I\$&\$9\$k\$s\$@e(B?
e\$B\$H;W\$C\$?\$j\$b\$7\$F\$^\$9e(B.

class << (Prime = Object.new); end e\$B\$G!#e(B

e\$B\$=\$l\$b9M\$(\$?\$s\$G\$9\$,e(B… Generator e\$B\$HMm\$_\$^\$9\$N\$Ge(B,
e\$B\$=\$A\$i\$Ge(B.

Enumerablee\$B\$OF~\$lK:\$l\$G\$9\$,!"e(BGeneratore\$B\$N\$h\$&\$KJL%/%i%9\$r2p\$9\$kI,e(B
e\$BMW\$O\$J\$\$\$N\$G\$O\$J\$\$\$+\$H\$\$\$&\$3\$H\$G\$9!#e(B

Generatore\$B\$,0l<oN`\$J\$i\$=\$l\$O8@\$(\$k\$H;W\$&\$N\$G\$9\$,e(B,
e\$BK-J!\$5\$s\$N%a%\$%k\$N\$he(B
e\$B\$&\$Ke(B,e\$BC1D4A}2C\$G>/\$J\$/\$H\$b\$9\$Y\$F\$NAG?t\$,4^\$^\$l\$k?tNs\$rMQ\$\$\$kJ}\$,NI\$\$>le(B
e\$B9g\$b\$"\$k\$N\$Ge(B, Generator e\$B\$Oe(B
e\$BJL\$K\$"\$C\$?J}\$,\$h\$\$5\$\$,\$7\$F\$\$\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.
e\$BL>A0\$NM3Mhe(B: 2e\$B\$He(B3e\$B\$NG?t0J30\$N?tCM\$J\$N\$Ge(B.

• Prime::IncreaseSuperPrimeGenerator
e\$B>e5-e(B2e\$B\$D\$Ne(Bgeneratore\$B\$NCj>]%9!<%Q!<%/%i%9e(B
e\$BL>A0\$O\$\$\$^\$\$\$A\$J\$s\$G\$9\$,e(B, e\$B>:=g\$Ge(B,
e\$BAG?t=89g\$N%9!<%Q!<%;%C%H\$K\$J\$ke(B
generator e\$B\$H\$\$\$&\$3\$H\$m\$+\$i\$H\$C\$F\$\$\$^\$9e(B.
e\$B\$h\$\$L>A0Jg=8Cfe(B

• Prime::PrimeSet
e\$BAG?t@8@.\$NK\BNe(B
singletone\$B\$K\$9\$k\$3\$H\$K\$h\$C\$Fe(B, e\$B%/%i%9JQ?t\$r\$J\$/\$7\$F\$\$\$^\$9e(B

• Integer e\$B\$KDI2C\$5\$l\$k%a%=%C%Ie(B
e\$B<BAu\$Oe(B, Primee\$B\$NJ}\$K0\F0e(B

• Prime#prime_division. Prime.prime?
generator e\$B\$,;XDj\$G\$-\$k\$h\$&\$K\$7\$F\$"\$j\$^\$9e(B.
e\$BC1H/\$K;H\$&\$J\$iK-J!\$5\$s\$NJ}e(B
e\$B\$,Aa\$\$\$H;W\$\$\$^\$9\$,e(B, e\$B%-%c%C%7%e\$N8z2L\$,\$G\$k\$3\$H\$b\$"\$k\$N\$Ge(B,
e\$B;H\$\$J}\$K\$h\$Ce(B
e\$B\$FJQ\$(\$i\$l\$k\$h\$&\$K\$H8@\$&0U?^\$G\$9e(B

e\$B\$s\$Ge(B, e\$B%3!<%I\$O0J2<\$N46\$8\$K\$J\$C\$F\$\$\$^\$9e(B.

e\$B\$40U8+Jg=8Cfe(B!!

– prime.rb
require “singleton”
require “forwardable”

class Integer

def Integer.from_prime_division(pd)
Prime.int_from_prime_division(pd)
end

def prime_division(generator = Prime::Generator23.new)
Prime.prime_division(self, generator)
end

def prime?
Prime.prime?(self)
end
end

class Prime
include Singleton
include Enumerable

class<<self
extend Forwardable
include Enumerable

``````def method_added(method)
(class<<self;self;end).def_delegator :instance, method
end
``````

end

def each(&block)
Generator.new.each(&block)
end

def prime?(value, generator = Prime::Generator23.new)
for num in generator
return false if value % num == 0
return true if value > num * num
end
end

def int_from_prime_division(pd)
pd.inject(1){|value, (prime, index)|
value *= prime**index
}
end

def prime_division(value, generator= Prime::Generator23.new)
raise ZeroDivisionError if self == 0
pv = []
for prime in generator
count = 0
while (value1, mod = value.divmod(prime)
mod) == 0
value = value1
count += 1
end
if count != 0
pv.push [prime, count]
end
break if prime * prime >= value
end
if value > 1
pv.push [value, 1]
end
return pv
end

class IncreaseSuperPrimeGenerator
include Enumerable

``````def succ
raise NotImplementedError, "need to define `succ'"
end

def each(&block)
return self unless block
loop do
``````

block.call succ
end
end

end
ISPG = IncreaseSuperPrimeGenerator

class Generator<ISPG
def initialize
@index = -1
end

``````def succ
PrimeSet.instance[@index += 1]
end
alias next succ
``````

end

class Generator23<ISPG
def initialize
@prime = 1
@step = nil
end

``````def succ
loop do
``````

if (@step)
@prime += @step
@step = 6 - @step
else
case @prime
when 1; @prime = 2
when 2; @prime = 3
when 3; @prime = 5; @step = 2
end
end
return @prime
end

``````end
``````

end

class PrimeSet
include Singleton

``````def initialize
# These are included as class variables to cache them for later
``````

uses. If memory
# usage is a problem, they can be put in Prime#initialize as
instance variables.

``````  # There must be no primes between @primes[-1] and @next_to_check.
@primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
``````

53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
# @next_to_check % 6 must be 1.
@next_to_check = 103 # @primes[-1] - @primes[-1] % 6 +
7
@ulticheck_index = 3 #
@primes.index(@primes.reverse.find {|n|
# n < Math.sqrt(@@next_to_check) })
@ulticheck_next_squared = 121 # @primes[@ulticheck_index + 1] **
2
end

``````# Return the prime cache.
def cache
return @primes
end
alias primes cache
alias primes_so_far cache

def [](index)
while index >= @primes.length
``````

primes,

# but without the performance hit of an actual square root

calculation.
if @next_to_check + 4 > @ulticheck_next_squared
@ulticheck_index += 1
@ulticheck_next_squared = @primes.at(@ulticheck_index + 1) ** 2
end

checking against

# two and three.

@primes.push @next_to_check if @primes[2…@ulticheck_index].find
{|prime| @next_to_check % prime == 0 }.nil?
@next_to_check += 4
@primes.push @next_to_check if @primes[2…@ulticheck_index].find
{|prime| @next_to_check % prime == 0 }.nil?
@next_to_check += 2
end
return @primes[index]
end
end
end

__
---------------------------------------------------->> e\$B@PDMe(B
e\$B7=<ye(B <<—
---------------------------------->> e-mail: [email protected] <<—

Yuguiã§ã™ã€‚

çŸ³å¡šåœ­æ¨¹ ã•ã‚“ã¯æ›¸ãã¾ã—ãŸ:

ã¨æ€ã£ãŸã‚Šã‚‚ã—ã¦ã¾ã™.
class << (Prime = Object.new); end ã§ã€‚

ç§ãŒé€ã£ãŸã‚³ãƒ¼ãƒ‰ã‚‚æ—¢å­˜ã®Prime.newã—ã¦ã„ã‚‹ã‚³ãƒ¼ãƒ‰ã«é…æ…®ã—ã¦ã¯ã„ã¾ã™ãŒã€
è‰¯ã„ã‹ã‚‚ã—ã‚Œã¾ã›ã‚“ã€‚

ãŸã ã€ãã®æ„å‘³ã§ã¯çŸ³å¡šã•ã‚“ã®ã€Primeã‚’å®Œå…¨ã«ã‚·ãƒ³ã‚°ãƒ«ãƒˆãƒ³ãƒ‘ã‚¿ãƒ¼ãƒ³ã«ã—ã¦ã—
ã¾ã†ã®ã¯Prime.newã§ããªã„ã®ã§å›°ã‚‹ã¨æ€ã„ã¾ã™ã€‚

ã‚‚ã†ä¸€åº¦æ•´ç†ã—ã¾ã™ã€‚

1. ç´ æ•°å…¨ä½“ã‚’è¡¨ã™Enumerableãªã‚ªãƒ–ã‚¸ã‚§ã‚¯ãƒˆã¯å”¯ä¸€ã§ã‚ã‚‹ã¹ã(yugui)
äº’æ›æ€§ã®ãŸã‚ã«Prime.newã§ãã‚‹ã®ã¯ä»•æ–¹ãŒãªã„ãŒã€éžæŽ¨å¥¨ã¨ã—ãŸã„

2. ç´ æ•°å…¨ä½“ã‚’è¡¨ã™ã‚‚ã®ã¯ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ã§ã‚ã‚‹ã¹ã(çŸ³å¡šã•ã‚“)
Primeã¸ã®ãƒ¡ã‚½ãƒƒãƒ‰å‘¼ã³å‡ºã—ã‚’Primeã®ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ã¸å§”è­²ã™ã‚‹ãªã‚‰ã°ã‚ã‚Šãˆã‚‹ã€‚

3. ç´ æ•°åˆ—æŒ™ã®é€”ä¸­ã®çŠ¶æ…‹ã‚’ä¿æŒã™ã‚‹å¤–éƒ¨ã‚¤ãƒ†ãƒ¬ãƒ¼ã‚¿ãŒå¿…è¦

4. ç–‘ä¼¼ç´ æ•°åˆ—ã‚’ç”Ÿæˆã™ã‚‹Generatorã‚‚ã»ã—ã„
=> Prime::Generator, Prime::Generator23, Prime::EratosthenesGenerator

Prime::*ã®å®šæ•°ã‚¹ã‚³ãƒ¼ãƒ—ã‚’æä¾›ã™ã‚‹ãŸã‚ã«ã‚‚Primeã¯ã€ç§ãŒæ›¸ã„ãŸã‚ˆã†ãª
singletonãªã‚ªãƒ–ã‚¸ã‚§ã‚¯ãƒˆã‚ˆã‚Šã¯ã€çŸ³å¡šã•ã‚“ã®ã‚ˆã†ãªsingletonãƒ‘ã‚¿ãƒ¼ãƒ³ã®ã»ã†ãŒ
è‰¯ã•ãã†ã«æ€ãˆã¾ã™ã€‚

ãŸã ã—ã€äº’æ›æ€§ã®ãŸã‚ã«Prime.newã‚‚è¨±å®¹ã™ã‚‹ã‚ˆã†ã«ã—ã¦ã¿ã¾ã—ãŸã€‚ã“ã®ãƒãƒ¼
ã‚¸ãƒ§ãƒ³ã®prime.rbã‚’æ·»ä»˜ã—ã¾ã™ã€‚

ã“ã®ä»–ã€
Prime::EratosthenesGeneratorã‚’åŠ ãˆã¦ã‚ã‚Šã¾ã™ã€‚

``````  user     system      total        real
``````

trial division
17.060000 22.580000 39.640000 ( 39.556764)
eratosthenes
2.270000 0.010000 2.280000 ( 2.277945)

b) Prime.eachãŒä¸Šç•Œã‚’ã¨ã‚‹ã‚ˆã†ã«ã—ã¦ã¿ã¾ã—ãŸã€‚ãã“ã«åˆ°é”ã—ãŸã‚‰åˆ—æŒ™ã‚’æ‰“ã¡
åˆ‡ã‚Šã¾ã™ã€‚
c) Prime.eachã®ç¬¬2å¼•æ•°ã«generatorã‚’æŒ‡å®šã§ãã‚‹ã‚ˆã†ã«ã—ã¦ã¿ã¾ã—ãŸã€‚

d) Prime.eachã¨Prime.each.eachãŒåŒã˜ã‚ªãƒ–ã‚¸ã‚§ã‚¯ãƒˆã§ã‚ã‚‹ãŸã‚ã«åˆ—æŒ™çŠ¶æ…‹ã‚’å…±
æœ‰ã—ã¦ã—ã¾ã„ã¾ã™ã€‚generatorãŒselfã§ã¯ãªãself.dupã‚’è¿”ã™ã‚ˆã†ã«ã—ã¾ã—ãŸã€‚

e) Enumeratorã«åˆã‚ã›ã¦ã€Generatorã«with_indexã¨nextã¨rewindã‚’è¶³ã—ã¾ã—ãŸã€‚

• Prime::IncreaseSuperPrimeGenerator
åå‰ã¯ã„ã¾ã„ã¡ãªã‚“ã§ã™ãŒ, æ˜‡é †ã§, ç´ æ•°é›†åˆã®ã‚¹ãƒ¼ãƒ‘ãƒ¼ã‚»ãƒƒãƒˆã«ãªã‚‹
generator ã¨ã„ã†ã“ã¨ã‚ã‹ã‚‰ã¨ã£ã¦ã„ã¾ã™.
ã‚ˆã„åå‰å‹Ÿé›†ä¸­

ç¢ºã‹ã«ã“ã®åå‰ã«ã¯é•å’Œæ„ŸãŒã‚ã‚Šã¾ã™ã­ã€‚

ä¼¼ã€ã®ç²¾åº¦ãŒæ‚ªãã¦ã‚‚ã€é€†ã«çœŸã«primeã§ã‚ã£ã¦ã‚‚Pseudoã®ã‚µãƒ–ã‚»ãƒƒãƒˆã«ã¯ãªã‚‹
ã¨æ€ã„ã¾ã™ã€‚

Yuguiã§ã™ã€‚

keiju ISHITSUKA ã•ã‚“ã¯æ›¸ãã¾ã—ãŸ:

ä½•ã‚«æ‰€ã‹å¤‰æ›´å€‹æ‰€ãŒã‚ã‚Šã¾ã—ãŸãŒ, ã„ã‹ãŒã§ã—ã‚‡ã†?

ã‚‚ã—, å•é¡Œãªã„ã‚ˆã†ã§ã‚ã‚Œã°, ã™ã„ã¾ã›ã‚“ãŒ, ãã‚Œã‚’å¤‰æ›´ã—ã¦comit ã—ã¦ã„ãŸ
ã ã‘ãªã„ã§ã™?

ã‚ã‚ŠãŒã¨ã†ã”ã–ã„ã¾ã™ã€‚ã‚³ãƒŸãƒƒãƒˆã—ã‚ˆã†ã‹ã¨æ€ã„ã¾ã—ãŸãŒãƒ†ã‚¹ãƒˆã‚’æ›¸ã„ã¦ã„ãŸã‚‰
2ç‚¹å•é¡Œã«æ°—ã¥ãã¾ã—ãŸã€‚çµæžœã¨ã—ã¦ã¡ã‚‡ã£ã¨å¤‰æ›´ãŒå¿…è¦ã«ãªã‚Šãã†ã§ã™ã®ã§ã€
æã‚Œå…¥ã‚Šã¾ã™ãŒå†åº¦ç¢ºèªã‚’ãŠé¡˜ã„ã—ã¾ã™ã€‚

å…ˆã«ãŠé€ã‚Šã—ãŸprime.rbã§ã¯2ç‚¹å•é¡ŒãŒã‚ã‚Šã¾ã—ãŸã€‚

1. Prime.eachãŒè¿”ã™enumerator(å®Ÿéš›ã«ã¯generator)ãŒã€uboundã‚’è¨˜æ†¶ã—ãªã„ã€‚
Prime.each(100){|p| }ã¯åœæ­¢ã™ã‚‹ã®ã«ã€Prime.each(100).each{|p| }ã¯åœæ­¢
ã—ãªã„ã¨ã„ã†å¥‡å¦™ãªçµæžœã«ãªã‚Šã¾ã™ã€‚

2. Prime#nextãŒãªã„ã€‚
äº’æ›æ€§ã«ã“ã ã‚ã£ã¦è¦‹ã›ãŸå‰²ã«ã†ã£ã‹ã‚ŠPrime#nextã®å®Ÿè£…ã‚’æ›¸ãå¿˜ã‚Œã¦ã„ã¾ã—ãŸã€‚
ã¾ãŸã€å…ˆã®ãƒãƒ¼ã‚¸ãƒ§ãƒ³ã‚’å…ƒã«å˜ç´”ã«Prime#nextã‚’å®Ÿè£…ã™ã‚‹ã¨Prime#nextã«ã‚ˆã£
ã¦é€²ã‚“ã å†…éƒ¨ãƒã‚¤ãƒ³ã‚¿ã¨Prime#eachãŒåŒæœŸã—ã¾ã›ã‚“ã€‚Prime#eachã¯å…ˆç«‹ã¤
Prime#nextã«é–¢ã‚ã‚‰ãš2ã‹ã‚‰åˆ—æŒ™ã‚’é–‹å§‹ã—ã¦ã—ã¾ã„ã¾ã™ã€‚
ä¸€æ–¹ã€Prime::eachã‚„Prime::instance.eachã®ã»ã†ã¯å…ˆç«‹ã¤Primeã®ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³
ã‚¹ãƒ¡ã‚½ãƒƒãƒ‰å‘¼ã³å‡ºã—ã«ã¯å½±éŸ¿ã•ã‚Œãšã«2ã‹ã‚‰åˆ—æŒ™ã™ã‚‹ã“ã¨ã‚’æœŸå¾…ã•ã‚Œã‚‹ã¨æ€ã„ã¾ã™ã€‚

ã—ãŸã€‚æ–°ã—ã„prime.rbã‚’æ·»ä»˜ã—ã¾ã™ã€‚ã“ã‚Œã§å·®ã—æ”¯ãˆãªã„ã‚ˆã†ã§ã—ãŸã‚‰Primeã‚’
å‰Šé™¤ã—ãŸãƒãƒ¼ã‚¸ãƒ§ãƒ³ã®mathn.rbã¨ä½µã›ã¦ã‚³ãƒŸãƒƒãƒˆã—ã‚ˆã†ã¨æ€ã„ã¾ã™ã€‚ã„ã‹ãŒã§
ã—ã‚‡ã†ã‹?

e\$BK-J!\$G\$9!#e(B

In [ruby-dev :36067]

e\$B?7\$7\$\$e(Bprime.rbe\$B\$rE:IU\$7\$^\$9!#e(B

[ruby-dev:30937] Re: Integer#prime_division e\$B\$He(B Prime
e\$B\$K=q\$\$\$?\$5\$5\$d\$+\$J:GE,2=\$r<h\$j9~\$s\$G\$b\$i\$(\$?\$i\$H;W\$\$\$^\$9!#e(B
divmod e\$B\$He(B % e\$B\$N7W;;%3%9%H\$O\$[\$\F1\$8\$G\$9\$h\$M!#e(B

prime.rb.org
+++ prime.rb
@@ -87,8 +87,9 @@

# +generator+:: optional. A pseudo-prime generator.

def prime?(value, generator = Prime::Generator23.new)
for num in generator

• `````` return true if value < num * num
``````
• `````` return false if value % num == 0
``````
• `````` q,r = divmod value num
``````
• `````` return false if r == 0
``````
• `````` return true if q <= num
``````
end
end

@@ -135,7 +136,7 @@
if count != 0
pv.push [prime, count]
end

• `````` break if prime * prime  >= value
``````
• `````` break if value1 <= prime
``````
end
if value > 1
pv.push [value, 1]

e\$B\$1\$\$\$8\$e!w\$\$\$7\$D\$+\$G\$9e(B.

In [ruby-dev :35983 ] the message: "[ruby-dev:35983] Re: Refactoring
of enumerating prime numbers ", on Aug/26 22:09(JST) “Yugui (Yuki
Sonoda)” writes:

Yuguie\$B\$G\$9!#e(B

e\$BCY%a%\$%k\$G\$9\$\$\$^\$;\$se(B.

e\$B;d\$,Aw\$C\$?%3!<%I\$b4{B8\$Ne(BPrime.newe\$B\$7\$F\$\$\$k%3!<%I\$KG[N8\$7\$F\$O\$\$\$^\$9\$,!"e(B
e\$B8_49@-\$r\$h\$j3N<B\$KDs6!\$9\$k\$H\$\$\$&0UL#\$G\$Oe(BPrimee\$B\$,%/%i%9\$GM-\$jB3\$1\$?\$[\$&\$,e(B
e\$BNI\$\$\$+\$b\$7\$l\$^\$;\$s!#e(B

e\$B\$"\$H\$+\$i\$G\$F\$-\$^\$9\$,e(B, Generatore\$B\$r30\$+\$i8F\$Y\$J\$/\$J\$j\$^\$9\$7\$Me(B.

e\$B\$?\$@!"\$=\$N0UL#\$G\$O@PDM\$5\$s\$N!"e(BPrimee\$B\$r40A4\$K%7%s%0%k%H%s%Q%?!<%s\$K\$7\$F\$7e(B
e\$B\$^\$&\$N\$Oe(BPrime.newe\$B\$G\$-\$J\$\$\$N\$G:\$\$k\$H;W\$\$\$^\$9!#e(B

e\$B\$^\$"e(B, 1.9e\$BMQ\$@\$+\$iNI\$\$\$+\$H;W\$C\$Fe(B(^^;;

1. e\$B5?;wAG?tNs\$r@8@.\$9\$ke(BGeneratore\$B\$b\$[\$7\$\$e(B
=> Prime::Generator, Prime::Generator23, Prime::EratosthenesGenerator

e\$B\$O\$\$e(B.

Prime::e\$B\$NDj?t%9%3!<%W\$rDs6!\$9\$k\$?\$a\$K\$be(BPrimee\$B\$O!";d\$,=q\$\$\$?\$h\$&\$Je(B
singletone\$B\$J%
%V%8%’%/%H\$h\$j\$O!"@PDM\$5\$s\$N\$h\$&\$Je(Bsingletone\$B%Q%?!<%s\$N\$[\$&\$,e(B
e\$BNI\$5\$=\$&\$K;W\$(\$^\$9!#e(B

e\$B\$G\$9\$Me(B.

e\$B\$?\$@\$7!"8_49@-\$N\$?\$a\$Ke(BPrime.newe\$B\$b5vMF\$9\$k\$h\$&\$K\$7\$F\$_\$^\$7\$?!#\$3\$N%P!<e(B
e\$B%8%g%s\$Ne(Bprime.rbe\$B\$rE:IU\$7\$^\$9!#e(B

e\$B\$3\$l\$Ge(B, e\$B\$\$\$\$\$s\$8\$c\$J\$\$\$G\$7\$g\$&\$+e(B.

e\$B\$3\$NB>!"e(B
a) e\$B%(%i%H%9%F%M%9\$N\$U\$k\$\$\$Ge(BGeneratore\$B\$r=q\$\$\$F\$_\$?\$i\$H\$F\$bB.\$/\$J\$C\$?\$N\$G!“e(B
Prime::EratosthenesGeneratore\$B\$r2C\$(\$F\$”\$j\$^\$9!#e(B

`````` user     system      total        real
``````

trial division
17.060000 22.580000 39.640000 ( 39.556764)
eratosthenes
2.270000 0.010000 2.280000 ( 2.277945)

e\$B\$*!<e(B.

(2**19-1)**2 e\$B\$NAG0x?tJ,2r\$G\$be(B:

Generator23 0.450000 0.000000 0.450000 ( 0.468806)
Generator 3.680000 0.430000 4.110000 ( 4.145663)
Eratos 0.620000 0.000000 0.620000 ( 0.635689)

e\$B\$1\$C\$3\$&NI\$\$CM\$,\$G\$F\$\$\$^\$9e(B.

e\$B5\$\$K\$J\$k\$N\$,\$I\$N\$/\$i\$\$%a%b%j\$r>CHq\$9\$k\$+\$G\$9e(B. n
e\$B0J2<\$N\$NAG?t\$G9M\$(\$k\$He(B:

Generator: e\$BLse(B n/lon(n) * 4 byte
Eratos: n/32 byte

e\$B\$K\$J\$j\$^\$9e(B. e\$B%*!<%@!<\$Oe(BGeneratore\$B\$NJ}\$,NI\$\$\$G\$9e(B.
e\$B\$?\$@e(B, e\$B:G=i\$N\$&\$A\$Oe(B
Eratos e\$B\$NJ}\$,6u4V8zN(\$ONI\$\$\$G\$9e(B,
Generatore\$B\$NJ}\$,%a%b%j\$r;H\$o\$J\$/\$J\$ke(B n
e\$B\$r5a\$a\$k\$He(B

n = 3.8*10**55

e\$B\$H\$J\$j\$^\$9e(B. e\$B\$=\$N;~\$N;HMQ%P%\$%H?t\$Oe(B 10**54
bytee\$B\$0\$i\$\$\$K\$J\$j\$^\$9e(B. e\$B\$=\$s\$Je(B
e\$B\$K%a%b%j\$J\$\$\$N\$Ge(B, e\$B8=<BE*\$JHO0O\$G\$Oe(B
Eratose\$B\$NJ}\$,6u4V;HMQNL\$O>/\$J\$\$\$H\$\$e(B
e\$B\$(\$^\$9e(B.

# e\$B8!;;5a\$`e(B(^^;

e\$B\$H\$\$\$&\$3\$H\$Ge(B, Prime#each e\$B\$Ne(B e\$B%G%U%)%k%H\$Ne(B Generator
e\$B\$re(B
EratosthenesGenerator e\$B\$K\$7\$^\$7\$g\$&e(B.

e\$B\$"\$He(B,

Generator e\$B\$re(B TrialDivisionGenerator
PrimeSet e\$B\$re(B TrialDivision

e\$B\$K2~L>\$7\$^\$7\$g\$&\$+e(B? e\$B\$=\$l\$He(B, e\$B7G:=gHV\$re(B Eratosthenes*
e\$B\$r@h\$K\$7\$^\$7\$g\$&e(B
e\$B\$+e(B?

b) Prime.eache\$B\$,>e3&\$r\$H\$k\$h\$&\$K\$7\$F\$_\$^\$7\$?!#\$=\$3\$KE~C#\$7\$?\$iNs5s\$rBGe(B
e\$B\$A@Z\$j\$^\$9!#e(B

e\$B\$?\$7\$+\$K\$Me(B. e\$B\$"\$C\$?J}\$,NI\$\$\$+\$be(B.

c) Prime.eache\$B\$NBhe(B2e\$B0z?t\$Ke(Bgeneratore\$B\$r;XDj\$G\$-\$k\$h\$&\$K\$7\$F\$_\$^\$7\$?!#e(B

d) Prime.eache\$B\$He(BPrime.each.eache\$B\$,F1\$8%*%V%8%’%/%H\$G\$"\$k\$?\$a\$KNs5s>uBV\$r6&e(B
e\$BM-\$7\$F\$7\$^\$\$\$^\$9!#e(Bgeneratore\$B\$,e(Bselfe\$B\$G\$O\$J\$/e(Bself.dupe\$B\$rJV\$9\$h\$&\$K\$7\$^\$7\$?!#e(B

e\$B\$*!<e(B. e\$B5\$\$,IU\$-\$^\$;\$s\$G\$7\$?e(B.

e) Enumeratore\$B\$K9g\$o\$;\$F!"e(BGeneratore\$B\$Ke(Bwith_indexe\$B\$He(Bnexte\$B\$He(Brewinde\$B\$rB-\$7\$^e(B
e\$B\$7\$?!#e(B

e\$BN;2re(B

• Prime::IncreaseSuperPrimeGenerator
e\$B3N\$+\$K\$3\$NL>A0\$K\$O0cOB46\$,\$"\$j\$^\$9\$M!#e(B

PseudoPrimeGeneratore\$B\$"\$?\$j\$G\$O\$I\$&\$G\$7\$g\$&\$+!#e(BGenerator23e\$B\$_\$?\$\$\$K!V5?e(B
e\$B;w!W\$N@:EY\$,0-\$/\$F\$b!“5U\$K??\$Ke(Bprimee\$B\$G\$”\$C\$F\$be(BPseudoe\$B\$N%5%V%;%C%H\$K\$O\$J\$ke(B
e\$B\$H;W\$\$\$^\$9!#e(B

e\$B\$=\$&\$7\$^\$7\$g\$&e(B.

e\$B2?%+=j\$+JQ998D=j\$,\$"\$j\$^\$7\$?\$,e(B, e\$B\$\$\$+\$,\$G\$7\$g\$&e(B?

e\$B\$b\$7e(B, e\$BLdBj\$J\$\$\$h\$&\$G\$"\$l\$Pe(B, e\$B\$9\$\$\$^\$;\$s\$,e(B,
e\$B\$=\$l\$rJQ99\$7\$Fe(Bcomit e\$B\$7\$F\$\$\$?e(B
e\$B\$@\$1\$J\$\$\$G\$9e(B?

__
---------------------------------------------------->> e\$B@PDMe(B
e\$B7=<ye(B <<—
---------------------------------->> e-mail: [email protected] <<—

e\$BK-J!\$G\$9!#e(B

• `````` q,r = divmod value num
``````

e\$B\$9\$\$\$^\$;\$s!#e(B
q,r = value.divmod num

Yuguie\$B\$G\$9!#e(B

TOYOFUKU Chikanobu e\$B\$5\$s\$O=q\$-\$^\$7\$?e(B:

• `````` return true if value < num * num
``````
• `````` return false if value % num == 0
``````
• `````` q,r = divmod value num
``````
• `````` return false if r == 0
``````
• `````` return true if q <= num
``````
end
end

e\$B\$3\$l\$@\$He(B2e\$B\$,AG?t\$K\$J\$i\$J\$\$\$G\$9!#99\$Ke(B

• `````` return false if r == 0
return true if q <= num
``````
• `````` return false if r == 0
``````

e\$B\$+\$J!#e(B

Yugui (Yuki S.) e\$B\$5\$s\$O=q\$-\$^\$7\$?e(B:

e\$B\$3\$l\$@\$He(B2e\$B\$,AG?t\$K\$J\$i\$J\$\$\$G\$9!#99\$Ke(B

• `````` return false if r == 0
return true if q <= num
``````
• `````` return false if r == 0
``````

e\$B\$*\$C\$H!#e(B

• `````` return false if r == 0
``````
• `````` return true if q <= num
return true if q < num
``````
• `````` return false if r == 0
``````

e\$B\$G\$7\$?!#e(B

Yuguie\$B\$G\$9!#e(B

keiju ISHITSUKA e\$B\$5\$s\$O=q\$-\$^\$7\$?e(B:

OldCompatibility

e\$B\$3\$l\$ONI\$\$\$G\$9\$M!#e(B
e\$BK-J!\$5\$s\$N\$rJ;\$;\$F!"\$9\$Y\$F%3%_%C%H\$7\$^\$7\$?!#e(B

e\$B\$1\$\$\$8\$e!w\$\$\$7\$D\$+\$G\$9e(B.

In [ruby-dev :36067 ] the message: "[ruby-dev:36067] Re: Refactoring
of enumerating prime numbers ", on Sep/01 00:07(JST) “Yugui (Yuki
Sonoda)” writes:

Yuguie\$B\$G\$9!#e(B

e\$B\$I\$b\$G\$9e(B.

e\$B\$"\$j\$,\$H\$&\$4\$6\$\$\$^\$9!#%3%_%C%H\$7\$h\$&\$+\$H;W\$\$\$^\$7\$?\$,%F%9%H\$r=q\$\$\$F\$\$\$?\$ie(B
2e\$BE@LdBj\$K5\$\$E\$-\$^\$7\$?!#7k2L\$H\$7\$F\$A\$g\$C\$HJQ99\$,I,MW\$K\$J\$j\$=\$&\$G\$9\$N\$G!"e(B
e\$B62\$lF~\$j\$^\$9\$,:FEY3NG’\$r\$*4j\$\$\$7\$^\$9!#e(B

e\$B\$O\$\$e(B.

e\$B@h\$K\$*Aw\$j\$7\$?e(Bprime.rbe\$B\$G\$Oe(B2e\$BE@LdBj\$,\$"\$j\$^\$7\$?!#e(B

1. Prime.eache\$B\$,JV\$9e(Benumerator(e\$B<B:]\$K\$Oe(Bgenerator)e\$B\$,!"e(Bubounde\$B\$r5-21\$7\$J\$\$!#e(B
Prime.each(100){|p| }e\$B\$ODd;_\$9\$k\$N\$K!"e(BPrime.each(100).each{|p| }e\$B\$ODd;_e(B
e\$B\$7\$J\$\$\$H\$\$\$&4qL/\$J7k2L\$K\$J\$j\$^\$9!#e(B

e\$B\$3\$l\$Oe(B, e\$B\$^\$:\$\$\$G\$9\$Me(B.

1. Prime#nexte\$B\$,\$J\$\$!#e(B
e\$B8_49@-\$K\$3\$@\$o\$C\$F8+\$;\$?3d\$K\$&\$C\$+\$je(BPrime#nexte\$B\$N<BAu\$r=q\$-K:\$l\$F\$\$\$^\$7\$?!#e(B
e\$B\$^\$?!"@h\$N%P!<%8%g%s\$r85\$KC1=c\$Ke(BPrime#nexte\$B\$r<BAu\$9\$k\$He(BPrime#nexte\$B\$K\$h\$Ce(B
e\$B\$F?J\$s\$@FbIt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9e(B.

— prime.rb 2008-09-03 11:01:29.000000000 +0900
+++ prime2.rb 2008-09-03 20:41:10.000000000 +0900
@@ -41,9 +41,6 @@
class Prime
include Enumerable

• def initialize # :nodoc:

• @generator = nil

• end
@the_instance = Prime.new

# obsolete.

@@ -51,13 +48,22 @@

# Use +Prime::instance+ or class methods of +Prime+.

def initialize
@generator = EratosthenesGenerator.new

• extend OldCompatibility
warn “Prime::new is obsolete. use Prime::instance or class methods
of Prime.”
end
• def succ
• @generator.succ
• module OldCompatibility
• def succ
• `````` @generator.succ
``````
• end
• alias next succ
• def each(&block)
• `````` loop do
``````
• yield succ
• `````` end
``````
• end
end
• alias next succ

class<<self
extend Forwardable
@@ -71,16 +77,11 @@
end

# iterates the given block over all prime numbers.

• def each(ubound = nil, generator = nil, &block)

• generator ||= (@generator || EratosthenesGenerator.new)

• orig_ubound = generator.upper_bound

• def each(ubound = nil, generator = EratosthenesGenerator.new, &block)
generator.upper_bound = ubound
generator.each(&block)
• ensure

• generator.upper_bound = orig_ubound
end

# +value+:: an arbitrary integer.

__
---------------------------------------------------->> e\$B@PDMe(B
e\$B7=<ye(B <<—
---------------------------------->> e-mail: [email protected] <<—