aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-07-05 19:23:58 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2024-07-05 19:23:58 +0200
commit5fb58d73581256c64d47250c5544d1cbf22f79c3 (patch)
tree2d5774c0e31dc095cc70a010cad908e431736233
parent518d98ad5f9eec0cf4124375bdbb84d1296b3f3f (diff)
downloadnissy-core-5fb58d73581256c64d47250c5544d1cbf22f79c3.tar.gz
nissy-core-5fb58d73581256c64d47250c5544d1cbf22f79c3.zip
Added more math utilities
Diffstat (limited to '')
-rw-r--r--TODO.txt5
-rw-r--r--src/utils.h58
-rw-r--r--test/013_math_digitstosumzero/00_bad_n.in5
-rw-r--r--test/013_math_digitstosumzero/00_bad_n.out1
-rw-r--r--test/013_math_digitstosumzero/01_bad_b12.in14
-rw-r--r--test/013_math_digitstosumzero/01_bad_b12.out1
-rw-r--r--test/013_math_digitstosumzero/02_bad_b8.in10
-rw-r--r--test/013_math_digitstosumzero/02_bad_b8.out1
-rw-r--r--test/013_math_digitstosumzero/03_zero12.in14
-rw-r--r--test/013_math_digitstosumzero/03_zero12.out1
-rw-r--r--test/013_math_digitstosumzero/04_zero8.in10
-rw-r--r--test/013_math_digitstosumzero/04_zero8.out1
-rw-r--r--test/013_math_digitstosumzero/05_max12.in14
-rw-r--r--test/013_math_digitstosumzero/05_max12.out1
-rw-r--r--test/013_math_digitstosumzero/06_max8.in10
-rw-r--r--test/013_math_digitstosumzero/06_max8.out1
-rw-r--r--test/013_math_digitstosumzero/07_random12.in14
-rw-r--r--test/013_math_digitstosumzero/07_random12.out1
-rw-r--r--test/013_math_digitstosumzero/08_random8.in10
-rw-r--r--test/013_math_digitstosumzero/08_random8.out1
-rw-r--r--test/013_math_digitstosumzero/09_sumnonzero.in10
-rw-r--r--test/013_math_digitstosumzero/09_sumnonzero.out1
-rw-r--r--test/013_math_digitstosumzero/digitstosumzero_tests.c21
-rw-r--r--test/014_math_sumzerotodigits/00_bad_n.in3
-rw-r--r--test/014_math_sumzerotodigits/00_bad_n.out13
-rw-r--r--test/014_math_sumzerotodigits/01_bad_b12.in3
-rw-r--r--test/014_math_sumzerotodigits/01_bad_b12.out12
-rw-r--r--test/014_math_sumzerotodigits/02_bad_b8.in3
-rw-r--r--test/014_math_sumzerotodigits/02_bad_b8.out8
-rw-r--r--test/014_math_sumzerotodigits/03_zero12.in3
-rw-r--r--test/014_math_sumzerotodigits/03_zero12.out12
-rw-r--r--test/014_math_sumzerotodigits/04_zero8.in3
-rw-r--r--test/014_math_sumzerotodigits/04_zero8.out8
-rw-r--r--test/014_math_sumzerotodigits/05_max12.in3
-rw-r--r--test/014_math_sumzerotodigits/05_max12.out12
-rw-r--r--test/014_math_sumzerotodigits/06_max8.in3
-rw-r--r--test/014_math_sumzerotodigits/06_max8.out8
-rw-r--r--test/014_math_sumzerotodigits/07_random12.in3
-rw-r--r--test/014_math_sumzerotodigits/07_random12.out12
-rw-r--r--test/014_math_sumzerotodigits/08_random8.in3
-rw-r--r--test/014_math_sumzerotodigits/08_random8.out8
-rw-r--r--test/014_math_sumzerotodigits/sumzerotodigits.c21
42 files changed, 345 insertions, 1 deletions
diff --git a/TODO.txt b/TODO.txt
index b625e12..abcefc8 100644
--- a/TODO.txt
+++ b/TODO.txt
@@ -3,6 +3,8 @@ Check stats for all tables using H48stats solver
3 x implement stuff in utils.h 3 x implement stuff in utils.h
4 x implement functions 4 x implement functions
5 x unit tests for permtoindex and indextoperm 5 x unit tests for permtoindex and indextoperm
6 x implement digit array to sumzero and inverse
7 x unit tests for digit arra to sumzero
6 x move cubefromarray from cube_io to where needed (cube_generic) 8 x move cubefromarray from cube_io to where needed (cube_generic)
7 - implement in cube_generic 9 - implement in cube_generic
8 x getcube_fix 10 x getcube_fix
@@ -60,6 +62,9 @@ Improvements
60small things 62small things
61 - maybe move part of the logic for coord_h48 (and its inverse) to 63 - maybe move part of the logic for coord_h48 (and its inverse) to
62 utils.h (subsettoindex-like) 64 utils.h (subsettoindex-like)
65 - rename TYPE build switch to something more intuitive like ARCH
66 remove one of TYPE and CUBE_TYPE (why do I have two?)
67 - merge constants and utils?
63 68
64## H48 optimal solver (some has already been implemented) 69## H48 optimal solver (some has already been implemented)
65 70
diff --git a/src/utils.h b/src/utils.h
index 0c0b4d1..3d35437 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -3,6 +3,8 @@ _static bool isperm(uint8_t *, int64_t);
3_static int64_t permtoindex(uint8_t *, int64_t); 3_static int64_t permtoindex(uint8_t *, int64_t);
4_static void indextoperm(int64_t, int64_t, uint8_t *); 4_static void indextoperm(int64_t, int64_t, uint8_t *);
5_static int permsign(uint8_t *, int64_t); 5_static int permsign(uint8_t *, int64_t);
6_static int64_t digitstosumzero(uint8_t *, uint8_t, uint8_t);
7_static void sumzerotodigits(int64_t, uint8_t, uint8_t, uint8_t *);
6 8
7_static int64_t 9_static int64_t
8factorial(int64_t n) 10factorial(int64_t n)
@@ -39,7 +41,7 @@ isperm(uint8_t *a, int64_t n)
39 memset(aux, false, n); 41 memset(aux, false, n);
40 42
41 for (i = 0; i < n; i++) { 43 for (i = 0; i < n; i++) {
42 if (a[i] < 0 || a[i] >= n) 44 if (a[i] >= n)
43 return false; 45 return false;
44 else 46 else
45 aux[a[i]] = true; 47 aux[a[i]] = true;
@@ -123,3 +125,57 @@ permsign(uint8_t *a, int64_t n)
123 125
124 return ret % 2; 126 return ret % 2;
125} 127}
128
129_static int64_t
130digitstosumzero(uint8_t *a, uint8_t n, uint8_t b)
131{
132 int64_t ret, p;
133 uint8_t i, sum;
134
135 if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) {
136 LOG("Won't compute 'sumzero' for n=%" PRIu8 "and b=%" PRIu8
137 " (use n=8 b=3 or n=12 b=2)\n", n, b);
138 return -1;
139 }
140
141 for (i = 1, ret = 0, p = 1; i < n; i++, p *= (int64_t)b) {
142 if (a[i] >= b) {
143 LOG("Error: digit %" PRIu8 " larger than maximum"
144 " (b=%" PRIu8 "\n", a[i], b);
145 return -1;
146 }
147 sum += a[i];
148 ret += p * (int64_t)a[i];
149 }
150
151 if ((sum + a[0]) % b != 0) {
152 LOG("Error: digits do not have sum zero modulo b\n");
153 return -1;
154 }
155
156 return ret;
157}
158
159_static void
160sumzerotodigits(int64_t d, uint8_t n, uint8_t b, uint8_t *a)
161{
162 uint8_t sum;
163 int64_t i;
164
165 if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) {
166 LOG("Won't compute 'digits' for n=%" PRIu8 "and b=%" PRIu8
167 " (use n=8 b=3 or n=12 b=2)\n");
168 goto digitstosumzero_error;
169 }
170
171 for (i = 1; i < n; i++, d /= (int64_t)b) {
172 a[i] = (uint8_t)(d % (int64_t)b);
173 sum += a[i];
174 }
175 a[0] = (b - (sum % b)) % b;
176
177 return;
178
179digitstosumzero_error:
180 memset(a, _error, n);
181}
diff --git a/test/013_math_digitstosumzero/00_bad_n.in b/test/013_math_digitstosumzero/00_bad_n.in
new file mode 100644
index 0000000..edcdc1b
--- /dev/null
+++ b/test/013_math_digitstosumzero/00_bad_n.in
@@ -0,0 +1,5 @@
13
24
30
41
50
diff --git a/test/013_math_digitstosumzero/00_bad_n.out b/test/013_math_digitstosumzero/00_bad_n.out
new file mode 100644
index 0000000..3a2e3f4
--- /dev/null
+++ b/test/013_math_digitstosumzero/00_bad_n.out
@@ -0,0 +1 @@
-1
diff --git a/test/013_math_digitstosumzero/01_bad_b12.in b/test/013_math_digitstosumzero/01_bad_b12.in
new file mode 100644
index 0000000..26bb819
--- /dev/null
+++ b/test/013_math_digitstosumzero/01_bad_b12.in
@@ -0,0 +1,14 @@
112
23
30
41
52
60
71
82
92
101
110
120
130
141
diff --git a/test/013_math_digitstosumzero/01_bad_b12.out b/test/013_math_digitstosumzero/01_bad_b12.out
new file mode 100644
index 0000000..3a2e3f4
--- /dev/null
+++ b/test/013_math_digitstosumzero/01_bad_b12.out
@@ -0,0 +1 @@
-1
diff --git a/test/013_math_digitstosumzero/02_bad_b8.in b/test/013_math_digitstosumzero/02_bad_b8.in
new file mode 100644
index 0000000..146d291
--- /dev/null
+++ b/test/013_math_digitstosumzero/02_bad_b8.in
@@ -0,0 +1,10 @@
18
22
30
40
50
60
70
80
90
101
diff --git a/test/013_math_digitstosumzero/02_bad_b8.out b/test/013_math_digitstosumzero/02_bad_b8.out
new file mode 100644
index 0000000..3a2e3f4
--- /dev/null
+++ b/test/013_math_digitstosumzero/02_bad_b8.out
@@ -0,0 +1 @@
-1
diff --git a/test/013_math_digitstosumzero/03_zero12.in b/test/013_math_digitstosumzero/03_zero12.in
new file mode 100644
index 0000000..8afbb56
--- /dev/null
+++ b/test/013_math_digitstosumzero/03_zero12.in
@@ -0,0 +1,14 @@
112
22
30
40
50
60
70
80
90
100
110
120
130
140
diff --git a/test/013_math_digitstosumzero/03_zero12.out b/test/013_math_digitstosumzero/03_zero12.out
new file mode 100644
index 0000000..573541a
--- /dev/null
+++ b/test/013_math_digitstosumzero/03_zero12.out
@@ -0,0 +1 @@
0
diff --git a/test/013_math_digitstosumzero/04_zero8.in b/test/013_math_digitstosumzero/04_zero8.in
new file mode 100644
index 0000000..a49222d
--- /dev/null
+++ b/test/013_math_digitstosumzero/04_zero8.in
@@ -0,0 +1,10 @@
18
23
30
40
50
60
70
80
90
100
diff --git a/test/013_math_digitstosumzero/04_zero8.out b/test/013_math_digitstosumzero/04_zero8.out
new file mode 100644
index 0000000..573541a
--- /dev/null
+++ b/test/013_math_digitstosumzero/04_zero8.out
@@ -0,0 +1 @@
0
diff --git a/test/013_math_digitstosumzero/05_max12.in b/test/013_math_digitstosumzero/05_max12.in
new file mode 100644
index 0000000..5daa5b0
--- /dev/null
+++ b/test/013_math_digitstosumzero/05_max12.in
@@ -0,0 +1,14 @@
112
22
31
41
51
61
71
81
91
101
111
121
131
141
diff --git a/test/013_math_digitstosumzero/05_max12.out b/test/013_math_digitstosumzero/05_max12.out
new file mode 100644
index 0000000..b6ac305
--- /dev/null
+++ b/test/013_math_digitstosumzero/05_max12.out
@@ -0,0 +1 @@
2047
diff --git a/test/013_math_digitstosumzero/06_max8.in b/test/013_math_digitstosumzero/06_max8.in
new file mode 100644
index 0000000..d2a9333
--- /dev/null
+++ b/test/013_math_digitstosumzero/06_max8.in
@@ -0,0 +1,10 @@
18
23
31
42
52
62
72
82
92
102
diff --git a/test/013_math_digitstosumzero/06_max8.out b/test/013_math_digitstosumzero/06_max8.out
new file mode 100644
index 0000000..fdc6491
--- /dev/null
+++ b/test/013_math_digitstosumzero/06_max8.out
@@ -0,0 +1 @@
2186
diff --git a/test/013_math_digitstosumzero/07_random12.in b/test/013_math_digitstosumzero/07_random12.in
new file mode 100644
index 0000000..44bb31d
--- /dev/null
+++ b/test/013_math_digitstosumzero/07_random12.in
@@ -0,0 +1,14 @@
112
22
30
41
50
61
70
81
91
101
110
121
130
140
diff --git a/test/013_math_digitstosumzero/07_random12.out b/test/013_math_digitstosumzero/07_random12.out
new file mode 100644
index 0000000..a5c3fde
--- /dev/null
+++ b/test/013_math_digitstosumzero/07_random12.out
@@ -0,0 +1 @@
373
diff --git a/test/013_math_digitstosumzero/08_random8.in b/test/013_math_digitstosumzero/08_random8.in
new file mode 100644
index 0000000..1d97656
--- /dev/null
+++ b/test/013_math_digitstosumzero/08_random8.in
@@ -0,0 +1,10 @@
18
23
31
41
50
62
70
80
90
102
diff --git a/test/013_math_digitstosumzero/08_random8.out b/test/013_math_digitstosumzero/08_random8.out
new file mode 100644
index 0000000..3ffc420
--- /dev/null
+++ b/test/013_math_digitstosumzero/08_random8.out
@@ -0,0 +1 @@
1477
diff --git a/test/013_math_digitstosumzero/09_sumnonzero.in b/test/013_math_digitstosumzero/09_sumnonzero.in
new file mode 100644
index 0000000..9466f9a
--- /dev/null
+++ b/test/013_math_digitstosumzero/09_sumnonzero.in
@@ -0,0 +1,10 @@
18
23
31
42
51
61
71
81
92
102
diff --git a/test/013_math_digitstosumzero/09_sumnonzero.out b/test/013_math_digitstosumzero/09_sumnonzero.out
new file mode 100644
index 0000000..3a2e3f4
--- /dev/null
+++ b/test/013_math_digitstosumzero/09_sumnonzero.out
@@ -0,0 +1 @@
-1
diff --git a/test/013_math_digitstosumzero/digitstosumzero_tests.c b/test/013_math_digitstosumzero/digitstosumzero_tests.c
new file mode 100644
index 0000000..f28c02a
--- /dev/null
+++ b/test/013_math_digitstosumzero/digitstosumzero_tests.c
@@ -0,0 +1,21 @@
1#include "../test.h"
2
3int64_t digitstosumzero(uint8_t *, uint8_t, uint8_t);
4
5void run(void) {
6 char str[STRLENMAX];
7 uint8_t i, n, b, a[100];
8 int64_t p;
9
10 fgets(str, STRLENMAX, stdin);
11 n = atoi(str);
12 fgets(str, STRLENMAX, stdin);
13 b = atoi(str);
14 for (i = 0; i < n; i++) {
15 fgets(str, STRLENMAX, stdin);
16 a[i] = atoi(str);
17 }
18
19 p = digitstosumzero(a, n, b);
20 printf("%" PRId64 "\n", p);
21}
diff --git a/test/014_math_sumzerotodigits/00_bad_n.in b/test/014_math_sumzerotodigits/00_bad_n.in
new file mode 100644
index 0000000..5e34db5
--- /dev/null
+++ b/test/014_math_sumzerotodigits/00_bad_n.in
@@ -0,0 +1,3 @@
113
24
32
diff --git a/test/014_math_sumzerotodigits/00_bad_n.out b/test/014_math_sumzerotodigits/00_bad_n.out
new file mode 100644
index 0000000..f9c0ae0
--- /dev/null
+++ b/test/014_math_sumzerotodigits/00_bad_n.out
@@ -0,0 +1,13 @@
1255
2255
3255
4255
5255
6255
7255
8255
9255
10255
11255
12255
13255
diff --git a/test/014_math_sumzerotodigits/01_bad_b12.in b/test/014_math_sumzerotodigits/01_bad_b12.in
new file mode 100644
index 0000000..8a6d31c
--- /dev/null
+++ b/test/014_math_sumzerotodigits/01_bad_b12.in
@@ -0,0 +1,3 @@
112
23
32
diff --git a/test/014_math_sumzerotodigits/01_bad_b12.out b/test/014_math_sumzerotodigits/01_bad_b12.out
new file mode 100644
index 0000000..32b57e3
--- /dev/null
+++ b/test/014_math_sumzerotodigits/01_bad_b12.out
@@ -0,0 +1,12 @@
1255
2255
3255
4255
5255
6255
7255
8255
9255
10255
11255
12255
diff --git a/test/014_math_sumzerotodigits/02_bad_b8.in b/test/014_math_sumzerotodigits/02_bad_b8.in
new file mode 100644
index 0000000..b70bee9
--- /dev/null
+++ b/test/014_math_sumzerotodigits/02_bad_b8.in
@@ -0,0 +1,3 @@
18
27
30
diff --git a/test/014_math_sumzerotodigits/02_bad_b8.out b/test/014_math_sumzerotodigits/02_bad_b8.out
new file mode 100644
index 0000000..22d5f76
--- /dev/null
+++ b/test/014_math_sumzerotodigits/02_bad_b8.out
@@ -0,0 +1,8 @@
1255
2255
3255
4255
5255
6255
7255
8255
diff --git a/test/014_math_sumzerotodigits/03_zero12.in b/test/014_math_sumzerotodigits/03_zero12.in
new file mode 100644
index 0000000..e1d53ea
--- /dev/null
+++ b/test/014_math_sumzerotodigits/03_zero12.in
@@ -0,0 +1,3 @@
112
22
30
diff --git a/test/014_math_sumzerotodigits/03_zero12.out b/test/014_math_sumzerotodigits/03_zero12.out
new file mode 100644
index 0000000..66f4ca4
--- /dev/null
+++ b/test/014_math_sumzerotodigits/03_zero12.out
@@ -0,0 +1,12 @@
10
20
30
40
50
60
70
80
90
100
110
120
diff --git a/test/014_math_sumzerotodigits/04_zero8.in b/test/014_math_sumzerotodigits/04_zero8.in
new file mode 100644
index 0000000..db46735
--- /dev/null
+++ b/test/014_math_sumzerotodigits/04_zero8.in
@@ -0,0 +1,3 @@
18
23
30
diff --git a/test/014_math_sumzerotodigits/04_zero8.out b/test/014_math_sumzerotodigits/04_zero8.out
new file mode 100644
index 0000000..405d334
--- /dev/null
+++ b/test/014_math_sumzerotodigits/04_zero8.out
@@ -0,0 +1,8 @@
10
20
30
40
50
60
70
80
diff --git a/test/014_math_sumzerotodigits/05_max12.in b/test/014_math_sumzerotodigits/05_max12.in
new file mode 100644
index 0000000..c688648
--- /dev/null
+++ b/test/014_math_sumzerotodigits/05_max12.in
@@ -0,0 +1,3 @@
112
22
32047
diff --git a/test/014_math_sumzerotodigits/05_max12.out b/test/014_math_sumzerotodigits/05_max12.out
new file mode 100644
index 0000000..4dff9ef
--- /dev/null
+++ b/test/014_math_sumzerotodigits/05_max12.out
@@ -0,0 +1,12 @@
11
21
31
41
51
61
71
81
91
101
111
121
diff --git a/test/014_math_sumzerotodigits/06_max8.in b/test/014_math_sumzerotodigits/06_max8.in
new file mode 100644
index 0000000..4d1df80
--- /dev/null
+++ b/test/014_math_sumzerotodigits/06_max8.in
@@ -0,0 +1,3 @@
18
23
32186
diff --git a/test/014_math_sumzerotodigits/06_max8.out b/test/014_math_sumzerotodigits/06_max8.out
new file mode 100644
index 0000000..a3ad9a2
--- /dev/null
+++ b/test/014_math_sumzerotodigits/06_max8.out
@@ -0,0 +1,8 @@
11
22
32
42
52
62
72
82
diff --git a/test/014_math_sumzerotodigits/07_random12.in b/test/014_math_sumzerotodigits/07_random12.in
new file mode 100644
index 0000000..99af582
--- /dev/null
+++ b/test/014_math_sumzerotodigits/07_random12.in
@@ -0,0 +1,3 @@
112
22
3373
diff --git a/test/014_math_sumzerotodigits/07_random12.out b/test/014_math_sumzerotodigits/07_random12.out
new file mode 100644
index 0000000..2230573
--- /dev/null
+++ b/test/014_math_sumzerotodigits/07_random12.out
@@ -0,0 +1,12 @@
10
21
30
41
50
61
71
81
90
101
110
120
diff --git a/test/014_math_sumzerotodigits/08_random8.in b/test/014_math_sumzerotodigits/08_random8.in
new file mode 100644
index 0000000..b392c81
--- /dev/null
+++ b/test/014_math_sumzerotodigits/08_random8.in
@@ -0,0 +1,3 @@
18
23
31477
diff --git a/test/014_math_sumzerotodigits/08_random8.out b/test/014_math_sumzerotodigits/08_random8.out
new file mode 100644
index 0000000..cb68c56
--- /dev/null
+++ b/test/014_math_sumzerotodigits/08_random8.out
@@ -0,0 +1,8 @@
11
21
30
42
50
60
70
82
diff --git a/test/014_math_sumzerotodigits/sumzerotodigits.c b/test/014_math_sumzerotodigits/sumzerotodigits.c
new file mode 100644
index 0000000..5bea7ca
--- /dev/null
+++ b/test/014_math_sumzerotodigits/sumzerotodigits.c
@@ -0,0 +1,21 @@
1#include "../test.h"
2
3void sumzerotodigits(int64_t, uint8_t, uint8_t, uint8_t *);
4
5void run(void) {
6 char str[STRLENMAX];
7 uint8_t i, n, b, a[100];
8 int64_t d;
9
10 fgets(str, STRLENMAX, stdin);
11 n = atoi(str);
12 fgets(str, STRLENMAX, stdin);
13 b = atoi(str);
14 fgets(str, STRLENMAX, stdin);
15 d = atoll(str);
16
17 sumzerotodigits(d, n, b, a);
18
19 for (i = 0; i < n; i++)
20 printf("%" PRIu8 "\n", a[i]);
21}

Generated with cgit - Back to sebastiano.tronto.net