PamGau

Web周り、サッカーの話、ときどきヌコ

Ruby : カテゴリーの記事一覧

"Ruby Way"章頭の言葉
Twitterの影響からか名言っぽいことを引用するのが流行っているような気がするので、手元にある"Ruby Way 第2版 (Professional Ruby Series)”の章の初めに記載された名言っぽい句を抜き出してみました。 Rubyの概要言語はわれわれの思考方法を形成し、思考できる...
2010/05/02 - [Ruby]
RubyでProject Euler - Problem 59
Problem 59 (Project Euler) [原文] 暗号化鍵は3文字の小文字である. cipher1.txtは暗号化されたASCIIのコードを含んでいる. また, 平文はよく用いられる英単語を含んでいる. この暗号文を復号し, 平文のASCIIでの値の和を求めよ. ちょっとしたハッカー気分でどう...
2009/06/23 - [Ruby]
RubyでProject Euler - Problem 58
Problem 58 (Project Euler) [原文] 1から初めて, 以下のように反時計回りに数字を並べていくと, 辺の長さが7の渦巻きが形成される.(中略)面白いことに, 奇平方数が右下の対角線上に出現する. もっと面白いことには, 対角線上の13個の数字のうち, 8個が素数である. ...
2009/06/23 - [Ruby]
RubyでProject Euler - Problem 57
Problem 57 (Project Euler) [原文] 2の平方根は無限に続く連分数で表すことができる. √ 2 = 1 + 1/(2 + 1/(2 + 1/(2 + … ))) = 1.414213… (中略) 第8項は1393/985である. これは分子の桁数が分母の桁数を超える最初の例である. 最初の1000項を考えたとき, 分子...
2009/06/23 - [Ruby]
RubyでProject Euler - Problem 55, 56
初の複数問掲載記事。 Problem 55 (Project Euler) [原文] 10000未満のLychrel数の個数を答えよ. 50回も題意の処理をしても回文数が表れなければ、Lychrel数と見做しても差し支えないという条件をつけられているので楽です。 案外、Lychrel数は多いです。また、L...
2009/06/22 - [Ruby]
RubyでProject Euler - Problem 54
Problem 54 (Project Euler) [原文] poker.txtには1000個のランダムな手札の組が含まれている. 各行は10枚のカードからなる (スペースで区切られている): 最初の5枚がプレイヤー1の手札であり, 残りの5枚がプレイヤー2の手札である. 以下のことを仮定してよい 全...
2009/06/22 - [Ruby]
RubyでProject Euler - Problem 53
Problem 53 (Project Euler) [原文] 12345から3つ選ぶ選び方は10通りである. 123, 124, 125, 134, 135, 145, 234, 235, 245, 345. 組み合わせでは, 以下の記法を用いてこのことを表す: 5C3 = 10. 一般に, r <= n についてnCr = n!/(r!(n-r)!) である. ここで, n!...
2009/06/21 - [Ruby]
RubyでProject Euler - Problem 52
Problem 52 (Project Euler) [原文] 125874を2倍すると251748となる. これは元の数125874と同じ数を含む.2x, 3x, 4x, 5x, 6xがxと同じ数を含むような最小の正整数xを求めよ 求める正の整数xは、6倍しても桁数が増えないことにまず気づきます。 また5倍しても同...
2009/06/21 - [Ruby]
RubyでProject Euler - Problem 51
Problem 51 (Project Euler) [原文] *57の第1桁を置き換えることで, 157, 257, 457, 557, 757, 857という6つの素数が得られる. 56**3の第3桁と第4桁を同じ数で置き換ることを考えよう. この5桁の数は7つの素数をもつ最初の例である: 56003, 56113, 56333, 56443, 5...
2009/06/21 - [Ruby]
RubyでProject Euler - Problem 50
Problem 50 (Project Euler) [原文] 素数41は6つの連続する素数の和として表せる: 41 = 2 + 3 + 5 + 7 + 11 + 13. 100未満の素数を連続する素数の和で表したときにこれが最長になる. 同様に, 連続する素数の和で1000未満の素数を表したときに最長になるのは953で...
2009/06/19 - [Ruby]
RubyでProject Euler - Problem 49
Problem 49 (Project Euler) [原文] 項差3330の等差数列1487, 4817, 8147は次の2つの変わった性質を持つ。 3つの項はそれぞれ素数である。各桁は他の項の置換で表される。 1, 2, 3桁の素数にはこのような性質を持った数列は存在しないが、4桁の増加列にはもう1つ...
2009/06/19 - [Ruby]
RubyでProject Euler - Problem 48
Problem 48 (Project Euler) [原文] 次の式は、11 + 22 + 33 + ... + 1010 = 10405071317 である。 では、11 + 22 + 33 + ... + 10001000 の最後の10桁を求めよ 何も思いつかなかったので、問題文の云うままに書いたら0.04秒で答えが出ました。Rubyさまさま。Cだ...
2009/06/17 - [Ruby]
RubyでProject Euler - Problem 47
Problem 47 (Project Euler) [原文] 連続する2つの数がそれぞれ2つの異なる素因数を持つのは 14 = 2 × 715 = 3 × 5 の場合である. 同様に連続する3つの数がそれぞれ3つの異なる素因数を持つのは 644 = 22 × 7 × 23645 = 3 × 5 × 43646 = 2 × 17 × 19 の場合で...
2009/06/17 - [Ruby]
RubyでProject Euler - Problem 46
Problem 46 (Project Euler) [原文] Christian Goldbachは全ての奇合成数は平方数の2倍と素数の和で表せると予想した. 9 = 7 + 2×1215 = 7 + 2×2221 = 3 + 2×3225 = 7 + 2×3227 = 19 + 2×2233 = 31 + 2×12 後に, この予想は誤りであることが分かった. 平方数の...
2009/06/17 - [Ruby]
RubyでProject Euler - Problem 45
Problem 45 (Project Euler) [原文] 三角数, 五角数, 六角数は以下のように生成される. 三角数Tn=n(n+1)/2 == 1, 3, 6, 10, 15, ... 五角数Pn=n(3n-1)/2 == 1, 5, 12, 22, 35, ... 六角数Hn=n(2n-1) == 1, 6, 15, 28, 45, ... T285 = P165 = H143 = 40755で...
2009/06/17 - [Ruby]
RubyでProject Euler - Problem 44
Problem 44 (Project Euler) [原文] 五角数は Pn = n(3n-1)/2で生成される. 最初の10項は 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, … である. P4 + P7 = 22 + 70 = 92 = P8である. しかし差 70 - 22 = 48は五角数ではない. 五角数のペア PjとPkについて, 差と...
2009/06/17 - [Ruby]
RubyでProject Euler - Problem 43
Problem 43 (Project Euler) [原文] 数1406357289は0から9のPandigital数である (0から9が1度ずつ現れるので). この数は部分語が面白い性質を持っている. d1を1桁目, d2を2桁目の数とし, 以下順にdnを定義する. この記法を用いると次のことが分かる. d2d3d4 = 4...
2009/06/17 - [Ruby]
RubyでProject Euler - Problem 42
Problem 42 (Project Euler) [原文] 三角数のn項は tn = n(n+1)/2 で与えられる. 最初の10項は 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, … である. 単語中のアルファベットを数値に変換した後に和をとる. この和を「単語の値」と呼ぶことにする. 例えば SKY は 19 ...
2009/06/15 - [Ruby]
RubyでProject Euler - Problem 41
Problem 41 (Project Euler) [原文] n桁の数がPandigitalであるとは, 1からnまでの数を各桁に1つずつもつことである. 例えば2143は4桁のPandigital数であり, かつ素数である. n桁のPandigitalな素数の中で最大の数を答えよ. "pan-"という接頭辞は「あまねく、広汎...
2009/06/15 - [Ruby]
RubyでProject Euler - Problem 40
Problem 40 (Project Euler) [原文] 正の整数を順に連結して得られる以下の10進の無理数を考える: 0.123456789101112131415161718192021... 小数点第12位は1である. dnで小数点第n位の数を表す. d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000 を求めよ...
2009/06/15 - [Ruby]
RubyでProject Euler - Problem 39
Problem 39 (Project Euler) [原文] 辺の長さが{a,b,c}と整数の3つ組である直角三角形を考え, その周囲の長さをpとする. p = 120のときには3つの解が存在する: {20,48,52}{24,45,51}{30,40,50} p < 1000 で解の数が最大になる p を求めよ ピタゴラス数を求める場...
2009/06/15 - [Ruby]
RubyでProject Euler - Problem 38
Problem 38 (Project Euler) [原文] 192を1, 2, 3で掛けてみよう. 192 × 1 = 192192 × 2 = 384192 × 3 = 576 積を連結することで1から9のPandigital数 192384576 が得られる. 192384576を 192と(1,2,3)の連結積と呼ぶ. 同じようにして, 9を1,2,3,4,5と掛け連結す...
2009/06/15 - [Ruby]
RubyでProject Euler - Problem 37
Problem 37 (Project Euler) [原文] 3797は面白い性質を持っている. まずそれ自身が素数であり, 左から右に桁を除いたときに全て素数になっている (3797, 797, 97, 7). 同様に右から左に桁を除いたときも全て素数である (3797, 379, 37, 3). 右から切り詰めても左...
2009/06/14 - [Ruby]
RubyでProject Euler - Problem 36
Problem 36 (Project Euler) [原文] 585 = 1001001001 (2進) は10進でも2進でも回文数である. 100万未満で10進でも2進でも回文数になるような数の総和を求めよ. (注: 先頭に0を含めて回文にすることは許されない.) 10進数を2進法で表現する方法は、逆引きRubyで...
2009/06/14 - [Ruby]
RubyでProject Euler - Problem 35
Problem 35 (Project Euler) [原文] 197は巡回素数と呼ばれる. 桁を回転させたときに得られる数 197, 971, 719 が全て素数だからである. 100未満には巡回素数が13個ある: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, および97である. 100万未満の巡回素数は何個...
2009/06/14 - [Ruby]
RubyでProject Euler - Problem 34
Problem 34 (Project Euler) [原文] 145は面白い数である. 1! + 4! + 5! = 1 + 24 + 120 = 145となる. 各桁の数の階乗の和が自分自身と一致するような数の総和を求めよ. 注: 1! = 1 と 2! = 2 は総和に含めてはならない. いったいいくつまでの数を調べればいいの...
2009/06/14 - [Ruby]
RubyでProject Euler - Problem 33
Problem 33 (Project Euler) [原文] 49/98は面白い分数である. 「分子・分母の9をキャンセルしたので 49/98 = 4/8 が得られた」と経験を積んでいない数学者が誤って思い込んでしまうかもしれないからである. 我々は 30/50 = 3/5 のようなタイプは自明な例だとする....
2009/06/14 - [Ruby]
RubyでProject Euler - Problem 32
Problem 32 (Project Euler) [原文] 7254は面白い性質を持っている. 39 × 186 = 7254と書け, 掛けられる数/掛ける数/積に1から9の数が1回ずつ出現する. 掛けられる数/掛ける数/積に1から9の数が1回ずつ出現するような積の総和を求めよ. HINT: いくつかの積は, 1通...
2009/06/14 - [Ruby]
RubyでProject Euler - Problem 31
Problem 31 (Project Euler) [原文] イギリスでは硬貨はポンドとペンスがあり,一般的な流通ではこれらの8つの硬貨がある. 1p, 2p, 5p, 10p, 20p, 50p, 1ポンド (100p) and 2ポンド (200p). 以下の方法で2ポンドを作ることが可能である. 1×1ポンド + 1×50p + 2...
2009/06/14 - [Ruby]
RubyでProject Euler - Problem 30
Problem 30 (Project Euler) [原文] 驚くべきことに, 各桁を4乗した和が元の数と一致する数は3つしかない. 1634 = 14 + 64 + 34 + 44 8208 = 84 + 24 + 04 + 84 9474 = 94 + 44 + 74 + 44 ただし, 1=14そのものは含まないものとする. この数たちの和は 1634...
2009/06/11 - [Ruby]
RubyでProject Euler - Problem 29
Problem 29 (Project Euler) [原文] 2 <= a <= 5 と 2 <= b <= 5について, abを全て考えてみよう: 22 = 4, 23 = 8, 24 = 16, 25 = 32 32 = 9, 33 = 27, 34 = 81, 35 = 243 42 = 16, 43 = 64, 44 = 256, 45 = 1024 52 = 25, 53 = 125, 54 = 625, 55 = 3125 ...
2009/06/11 - [Ruby]
RubyでProject Euler - Problem 28
Problem 28 (Project Euler) [原文] 1から初めて右方向に進み時計回りに数字を増やしていき, 5×5の螺旋が以下のように生成される: (表略) 両対角線上の数字の合計は101であることが確かめられる. 1001×1001の螺旋を同じ方法で生成したとき, 対角線上の数字の合計...
2009/06/11 - [Ruby]
RubyでProject Euler - Problem 27
Problem 27 (Project Euler) [原文] オイラーは以下の二次式を考案している: n2 + n + 41 この式は, nを0から39までの連続する整数としたときに40個の素数を生成する. しかし, n = 40のとき402 + 40 + 41 = 40(40 + 1) + 41となり41で割り切れる. また, n = 41の...
2009/06/10 - [Ruby]
RubyでProject Euler - Problem 26
Problem 26 (Project Euler) [原文] 単位分数とは分子が1の分数である。分母が2から10の単位分数を10進数で表記すると次のようになる。 1/2 = 0.51/3 = 0.(3)1/4 = 0.251/5 = 0.21/6 = 0.1(6)1/7 = 0.(142857)1/8 = 0.1251/9 = 0.(1)1/10 = 0.1 0.1(6) 0.166666...
2009/06/09 - [Ruby]
RubyでProject Euler - Problem 25
Problem 25 (Project Euler) [原文]) フィボナッチ数列は以下の漸化式で定義される : Fn = Fn-1 + Fn-2, ただし F1 = 1, F2 = 1. 最初の12項は以下である. F1 = 1F2 = 1F3 = 2F4 = 3F5 = 5F6 = 8F7 = 13F8 = 21F9 = 34F10 = 55F11 = 89F12 = 144 12番目の項, F...
2009/06/08 - [Ruby]
RubyでProject Euler - Problem 24
Problem 24 (原文) 順列とはモノの順番付きの並びのことである. たとえば, 3124は数1, 2, 3, 4の一つの順列である. すべての順列を数の大小でまたは辞書式に並べたものを辞書順と呼ぶ. 0と1と2の順列を辞書順に並べると 012, 021, 102, 120, 201, 210 になる. 0,...
2009/06/08 - [Ruby]
RubyでProject Euler - Problem 23
Problem 23 (Project Euler) [和訳]) A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a ...
2009/06/07 - [Ruby]
RubyでProject Euler - Problem 22
Problem 22 (Project Euler) [和訳]) Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for eac...
2009/06/04 - [Ruby]
RubyでProject Euler - Problem 21
Problem 21 (Project Euler) [和訳]) Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called a...
2009/06/04 - [Ruby]
RubyでProject Euler - Problem 20
Problem 20 (Project Euler) [和訳]) n! means n * (n - 1) * … * 3 * 2 * 1 Find the sum of the digits in the number 100! n の階乗を返すメソッドを"fact(n)"と別途書いておいてから、下記の一行を実行すると答えが出ました。 puts fact(100).to_s.split(//)...
2009/06/04 - [Ruby]
RubyでProject Euler - Problem 19
Problem 19 (Project Euler) [和訳]) You are given the following information, but you may prefer to do some research for yourself. 1 Jan 1900 was a Monday. Thirty days has September, April, June and November. All the rest have thirty-one, Savin...
2009/06/04 - [Ruby]
RubyでProject Euler - Problem 18
Problem 18 (Project Euler) [和訳]) By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 5 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum t...
2009/06/04 - [Ruby]
RubyでProject Euler - Problem 17
Problem 17 (Project Euler) [和訳]) If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written ...
2009/06/04 - [Ruby]
RubyでProject Euler - Problem 16
Problem 16 (Project Euler) [和訳]) 215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.What is the sum of the digits of the number 21000? Rubyでは一行で書けてしまって、ちっともおもしろくありません。 しかたなく、"LONG_MAX"が214748364...
2009/06/04 - [Ruby]
RubyでProject Euler - Problem 15
Problem 15 (Project Euler) [和訳]) Starting in the top left corner of a 2×2 grid, there are 6 routes (without backtracking) to the bottom right corner. (画像省略) How many routes are there through a 20×20 grid? 各20個ずつ用意された赤球・白球を...
2009/06/02 - [Ruby]
RubyでProject Euler - Problem 14
Problem 14 (Project Euler) [和訳]) The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 ...
2009/06/02 - [Ruby]
RubyでProject Euler - Problem 13
Problem 13 (Project Euler) [和訳]) Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.(以下略) Rubyだとこの問題をそのまま扱えてしまいます。 そうでなければ、50桁の数字を分割して、右から順次繰り上がりを自前で計...
2009/06/01 - [Ruby]
RubyでProject Euler - Problem 12
Problem 12 (Project Euler) [和訳]) The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55...
2009/06/01 - [Ruby]
RubyでProject Euler - Problem 11
Problem 11 (Project Euler) [和訳]) In the 20×20 grid below, four numbers along a diagonal line have been marked in red. (中略) The product of these numbers is 26 × 63 × 78 × 14 = 1788696. What is the greatest product of four adjacent numbers ...
2009/06/01 - [Ruby]
RubyでProject Euler - Problem 10
Problem 10 (Project Euler) [和訳]) The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.Find the sum of all the primes below two million. さすがに200万までの素数となると、「mathn」のPrimeだと少し遅く感じます。もちろん、「next_prime」も同様。解答...
2009/05/31 - [Ruby]
RubyでProject Euler - Problem 9
Problem 9 (Project Euler) [和訳]) A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Fi...
2009/05/31 - [Ruby]
RubyでProject Euler - Problem 8
Problem 8 (Project Euler) [和訳]) Find the greatest product of five consecutive digits in the 1000-digit number.以下略 なんかどうやっても大差はないようで、解説のPDF文書もありません。 課題の文字列を"split(//)"で配列にしてから、"to_i"で計算しただ...
2009/05/31 - [Ruby]
RubyでProject Euler - Problem 7
Problem 7 (Project Euler) [和訳]) By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime number? 件のメソッド「next_prime」では答えにたどり着くのに2分近くかかりました(笑) ...
2009/05/30 - [Ruby]
RubyでProject Euler - Problem 6
Problem 6 (Project Euler) [和訳]) The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)2 = 552 = 3025 Hence the difference between t...
2009/05/30 - [Ruby]
RubyでProject Euler - Problem 5
Problem 5 (Project Euler) [和訳]) 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest number that is evenly divisible by all of the numbers from 1 to 20? 電卓を使って...
2009/05/30 - [Ruby]
RubyでProject Euler - Problem 4
Problem 4 (Project Euler) [和訳]) A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers. Ru...
2009/05/29 - [Ruby]
RubyでProject Euler - Problem 3
Problem 3 (Project Euler) [和訳]) The prime factors of 13195 are 5, 7, 13 and 29.What is the largest prime factor of the number 600851475143 ? 素因数分解の問題ですから、素数のリストが必要となります。 Rubyに標準で添付されている「mathn」というラ...
2009/05/29 - [Ruby]
RubyでProject Euler - Problem 2
Problem 2 (Project Euler) [和訳]) Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...Find the sum of all the even-value...
2009/05/28 - [Ruby]
RubyでProject Euler - Problem 1
はじめに ご無沙汰しておりました。 私自身は健康だったのですが、いろいろときついことが重なり、Webから離れてしまった次第です。 さて、気を取り直し、表題の通りやってみます。 Project Euler 既に同様のことを実施されているブログもいくつかありますが、...
2009/05/26 - [Ruby]
Recent Entries
"Ruby Way"章頭の言葉
RubyでProject Euler - Problem 59
RubyでProject Euler - Problem 58
RubyでProject Euler - Problem 57
RubyでProject Euler - Problem 55, 56
RubyでProject Euler - Problem 54
RubyでProject Euler - Problem 53
RubyでProject Euler - Problem 52
RubyでProject Euler - Problem 51
RubyでProject Euler - Problem 50
Links
PamGau 系
PamGau::Memo
PamGau::Dust
PamgauSigh Wiki
はてなブックマーク
パンパでガウチョ
kyorecobaのdel.icio.us
BLOGNAVI
XREA.COM
VALUE-DOMAIN
PHP ver 4.4.2
Powered by Nucleus CMS Creative Commons
feedberner banner この日記のはてなブックマーク数
BlogPeople
あわせて読みたい