Skip to content

Commit 21edf8d

Browse files
committed
0.1.3 statHelpers
1 parent 5112216 commit 21edf8d

File tree

8 files changed

+695
-8
lines changed

8 files changed

+695
-8
lines changed

libraries/statHelpers/README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,21 @@ other same code examples exist.
4848
but accuracy is less than the **dfactorial()**, see example.
4949

5050

51-
### Combination
51+
### SemiFactorial
52+
53+
- **uint32_t semiFactorial(n)** exact up to 20!!
54+
- **uint64_t semiFactorial64(n)** exact up to 33!! (Print 64 bit integers with my printHelpers)
55+
- **double dSemiFactorial(n)** not exact up to 56!! (4 byte) or 300!! (8 byte)
56+
57+
SemiFactorial are defined for
58+
- **odd** values: n x (n-2) x (n-4) ... x 1
59+
- **even** values: n x (n-2) x (n-4) ... x 2
60+
61+
Notes:
62+
```n! = n!! x (n-1)!!``` this formula allows to calculate the value of n! indirectly
63+
64+
65+
### Combinations
5266

5367
returns how many different ways one can choose a set of k elements
5468
from a set of n. The order does not matter.
@@ -78,10 +92,34 @@ It allows you to process every instance.
7892
It is added to this library as it fits in the context.
7993

8094

95+
### Experimental - large numbers
96+
97+
- **void bigFactorial(uint32_t n, double &mantissa, uint32_t &exponent)** returns a double mantissa between 0 and 10, and an integer exponent.
98+
- **void bigPermutation(uint32_t n, uint32_t k, double &mantissa, uint32_t &exponent)** returns a double mantissa between 0 and 10, and an integer exponent.
99+
- **void bigCombination(uint32_t n, uint32_t k, double &mantissa, uint32_t &exponent)** returns a double mantissa between 0 and 10, and an integer exponent.
100+
101+
An experimental **bigFactorial(n)** calculation to get an idea of the big numbers. it can calculate factorials up to an exponent of 4294967295 max. 100.000.000! can be done in 38 minutes on an ESP32 @240 Mhz. Maximum value for n is **518678059! == 2.1718890e4294967292** a number that took ~10 hrs to calculate.
102+
103+
An experimental **bigPermutation(n, k)** calculation, to handle big numbers too. Maximum value for n is **518678059** to have full range support. For small(er) values of k, n can even be much larger, but not larger than 4294967295 max.
104+
105+
An experimental **bigCombination(n, k)** calculation for big numbers. Not investigated what its maximum value is, but it should be higher than **518678059** as the number of combinations is always smaller than number of permutations.
106+
107+
108+
#### Experimental - not investigated yet
109+
110+
To have support for huge numbers one could upgrade the code to use uint64_t as parameter and internally but calculating these values could take a lot of time, although **bigPermutations64(n, k)** and **bigCombinations64(n, k)** would work fast for small values of k.
111+
112+
- **void bigFactorial64(uint64_t n, double &mantissa, uint64_t &exponent)** returns a double mantissa between 0 and 10, and an integer exponent.
113+
- **void bigPermutation64(uint64_t n, uint64_t k, double &mantissa, uint64_t &exponent)** returns a double mantissa between 0 and 10, and an integer exponent.
114+
- **void bigCombination64(uint64_t n, uint64_t k, double &mantissa, uint64_t &exponent)** returns a double mantissa between 0 and 10, and an integer exponent.
115+
116+
117+
81118
## Future
82119

83120
- code & example for get Nth Permutation
84121
- investigate valid range detection for a given (n, k) for combinations and permutations.
122+
- investigate a bigFloat class to do math for permutations and combinations to substantially larger values.
85123

86124

