一般的なこと

割り算無しで円周率の計算

2009年7月2日

割り算は少し面倒なので、これを行わずに円周率を計算させる方法を考えてみた。

for ($max=2;$max<=2000000000;$max=$max*sqrt(10)) {
    $t=time();
    echo intval($max).': '.calc_pi(intval($max))."  (".(time()-$t).")\n";
}

function calc_pi($max) {
    $total=0;
    $max2=$max*$max;
    $prev_y=$max-1;
    for ($x=0;$x<$max;$x++) {
        $max2_x2=$max2-$x*$x;
        for ($y=$prev_y;0<=$y;$y--) {
            if ($y*$y < $max2_x2) break;
        }
        $total+=$y+1;
        $prev_y=$y;
    }
    return $total;
}


実行結果は、次のとおり。
2: 4  (0)
6: 33  (0)
20: 331  (0)
63: 3175  (0)
200: 31602  (0)
632: 314319  (0)
2000: 3143579  (0)
6324: 31416676  (0)
20000: 314179205  (0)
63245: 3141600733  (0)
200000: 31416126094  (1)
632455: 314159368708  (0)
2000000: 3141594652558  (3)
6324555: 31415929675774  (8)
20000000: 3.1415928535658E+14  (24)
63245553: 3.1415926966214E+15  (76)
200000000: 3.1415926756486E+16  (242)
632455532: 3.1415926602871E+17  (757)

ただ、2進数の数値を10進数で表示させるときに、割り算は必要になるのだけれど…。ただしこの場合、10で割るだけだから何か方法を考えてみよう。掛け算して、右シフトとか。

ちなみに、Wikipediaに載っている収束の早い方法で円周率を求めるスクリプトは、次の通り。
$prev='';
for ($max=1;$max<=1073741824;$max++) {
    $t=time();
    $pi=calc_pi($max);
    echo "$max: ".$pi."  (".(time()-$t).")\n";
    if ($pi===$prev) exit;
    $prev=$pi;
}

function calc_pi($max) {
    $fig=50;
    $pi='0';
    for($n=0;$n<$max;$n++){
        $y=bcdiv('3',bcadd('1',bcmul('2',$n,$fig),$fig),$fig);//$y=3/(2*$n+1);
        for($x=1;$x<=$n;$x++) $y=bcmul($y,bcdiv(bcadd($x,$n,$fig),bcmul('16',$x,$fig),$fig),$fig);//$y*=($x+$n)/(16*$x);
        $pi=bcadd($pi,$y,$fig);//$pi+=$y;
    }
    return $pi;
}

