Project Euler(51 ã 73) ããã³ããæ¨ãªã©
ä¹
ã
ã«Project Eulerããã¾ãããã¾ã rowlã¯ä½¿ãã段éã«ãªãã®ã§ãRubyã¤ãã£ã¦ã¾ãã
ãã®ãããããæ°è«ã®åé¡ãããããåºã¦ãã¦é¢ç½ãã£ãã§ãã
ãºã«æ¹ç¨å¼(Problem 66)
ç°¡åã«ã¯
Dãå¹³æ¹æ°ã§ãªãæ´æ°ã®æ
x^2 - Dy^2 = 1
ã®æ´æ°è§£ãæ±ãã
ã¨ããåé¡ãããã¯ãºã«æ¹ç¨å¼ã¨ãã£ã¦ããã£ãªãã¡ã³ãã¹æ¹ç¨å¼ã®ä¸ç¨®ã§ãã
以ä¸ã®ãã¼ã¸ã«è©³ããçè«ã¨è§£æ³ãç´¹ä»ããã¦ãã¾ããé£åæ°å±éã使ãã¾ãã
http://aozoragakuen.sakura.ne.jp/suuron/suuron.html
ä½è«ã§ããããã£ãªãã¡ã³ãã¹æ¹ç¨å¼(ç¹ã«ããºã¼æ¹ç¨å¼ a x + b y = d)ã¯ã³ã³ãã¤ã©ã®æç§æ¸ã«ãåºã¦ãã¾ãã¦ããã®é¢ä¿ã§ç§ã¯åãããºã«æ¹ç¨å¼ã®äºãç¥ã£ã¦ãã¾ããã
äºã¤ã®é
åã¢ã¯ã»ã¹a[p*i+q]ã¨b[r*j+s]ãåãå ´æãåç
§ãããã©ããã¯
a + sizeof(a[0]) * (p * i + q) == b + sizeof(b[0]) * (r * j + s)
ã解ãæã¤ãã©ããã§æ±ºã¾ãã¾ããããã¯i,jãå¤æ°ã¨ããããºã¼æ¹ç¨å¼ã«ãªã£ã¦ãã¾ãããã®è§£ã調ã¹ãäºã§ã«ã¼ãã®ä¸¦åæ§æ¤æ»ãªã©ãåºæ¥ã¾ãã
ãªã¤ã©ã¼ã®ãã¼ãã£ã¨ã³ãé¢æ°(Problem 69, 70)
ãªã¤ã©ã¼ã®ãã¼ãã£ã¨ã³ãé¢æ°Ï(n)ã¨ã¯nã¨äºãã«ç´ ãªn以ä¸ã®èªç¶æ°ã®æ°ã¨ãã¦å®ç¾©ããã¦ãã¾ãã
ããã¯nã®ç´ å æ°å解ã
ã¨ãªãã¨ãã
ãæºããã¾ããnã®ç´ å æ°å解ã¯é常ã«éãã§ãããããã¼ãã£ã¨ã³ãé¢æ°ã«é¢ããåé¡ã¯
- ç´ æ°ã®ãªã¹ãã®ä¸ã§æ¢ç´¢ãã
- ã¨ã©ãã¹ããã¹ã®ç¯©ã§è¨ç®
ã¨ãã£ãæ¹æ³ãåããã¨ã«ãªãã¨æãã¾ããä¾ãã°å¾è ã«é¢ãã¦ã¯ä»¥ä¸ã®ãããªã³ã¼ãã§2ãNã«ã¤ãã¦é¢æ°ã®å¤ãæ±ããäºãåºæ¥ã¾ãã
phi = (0..N).to_a 2.upto(N) do |n| if phi[n] == n # n is a prime number n.step(N, n) {|p| phi[p] = phi[p]*(n-1)/n} end end
ãã¡ã¬ã¤æ°åãããã³ããæ¨(Problem 71,72,73)
ãã®3åã¯ãã¡ã¬ã¤æ°åã«ã¤ãã¦ã®åé¡ã§ãã
ファレイ数列ã¨ããã®ã¯0以ä¸1以ä¸ã®æ¢ç´ãªåæ°ã並ã¹ããã®ã¨ãã¦å®ç¾©ããã¾ãã
ä¾ãã°åæ¯ã8以ä¸ã®æã¯
F8 = 0/1, 1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8, 1/1
ã¨ãªãã¾ãã
ãã¡ã¬ã¤æ°åã®é·ã
Problem 72ã¯ãã¡ã¬ã¤æ°åã®é·ããæ±ããåé¡ã§ãããããã¯ãã¼ãã£ã¨ã³ãé¢æ°ãç¨ãã¦
ã¨è¡¨ãã¾ããåæ¯ãkã§ããæ¢ç´ãª1æªæºã®åæ°ã¯ãkã¨äºãã«çãªk以ä¸ã®èªç¶æ°ã®æ°ã¨ä¸è´ããã®ã§ãããæç«ãã¾ãã
ããã³ããæ¨
Problem 71ã¨73ã¯ãã¡ã¬ã¤æ°åã®ä¸é¨ã ããèããåé¡ã§ãããããããåé¡ã§ã¯ããã³ããæ¨(Stern Brocot tree)ã便å©ã§ãã
ããã³ããæ¨ã¨ããã®ã¯
æ¢ç´ãªåæ°ã®ååå士ãåæ¯å士ãå ããäºãç¹°ãè¿ãã¦ä½ãããæ¨ã§å³ã®ããã«ãªãã¾ã(wikipediaããå¼ç¨)ã
ãã®ããã³ããæ¨ã¯
- çæãããåæ°ã¯å¿ ãæ¢ç´ã«ãªã£ã¦ãã
- å ¨ã¦ã®åæ°ãç¶²ç¾ ãã
ã¨ããé常ã«è¯ãæ§è³ªãæã£ã¦ãã¾ããåæ¯ååã®ç´åã®åé¡ãèããªãã¦è¯ãã®ã§ãæ¢ç´åæ°ã«é¢ããæ¢ç´¢åé¡ãé«éã«è§£ãã¾ãã
Problem 71: åæ¯ã1,000,000以ä¸ã®åæ°ã§3/7ã®æªæºã®æ大ã®ç©ã¯ä½ãï¼
3/7ãæãåºéã§ããã³ããæ¨ã辿ã£ã¦è¡ãã°è¯ãã§ãã
la, lb = 0, 1 ra, rb = 1, 2 while true ma, mb = la + ra, lb + rb if 7*ma < 3*mb if mb > 1000000 puts "#{la}/#{lb}" exit end la, lb = ma, mb else ra, rb = ma, mb end end
å®è¡æéã¯0.02sãããã§ãã
Problem 73: åæ¯ã12,000以ä¸ã®åæ°ã§1/2ã¨1/3ã®éã®æ°ã¯ä½åãï¼
æ¨ã®ãã¼ãæ°ãæ·±ãåªå æ¢ç´¢ã§æ±ããã ããã¾ããååã®æ°åã¯èããå¿ è¦ãªãã§ãã
def dfs(l, r) m = l + r return 0 if m > 12000 dfs(l, m) + dfs(m, r) + 1 end puts dfs(3, 2)
å®è¡æéã¯1.83sãããã§ãã
ããã辺ã®è©±ã¯Knuthã®æ¬ã«æ¸ãã¦ãã£ãã¨æãã¾ãã
- ä½è : ããã«ãã»L.ã°ã¬ã¢ã ,ãªã¼ã¬ã³ãã¿ã·ã¥ãã¯,ããã«ãã»E.ã¯ãã¼ã¹,Ronald L. Graham,Oren Patashnik,Donald E. Knuth,ææ²¢èª ,è©ééä¹,å®æéæ,ç³çæ¸
- åºç社/ã¡ã¼ã«ã¼: å ±ç«åºç
- çºå£²æ¥: 1993/08
- ã¡ãã£ã¢: åè¡æ¬
- è³¼å ¥: 5人 ã¯ãªãã¯: 224å
- ãã®ååãå«ãããã° (38件) ãè¦ã