2009-01-01から1ヶ月間の記事一覧

SchemeとRubyで高階関数を学ぼう ~その2~

前回に引き続き「[rakuten:book:10825992:title]」を使って SchemeとRubyで平方根の求め方と 手続きを出力とする高階手続きをまとめてみました なおSchemeのコードは本書からの抜粋で 説明は自分の要約です Newton法を使って平方根を求める 平方根を求めると…

SchemeとRubyで高階関数を学ぼう

「計算機プログラムの構造と解釈」という本を図書館で借りた プログラマー必読の名著で Amazonによればこれ1冊でコンピュータのすべてがわかるらしい 主著者はLISPの一方言であるSchemeという言語を作った人で 本書もSchemeで書かれている Ruby以外知らない…

Rubyでアルファベット値を数える 〜Rubyでオイラープロジェクトを解こう!Problem22

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 …

Rubyで友愛数を探す 〜Rubyでオイラープロジェクトを解こう!Problem21

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 calle…

Rubyで20世紀の日曜日を求める 〜Rubyでオイラープロジェクトを解こう!Problem19

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, Saving …

Rubyで階乗して桁を合計 〜Rubyでオイラープロジェクトを解こう!Problem20

Problem 20 - Project Eulerより n! means n × (n - 1)× ... × 3 × 2 × 1 Find the sum of the digits in the number 100! n!は n × (n - 1)× ... × 3 × 2 × 1を意味する。 100!における桁の合計を求めよ。 Integerクラスのインスタンスメソッドとして !とsu…

Rubyで三角形の最大ルートを求める 〜Rubyでオイラープロジェクトを解こう!Problem18

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 3That is, 3 + 7 + 4 + 9 = 23. Find the maximum total…

Rubyで英数字を作って文字数を数える 〜Rubyでオイラープロジェクトを解こう!Problem17

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 writte…

Rubyで最短ルート数を探す 〜Rubyでオイラープロジェクトを解こう!Problem15

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? 2×2グリッドの左上の角からスタートした場合、…

Rubyで桁の合計を求める 〜Rubyでオイラープロジェクトを解こう!Problem16

Problem 16 - Project Eulerより and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number ? の各桁の合計は 3 + 2 + 7 + 6 + 8 = 26である。 の各桁の合計はいくつか。 算数的でないけど def sum_of_digits(n) …

Rubyで最長の数列を探す 〜Rubyでオイラープロジェクトを解こう!Problem14

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 20 …

Rubyで三角数の約数を探せ! 〜Rubyでオイラープロジェクトを解こう!Problem12

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, …

Rubyで100個の数を足す 〜Rubyでオイラープロジェクトを解こう!Problem13

Problem 13 - Project Eulerより Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. 以下の50桁数字100個の合計における最初の10桁を算出せよ。 配列に入れてinject(:+)します number = <

Rubyでサブプライム問題解決! 〜Rubyでオイラープロジェクトを解こう!Problem10

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. 10未満の素数の和は、2 + 3 + 5 + 7 = 17 である。 200万未満の素数すべての和を求めよ。 Problem3で既に素数を…

Rubyで縦横斜めの積を求める 〜Rubyでオイラープロジェクトを解こう!Problem11

Problem 11 - Project Eulerより In the 20×20 grid below, four numbers along a diagonal line have been marked in red.08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 4…

Rubyで数字をスライスする 〜Rubyでオイラープロジェクトを解こう!Problem8

Problem 8 - Project Eulerより Find the greatest product of five consecutive digits in the 1000-digit number. 以下の1000桁の数字における連続した5つの数の積の最大値を求めよ。7316717653133062491922511967442657474235534919493496983520312774506…

Rubyでピタゴラスを求める 〜Rubyでオイラープロジェクトを解こう!Problem9

Problem 9 - Project Eulerより A Pythagorean triplet is a set of three natural numbers, a For example, . There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. ピタゴラス数とは、次の関係にある3つの自…

Rubyでサムオブスクエアスクエアオブサム 〜Rubyでオイラープロジェクトを解こう!Problem6

Problem 6 - Project Eulerより The sum of the squares of the first ten natural numbers is, The square of the sum of the first ten natural numbers is, Hence the difference between the sum of the squares of the first ten natural numbers and t…

Rubyでエラトステネス 〜Rubyでオイラープロジェクトを解こう!Problem7

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? 最初の6つの素数2、3、5、7、11、および13を並べれば、6番目が13であることがわ…

Rubyで最小公倍数を求める 〜Rubyでオイラープロジェクトを解こう!Problem5

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? 2520は1から10…

Rubyで回文数を求める 〜Rubyでオイラープロジェクトを解こう!Problem4

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. 回…

効率的市場仮説はそれを信じない者へのご褒美である

効率的市場仮説 〜Wikipediaより 市場参加者は利用可能なすべての情報を迅速に取り入れており、新規情報によって他の市場参加者より有利になるという状況は生じないため、市場の挙動はランダムウォークになるという仮説。 株券は紙幣同様信用ベースの資産で…

Rubyで3と5の倍数を求める 〜Rubyでオイラープロジェクトを解こう!Problem1

Problem 1 - Project Eulerより If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. 10未満の自然数で3または…

Rubyでフィボナッチ数列を求める 〜Rubyでオイラープロジェクトを解こう!Problem2

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-…

Rubyで素因数を求める 〜Rubyでオイラープロジェクトを解こう!Problem3

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 ? 13195の素因数は、5、7、13および29である。 では600851475143の最大の素因数はいくらか。 素因数 - Wikip…

Rubyでフィボナッチ、トリボナッチ、テトラナッチ!そして僕はヒトリボッチ

ぴえろっちが問題をくれた。その1 - とりこびとの雑記 フィボナッチ数列の項は前の2つの項の和である。最初の2項を 1, 2 とすれば、最初の10項は以下の通りである。 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...数列の項が400万を超えない範囲で、偶数の項の総…