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

dviファイルの構成2

TeX

ポスタンブル ヘッダ ポスタンブルの先頭です. post p[4] num[4] den[4] mag[4] l[4] u[4] s[2] t[2]post(248)から始まる,4+4+4+4+4+4+2+2=29バイトです. p (4バイト):最後のページのbopの位置(開始位置) num:プリアンブルと同じ den:プリアンブルと…

dviファイルの構成

TeX

TeXが生成するdviファイルは当然バイナリですが,実際は普通にテキストにしたって問題ないようなもの,つまり機械語とかになってるわけでもなく,要は中間コード,仮想コードのようなものです.実際,dvioutの大島先生が,dviファイルの可視化やページ独立性…

dviファイルのノンブルの抽出

TeX

ちょっと必要があって,dviファイルのページをdviファイルから引き出すスクリプトを書きました.紛失防止の意味も込めて超いいかげんなものを晒します. ノンブルは一貫している(つまり,途中でリセットとかされない)のが大前提です. use strict; use war…

イテレータ修正

やっぱりお馬鹿してたわけで,pushを使えば十分な速さでした. use strict; use warnings; use Time::HiRes qw/gettimeofday tv_interval/; my $x=primefactory2(); my $sum; my $start = [gettimeofday]; while (1){ my $p=$x->(); last if $p>2000000; $su…

Problem 10

素数関係なので着手. 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.2,000,000未満のすべての素数の和を求めよ. [Haskell] 前に考えたものをそのま…

Problem 9

A Pythagorean triplet is a set of three natural numbers, [tex:$a 訳 ピタゴラス数とは自然数の三つ組で[tex:$a [Haskell] {- Project Euler Problem 9 e9.hs -} line:: Int -> [(Int,Int)] line k = [(i,k-i) | i<-[0..k], i*k == 500, i>k-i ] lattice:…

素数を列挙する4

「mod 6で1か5」を探索する方法でいってみます. use Benchmark; $x=primefactory2(); $count=5000; timethese($count, {'p1' => '$x->();', } ); sub next_cand{ $_[0] + 2 * ($_[0] % 6 ==5) + 4*($_[0] % 6 ==1); } sub primefactory2{ my @primes=(2,3,5…

素数を列挙する3

キーボードが小さくなって誤入力倍増キャンペーン開催中. ##単純なエラトステネスの篩 use Benchmark; $x=primefactory1(); $count=5000; $i=0; timethese($count, {'p1' => '$x->();', } ); sub primefactory1{ my @primes=(2,3); my $state=-1; return su…

素数を列挙する2

素数を列挙するは最適化の罠にはまったようです.ただいま原因の探求中(正しくは「自分の納得させる」作業中). すくなくとも「探索範囲を縮小するための処理のコスト」が「縮小による効果」を大幅に上回ったのは間違いないでしょう(grepは遅いということ…

キーボードが・・・

壊れてしまった.特定の,そしてよく使うキーを押しても入力されない.小型のキーボードに買い換えたら・・・小さすぎて入力に慣れるまでしんどい。。。

素数を列挙する

Haskellだと遅延評価で素数列に限らず「無限列なんでも来い」ですが,Perlだとどうなるでしょう.ここはMJD師匠(勝手に師匠よばわり)のHigher-Order Perlに助けを求めます.Perlでの「遅延評価」はコードリファレンス(サブルーチンへのリファレンス)とク…

Problem 8

Find the greatest product of five consecutive digits in the 1000-digit number.73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698…

GHCを更新した

GHCを6.8.2に更新してみました.どうやらデバッガがついたみたい.それと起動時のロゴがでないのがちょっとさびしい・・・.Project Euler Problem 8,なんか毛色が変わってすっきり思いつかない.Haskellの文字列の扱いがよくわかってないということだろう…

Problem 7

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? 訳 最初の六つの素数,2,3,5,7,11,13を列挙することで,6番目の素数が13であることがわかる.10001番目の素…

Problem 6

The sum of the squares of the first ten natural numbers is, 1^2 + 2^2 + ... + 10^2 = 385The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)^2 = 55^2 = 3025Hence the difference between the sum of the squares of the…

Problem 5

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までのそれぞれの数で割り切…

素因数分解

コメント欄でご指摘いただいたことを考えてみます.素因数分解をする際に素数の列として,エラトステネスの篩ではなく, primes = 2:3:([6,12..] >>= (\x->[x-1,x+1] ) を使うというテクニック.6の倍数の「前後」を「素数のタネ」として使うということです…