-
Notifications
You must be signed in to change notification settings - Fork 0
/
pidigits.php-3.php
58 lines (50 loc) · 1 KB
/
pidigits.php-3.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/* The Great Computer Language Shootout
http://shootout.alioth.debian.org/
contributed by Oleksii Prudkyi
port from pidigits.lua-5.lua (Mike Pall, Wim Couwenberg)
*/
$N = (int) $argv[1];
ob_start(null, 1);
$w = gmp_init(0);
$k = 1;
$n1 = gmp_init(4);
$n2 = gmp_init(3);
$d = gmp_init(1);
$i = 0;
while(true) {
//digit
$u = gmp_div_q($n1, $d);
$v = gmp_div_q($n2, $d);
if(gmp_cmp($u, $v) == 0) {
echo gmp_strval($u);
++$i;
if($i % 10 == 0) {
echo "\t:" , $i , "\n";
}
if($i == $N) {
break;
}
//extract
$u = gmp_mul($d, gmp_mul(-10, $u));
$n1 = gmp_mul($n1, 10);
$n1 = gmp_add($n1, $u);
$n2 = gmp_mul($n2, 10);
$n2 = gmp_add($n2, $u);
} else {
//produce
$k2 = $k << 1;
$u = gmp_mul($n1, $k2 - 1);
$v = gmp_add($n2, $n2);
$w = gmp_mul($n1, $k - 1);
$n1 = gmp_add($u, $v);
$u = gmp_mul($n2, $k + 2);
$n2 = gmp_add($w, $u);
$d = gmp_mul($d, $k2 + 1);
++$k;
}
}
if($i % 10 != 0) {
echo str_repeat(' ', 10 - $N % 10), "\t:", $N, "\n";
}
ob_end_flush();