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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//! Sums and products of iterators of numbers: [`Sum`] and [`Product`], of owned or borrowed
//! numbers.
//!
//! A sum is built in place, each number added to the sum so far. A product is built as a balanced
//! tree, so that the factors of each multiplication are about as long as each other: the product
//! of n numbers of one limb each, multiplied in turn, takes O(n²) time, as each step multiplies the
//! whole product so far by one limb; in a tree, its last steps are products of halves, which
//! Karatsuba's and Toom-Cook's methods multiply in less. Measured on an Apple M4 Max, the product
//! of the numbers from 1 to 10,000 took 10.5 million cycles in turn and 1.9 million as a tree, and
//! from 1 to 1,000 253,000 and 134,000.
use trim;
use ;
use ;
/// A zero or one, little-endian, the sum or product of no numbers.
/// The sum of the numbers of `iter`, in the byte order of the first and in the shortest form.
/// The product of the numbers of `iter`, multiplied as a balanced tree, in the byte order of the
/// first and in the shortest form.
///
/// The partial products wait on a stack, each multiplied with the one below it while that one is
/// not longer, as a binary counter carries: so the product of n numbers of equal length is a
/// complete binary tree, and of numbers of different lengths one in which the factors of each
/// product are still of about equal length. The earlier factor is always the left one, so that the
/// result has the byte order of the first number.