実行結果:
1: 3.00000000000000000000000000000000000000000000000000  (0)
2: 3.12500000000000000000000000000000000000000000000000  (0)
3: 3.13906250000000000000000000000000000000000000000000  (0)
4: 3.14115513392857142857142857142857142857142857142857  (0)
5: 3.14151117234002976190476190476190476190476190476190  (0)
6: 3.14157671577486640963203463203463203463203463203462  (0)
7: 3.14158942531912159292530386280386280386280386280385  (0)
8: 3.14159198235838245718311636280386280386280386280384  (0)
9: 3.14159251115786195576584413164577456856868621574501  (0)
10: 3.14159262287061749311263384304585680541079147890290  (0)
11: 3.14159264687556079607822377507885066701793433604575  (0)
12: 3.14159265210588688135876980275995259042146694474140  (0)
13: 3.14159265325873792265602348969466213937166225724139  (0)
14: 3.14159265351533760243194355890199599553044860706777  (0)
15: 3.14159265357293032612549212123332185179268852363902  (0)
16: 3.14159265358595061661642608384639847682401749402515  (0)
17: 3.14159265358891285600131896691248906315335157085779  (0)
18: 3.14159265358959056160849298995008835905936938801550  (0)
19: 3.14159265358974637793146374412070756654526875476065  (0)
20: 3.14159265358978236182926181471105468109455961729002  (0)
21: 3.14159265358979070504702871491957876055027172123928  (0)
22: 3.14159265358979264648516079676489008081121469559402  (0)
23: 3.14159265358979309973252269314519066555395251877355  (0)
24: 3.14159265358979320586410315569955105918300479582472  (0)
25: 3.14159265358979323078381629789582891946431607133556  (0)
26: 3.14159265358979323664972132283830569814426002942394  (0)
27: 3.14159265358979323803372159223919304405740718426681  (0)
28: 3.14159265358979323836096542698221430286801580025617  (0)
29: 3.14159265358979323843849617573672865968992142865277  (0)
30: 3.14159265358979323845689896669986550549021156502481  (0)
31: 3.14159265358979323846127465763857311206335432217720  (0)
32: 3.14159265358979323846231676887659993003945930165518  (0)
33: 3.14159265358979323846256533495254514882234155307033  (0)
34: 3.14159265358979323846262470806826589448932920075401  (0)
35: 3.14159265358979323846263890915077177829682640612762  (0)
36: 3.14159265358979323846264231012430349423281705978877  (0)
37: 3.14159265358979323846264312558794378057382395120362  (0)
38: 3.14159265358979323846264332133595004570496024509324  (0)
39: 3.14159265358979323846264336837467762778641281058918  (0)
40: 3.14159265358979323846264337968969774439469471016827  (0)
41: 3.14159265358979323846264338241412060503328604254763  (0)
42: 3.14159265358979323846264338307070811563605557427481  (0)
43: 3.14159265358979323846264338322908456383432165635004  (0)
44: 3.14159265358979323846264338326731865359081966634046  (0)
45: 3.14159265358979323846264338327655619908899808301619  (0)
46: 3.14159265358979323846264338327878973382315776673073  (0)
47: 3.14159265358979323846264338327933017039204440595583  (0)
48: 3.14159265358979323846264338327946102805957801577268  (1)
49: 3.14159265358979323846264338327949273420351838380846  (0)
50: 3.14159265358979323846264338327950042135844352612057  (0)
51: 3.14159265358979323846264338327950228625461732116515  (0)
52: 3.14159265358979323846264338327950273894369663020878  (0)
53: 3.14159265358979323846264338327950284889283679480685  (0)
54: 3.14159265358979323846264338327950287561187378816546  (0)
55: 3.14159265358979323846264338327950288210835387941934  (0)
56: 3.14159265358979323846264338327950288368871178204397  (0)
57: 3.14159265358979323846264338327950288407334337413938  (0)
58: 3.14159265358979323846264338327950288416700013995681  (0)
59: 3.14159265358979323846264338327950288418981569271129  (0)
60: 3.14159265358979323846264338327950288419537619160133  (0)
61: 3.14159265358979323846264338327950288419673194616024  (0)
62: 3.14159265358979323846264338327950288419706264059075  (0)
63: 3.14159265358979323846264338327950288419714333536557  (0)
64: 3.14159265358979323846264338327950288419716303377602  (0)
65: 3.14159265358979323846264338327950288419716784415123  (0)
66: 3.14159265358979323846264338327950288419716901927533  (0)
67: 3.14159265358979323846264338327950288419716930644645  (0)
68: 3.14159265358979323846264338327950288419716937664780  (1)
69: 3.14159265358979323846264338327950288419716939381476  (0)
70: 3.14159265358979323846264338327950288419716939801409  (0)
71: 3.14159265358979323846264338327950288419716939904164  (0)
72: 3.14159265358979323846264338327950288419716939929315  (0)
73: 3.14159265358979323846264338327950288419716939935472  (0)
74: 3.14159265358979323846264338327950288419716939936980  (0)
75: 3.14159265358979323846264338327950288419716939937349  (0)
76: 3.14159265358979323846264338327950288419716939937439  (0)
77: 3.14159265358979323846264338327950288419716939937461  (0)
78: 3.14159265358979323846264338327950288419716939937466  (0)
79: 3.14159265358979323846264338327950288419716939937467  (0)
80: 3.14159265358979323846264338327950288419716939937467  (0)

1秒以内に40桁以上。速い。これは、先日作った、pi calculatorに入れてみたいアルゴリズムだ。

コメント

lebron (2018年10月16日 21:10:07)