87125
## Operation
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//
2+
// FILE: bigCombinations.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// PURPOSE: demo
6+
// DATE: 2021-08-06
7+
// URL: https://github.com/RobTillaart/statHelpers
8+
9+
10+
#include "statHelpers.h"
11+
#include "printHelpers.h"
12+
13+
14+
uint32_t start, duration1;
15+
volatile uint32_t x;
16+
17+
void setup()
18+
{
19+
Serial.begin(115200);
20+
Serial.println(__FILE__);
21+
22+
Serial.println("dcombinations(n, k); ");
23+
delay(10);
24+
start = micros();
25+
uint32_t nmax = 125;
26+
if (sizeof(double) == 8) nmax = 1020;
27+
for (int n = nmax - 2; n < nmax + 1; n++)
28+
{
29+
for (int k = n / 2 - 1 ; k <= n / 2 + 1; k++)
30+
{
31+
double xx = dcombinations(n, k);
32+
Serial.print(n);
33+
Serial.print('\t');
34+
Serial.print(k);
35+
Serial.print('\t');
36+
Serial.print(sci(xx, 15));
37+
Serial.println();
38+
}
39+
//Serial.println();
40+
}
41+
duration1 = micros() - start;
42+
Serial.println(duration1);
43+
Serial.println();
44+
45+
46+
Serial.println("bigCombinations(n, k, mant, expo); ");
47+
delay(10);
48+
start = micros();
49+
nmax = 10000000;
50+
for (uint32_t n = nmax - 2; n < nmax + 1; n++)
51+
{
52+
for (uint32_t k = n / 2 - 1 ; k <= n / 2 + 1; k++)
53+
{
54+
double mant = 0;
55+
uint32_t expo = 0;
56+
bigCombinations(n, k, mant, expo);
57+
Serial.print(n);
58+
Serial.print('\t');
59+
Serial.print(k);
60+
Serial.print('\t');
61+
Serial.print(mant, 15);
62+
Serial.print('E');
63+
Serial.print(expo);
64+
Serial.println();
65+
}
66+
//Serial.println();
67+
}
68+
duration1 = micros() - start;
69+
Serial.println(duration1);
70+
Serial.println();
71+
72+
Serial.println("\nDone...");
73+
}
74+
75+
void loop()
76+
{
77+
}
78+
79+
// -- END OF FILE --
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//
2+
// FILE: bigFactorial.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// PURPOSE: demo
6+
// DATE: 2021-08-05
7+
// URL: https://github.com/RobTillaart/statHelpers
8+
9+
10+
#include "statHelpers.h"
11+
#include "printHelpers.h" // to print large numbers....
12+
13+
14+
uint32_t start, duration1, duration2, duration3;
15+
16+
17+
void setup()
18+
{
19+
Serial.begin(115200);
20+
Serial.println(__FILE__);
21+
22+
uint32_t m = 10000001;
23+
double mant = 0;
24+
uint32_t expo = 0;
25+
26+
for (uint32_t i = 1; i < m; i *= 10)
27+
{
28+
start = millis();
29+
bigFactorial(i, mant, expo);
30+
duration1 = millis() - start;
31+
Serial.print(i);
32+
Serial.print('\t');
33+
Serial.print(mant, 5);
34+
Serial.print('e');
35+
Serial.print(expo);
36+
Serial.print('\t');
37+
Serial.print(duration1);
38+
Serial.println();
39+
}
40+
Serial.println("-----");
41+
Serial.println();
42+
43+
m = 5;
44+
mant = 0;
45+
expo = 0;
46+
double f = 24;
47+
bool flag = false;
48+
while (flag == false)
49+
{
50+
f *= m++;
51+
while (f > 10)
52+
{
53+
f /= 10;
54+
expo++;
55+
if (expo > 0xFFFFFFF0)
56+
{
57+
flag = true;
58+
}
59+
}
60+
}
61+
mant = f;
62+
63+
Serial.println();
64+
Serial.print(m);
65+
Serial.print('\t');
66+
Serial.print(mant, 15);
67+
Serial.print('e');
68+
Serial.print(expo);
69+
Serial.println();
70+
71+
72+
Serial.println("\n Done...");
73+
}
74+
75+
void loop()
76+
{
77+
}
78+
79+
// -- END OF FILE --
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
//
2+
// FILE: permutations.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// PURPOSE: demo
6+
// DATE: 2021-08-06
7+
// URL: https://github.com/RobTillaart/statHelpers
8+
9+
10+
#include "statHelpers.h"
11+
#include "printHelpers.h"
12+
13+
14+
uint32_t start, duration1, duration2;
15+
volatile uint32_t x;
16+
17+
void setup()
18+
{
19+
Serial.begin(115200);
20+
Serial.println(__FILE__);
21+
22+
///////////////////////////////////////////
23+
24+
Serial.println("permutations64(n, k)");
25+
start = micros();
26+
for (int n = 0; n < 20; n++)
27+
{
28+
for (int k = 0; k <= n; k++)
29+
{
30+
// x = permutations64(n, k);
31+
Serial.print(n);
32+
Serial.print('\t');
33+
Serial.print(k);
34+
Serial.print('\t');
35+
Serial.print(print64(permutations64(n, k)));
36+
Serial.println();
37+
}
38+
Serial.println();
39+
}
40+
duration1 = micros() - start;
41+
Serial.println(duration1);
42+
Serial.println();
43+
44+
///////////////////////////////////////////
45+
//
46+
// tests are commented as they take serious time.
47+
48+
/*
49+
Serial.println("dpermutations(n, k) - double can be 4 or 8 bit.");
50+
start = micros();
51+
int nmax = 34;
52+
if (sizeof(double) == 8) nmax = 170; // Exponent overflow in double
53+
for (int n = 0; n <= nmax; n++)
54+
{
55+
for (int k = 0; k <= n; k++)
56+
{
57+
// x = dpermutations(n, k);
58+
Serial.print(n);
59+
Serial.print('\t');
60+
Serial.print(k);
61+
Serial.print('\t');
62+
Serial.print(sci(dpermutations(n, k), 15));
63+
Serial.println();
64+
}
65+
Serial.println();
66+
}
67+
duration1 = micros() - start;
68+
Serial.println(duration1);
69+
Serial.println();
70+
*/
71+
72+
/* takes 40 seconds ...
73+
Serial.println("bigPermutations(n, k, mant, expo)");
74+
start = micros();
75+
nmax = 30;
76+
// if (sizeof(double) == 8) nmax = 170;
77+
for (int n = 0; n <= nmax; n++)
78+
{
79+
for (int k = 0; k <= n; k++)
80+
{
81+
double mant = 0;
82+
uint32_t expo = 0;
83+
bigPermutations(n, k, mant, expo);
84+
Serial.print(n);
85+
Serial.print('\t');
86+
Serial.print(k);
87+
Serial.print('\t');
88+
Serial.print(mant, 15);
89+
Serial.print('E');
90+
Serial.print(expo);
91+
Serial.println();
92+
}
93+
Serial.println();
94+
}
95+
duration1 = micros() - start;
96+
Serial.println(duration1);
97+
Serial.println();
98+
*/
99+
100+
/*
101+
Serial.println("bigPermutations(n, k, mant, expo)");
102+
start = micros();
103+
for (int n = 1000; n <= 10000; n += 1000)
104+
{
105+
for (int k = 0; k <= n; k += n / 4)
106+
{
107+
double mant = 0;
108+
uint32_t expo = 0;
109+
bigPermutations(n, k, mant, expo);
110+
Serial.print(n);
111+
Serial.print('\t');
112+
Serial.print(k);
113+
Serial.print('\t');
114+
Serial.print(mant, 15);
115+
Serial.print('E');
116+
Serial.print(expo);
117+
Serial.println();
118+
}
119+
Serial.println();
120+
}
121+
duration1 = micros() - start;
122+
Serial.println(duration1);
123+
Serial.println();
124+
*/
125+
126+
// pick 1000 persons from 4294967295 (maxLong)
127+
double mant = 0;
128+
uint32_t expo = 0;
129+
bigPermutations(4294967295, 1000, mant, expo);
130+
Serial.print(mant, 15);
131+
Serial.print('E');
132+
Serial.print(expo);
133+
Serial.println();
134+
135+
Serial.println("\nDone...");
136+
}
137+
138+
void loop()
139+
{
140+
141+
}
142+
143+
// -- END OF FILE --

0 commit comments

Comments
 (0)