http://www.nikeairmax-90.us.com/ air max 90
http://www.kyrie-irving-shoes.us.com/ nike kyrie
http://www.air-max-97.us.com/ nike air max 97
http://www.pandorasjewelry.co.uk/ pandora charm
http://www.ultra--boost.us.com/ ultra boost
http://www.balenciagas-sneakers.us.com/ balenciaga sneakers
http://www.pandorajewelryrings.us.com/ pandora earrings
http://www.stephen-curry-shoes.us.com/ stephen curry
http://www.curry4-shoes.us.com/ stephen curry shoes
http://www.chromehearts-jewelry.us.com/ chrome hearts sunglasses
http://www.mbts.us.com/ mbt
http://www.adidasnmds.co.uk/ adidas nmd womens
http://www.adidas-superstarshoes.us.com/ adidas originals superstar
http://www.jimmychooheels.us.com/ jimmy choo trainers
http://www.oakley--sunglassess.us.com/ cheap oakley sunglasses
http://www.asics--shoes.us.com/ asics shoes
http://www.pandoras-jewelrys.us.com/ pandora charms
http://www.hermes--belt.us.com/ hermes belt men
http://www.monclers.uk/ moncler coat
http://www.skechersuk.co.uk/ skechers
http://www.salomon--shoes.us.com/ Salomon xa pro 3d
http://www.kevin-durant-shoes.us.com/ kd 10
http://www.swarovskis.us.com/ swarovski necklace
http://www.ralphlauren--polo.us.com/ ralph lauren outlet
http://www.supremeshirt.us.com/ supreme clothing
http://www.moncler-coat.us.com/ moncler sale
http://www.airjordan11spacejam.us.com/ jordan retro 11
http://www.jordan--11.us.com/ air jordan
http://www.uggs--boots.us.com/ uggs for women
http://www.longchampbag.us.com/ longchamp bag
http://www.nikeairvapormaxflyknit.us.com/ nike vapormax
http://www.pandorauk.us.com/ pandora uk
http://www.pandora--rings.us.com/ Pandora Bracelet
http://www.michael-kors-bags.us.com/ michael kors purse
http://www.lacosteshirts.us.com/ lacoste
http://www.fitflop--sandals.us.com/ fitflop
http://www.katespadepurses.us.com/ kate spade handbags
http://www.adidasoriginalsnmd.us.com/ adidas originals nmd
http://www.polo-ralphlauren-outlet.us.com/ polo ralph lauren
http://www.monclerjacket.org.uk/ moncler jacket
http://www.ed-hardys.us.com/ ed hardy
http://www.vapormaxs.us.com/ nike air vapormax
http://www.ray--bansunglassess.us.com/ ray ban
http://www.mulberry-handbags.us.com/ mulberry bag
http://www.lebron15-shoes.us.com/ basketball shoes
http://www.ralphlaurenspoloshirts.us.com/ ralph lauren shirts
http://www.pandoraearrings.us.com/ pandora bracelet
http://www.air-max-270.us.com/ nike air max 270 women
http://www.mcms.us.com/ mcm bags
http://www.mcm--bags.us.com/ mcm bags
http://www.asicss.us.com/ asics running shoes
http://www.jordan11.co.uk/ jordan 11
http://www.yeezyboost350shoes.us.com/ yeezy
http://www.nikeairmax97.co.uk/ nike air max
http://www.kyrie-4.us.com/ kyrie irving shoes
http://www.polo-ralph-lauren.org.uk/ ralph lauren polo shirts
http://www.ray--ban.us.com/ ray ban
http://www.pandora--jewelrys.us.com/ pandora rings
http://www.prada--bags.us.com/ prada
http://www.bvlgaris.us.com/ bvlgari ring
http://www.jordans11shoes.us.com/ jordan 11
http://www.michaelkors-handbagss.us.com/ michael kors handbags
http://www.montblancs.us.com/ mont blanc sale
http://www.valentino--shoes.us.com/ valentino heels
http://www.skecherss.us.com/ skechers shoes
http://www.burberrys.org.uk/ burberry handbags
http://www.huaraches.us.com/ nike air huarache
http://www.airmax-2018.us.com/ air max 2018
http://www.pandora-charmsjewelry.us.com/ pandora
http://www.ferragamoshoess.us.com/ ferragamo shoes
http://www.salvatore-ferragamo.us.com/ ferragamo
http://www.yeezyboost350--v2.us.com/ yeezy boost 350 v2
http://www.goyardbackpack.us.com/ goyard backpack
http://www.fitflopsshoes.us.com/ fitflop shoes
http://www.fitflop--shoes.us.com/ fitflop shoes
http://www.chrome--hearts.us.com/ chrome hearts
http://www.pandora-jewelrybracelet.us.com/ pandora,pandora charms
http://www.nikeairmax-97.us.com/ air max 97 silver bullet
http://www.ugg--slippers.us.com/ uggs for men
http://www.uggs-forwomen.us.com/ ugg slippers
http://www.monclerjacketmens.us.com/ moncler coat
http://www.barbours.org.uk/ barbour wax jacket
http://www.nikeair-max90.co.uk/ nike vapormax
http://www.adidasyeezy-350.us.com/ yeezy boost
http://www.katespadebags.us.com/ kate spade
http://www.nikeairmax-95.us.com/ nike air max 95 mens
http://www.pumarihannafenty.us.com/ fenty puma shoes
http://www.katespade--outlet.us.com/ kate spade handbags
http://www.northfacejacketoutlets.us.com/ north face coats
http://www.air-max-95.us.com/ air max 95 black
http://www.adidas-yeezyboost350.us.com/ yeezy
http://www.adidasnmdrunnerr1.us.com/ yeezy boost 350
http://www.kd10-shoes.us.com/ kevin durant shoes
http://www.burberry-bags.us.com/ burberry bags
http://www.oakley--glasses.us.com/ oakley sunglasses
http://www.adidas-ultraboost.us.com/ ultra boost
http://www.celines.us.com/ celine handbags
http://www.chromeheartsjewelrys.us.com/ chrome hearts bracelet
http://www.swarovskisjewelry.us.com/ swarovski pen
http://www.coachbags-outlet.us.com/ coach handbags
http://www.truereligion--jeans.us.com/ true religion jeans
http://www.pandoras-uk.co.uk/ pandora uk
http://www.fitflops-sale.us.com/ fitflop shoes
http://www.nikeair-max.co.uk/ nike air max 270
http://www.salomonspeedcross3.us.com/ salomon xa pro 3d
http://www.louboutinredbottoms.us.com/ christian louboutin
http://www.nike-airmax270.us.com/ air max 270
http://www.monclerjacketwomens.us.com/ moncler coat
http://www.mulberrys.org.uk/ mulberry
http://www.pandorajewelryscharms.us.com/ pandora jewelry
http://www.airmax270.co.uk/ air max 270
http://www.goyardbag.us.com/ goyard backpack
http://www.yeezyboost350v2.co.uk/ adidas yeezy
http://www.birkenstocksandalssale.us.com/ birkenstock sandals
http://www.coach--bags.us.com/ coach purses
http://www.hermesbirkin-handbags.us.com/ Hermes Wallets
http://www.pandora-jewelrysale.us.com/ pandora charms
http://www.nikelebron15.us.com/ lebron
http://www.barbourjackets.co.uk/ barbour wax jacket
http://www.adidasnmd-womens.us.com/ adidas originals nmd
http://www.uggsformen.us.com/ ugg boots
http://www.redbottoms-louboutin.us.com/ christian louboutin
http://www.nike-huaraches.us.com/ nike huarache
http://www.longchampbackpack.us.com/ longchamp backpack
http://www.skechers--shoes.us.com/ skechers women
http://www.james-hardenshoes.us.com/ james harden shoes
http://www.adidas-yeezy-shoes.us.com/ Yeezy Boost 350 V2
http://www.nike-airmax-2018.us.com/ air max 2018
http://www.new--balance.us.com/ new balance
http://www.prada--handbags.us.com/ prada purse
http://www.eccoshoesoutlets.us.com/ ecco outlet
http://www.northface-jacket.us.com/ womens north face jacket
http://www.salomonshoes.co.uk/ salomon shoes
http://www.airmax95.co.uk/ nike air max 97
http://www.supreme--clothing.us.com/ supreme shoes
http://www.nikeairmax270.org.uk/ nike air max 97
http://www.adidas--shoes.co.uk/ adidas shoes
http://www.true--religion.us.com/ true religion outlet
http://www.rayban--glasses.us.com/ ray ban wayfarer
http://www.cartier-bracelet.us.com/ cartier sunglasses
http://www.christianlouboutinredbottoms.us.com/ christian louboutin
http://www.nike--shoes.co.uk/ nike air max 97
http://www.redbottom--shoes.us.com/ red bottom shoes
http://www.nikekdshoes.us.com/ kevin durant shoes
http://www.salomon-boots.us.com/ salomon shoes
http://www.adidasyeezys.org.uk/ yeezy boost 350
http://www.adidasoriginalsultraboost.us.com/ adidas ultra boost uncaged
http://www.airmax90shoes.us.com/ nike air max
http://www.balenciaga--shoes.us.com/ balenciaga
http://www.nike--vapormax.us.com/ nike air vapormax
http://www.under-armour-shoes.us.com/ under armour outlet
http://www.burberry--scarf.us.com/ burberry scarf
http://www.rayban-glasses.us.com/ ray ban glasses
http://www.jordan11spacejams.us.com/ jordan retro 12
http://www.air-max-90.us.com/ nike air max 90
http://www.moncler--jacket.us.com/ moncler jacket womens
http://www.birkenstock--sandals.us.com/ birkenstock outlet
http://www.jordanretro11-shoes.us.com/ jordan
私が忙しくなり

air max 270 (2019年1月14日 00:05:26)

http://www.jordanretro11.in.net/ jordan retro
http://www.christianlouboutins.fr/ louboutin homme
http://www.jordan-11.co.uk/ jordan shoes
http://www.pandorajewelrycharms.org.uk/ pandora charms
http://www.christianlouboutins.ca/ louboutin
http://www.salomonshoes.ca/ salomon shoes
http://www.adidaszxflux.us/ adidas flux
http://www.giuseppezanottis.us/ isabel marant
もできなく

コメント送信