-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcuboid.c
2718 lines (2606 loc) · 133 KB
/
pcuboid.c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <inttypes.h>
#include <string.h>
#include <time.h>
#include <sys/timeb.h>
#ifdef BOINC
#include "boinc_api.h"
#endif
#ifdef __linux__
#include <sys/utsname.h>
#endif
#define VERSION "3.05"
#ifdef _WIN64
const char* OS = "Windows 64-bit";
#elif _WIN32
const char* OS = "Windows 32-bit";
#elif __APPLE__ || __MACH__
const char* OS = "Mac OS X";
#elif __FreeBSD__
const char* OS = "FreeBSD";
#else
const char* OS = "Other";
#endif
// almost - режим пошуку майже ідеальних кубоїдів
int almost = 0;
// progress - режим із відображенням прогресу
int progress = 0;
// quiet - подавити вивід на stdout
int quiet = 0;
// output - відправити stdout у файл out_%
int output = 0;
// report - створити файл із статистикою задачі rep_%
int report = 0;
// backward - пошук у зворотньому напрямку
int backward = 0;
// skip - вважати такими, що виконані, завдання, для яких є out і немає chk
int skip = 0;
// debug - режим із відображенням факторизації та декомпозицій
int debug = 0;
uint32_t debug_step = 1;
// verbose - режим із виводом результату в stderr
int verbose = 0;
uint32_t verbose_step = 1;
// check sum
uint64_t check_sum = 0;
//uint64_t loopCnt = 0;
#define max(a,b) ((a) > (b) ? a : b)
#define min(a,b) ((a) < (b) ? a : b)
#define sign(x) (x > 0 ? 1 : (x == 0 ? 0 : -1))
#ifndef HAVE_BZERO
#define bzero(ptr,n) \
memset(ptr, 0, n)
#endif //HAVE_BZERO
#define xchgu64(a,b) \
do {uint64_t c = *a; *a = *b; *b = c;} while (0)
#define xchgu128(a,b) \
do {__uint128_t c = *a; *a = *b; *b = c;} while (0)
// 6542 простих, менших за 2^16 = 65536
#define SMALL_PRIMES_CNT 6542
uint32_t SmallPrimes[SMALL_PRIMES_CNT];
uint32_t * Primes4k1 = NULL, * Primes4k3 = NULL;
uint32_t p4k1Size = 0, p4k3Size = 0;
long double sqrt2;
const int Step = 4;
const uint64_t MinG = 5;
const uint64_t MaxG = (int64_t)((INT64_MAX - 1) / 4 - 1) * 4 + 1;
struct timeb starttime, endtime;
FILE * fout, * frep, * fchk;
uint32_t block_size = 100000;
typedef struct {uint64_t Number; uint8_t Div1Cnt, Div3Cnt;} TBlock;
TBlock * Block = NULL;
uint32_t bSize = 0;
// Число 6437978455413290825^2 = (5^2 * 13^2 * 17 * 29 * 37 * 41 * 53 * 61 * 73 * 89 * 97)^2
// має найбільшу кількість розкладів на суму двох квадратів серед чисел виду 4k+1, менших за 2^63,
// що факторизуються лише простими виду 4k+1, а саме — 246038 розкладів.
//#define MAX_SQUARES_CNT 246038
// Також він має найбільшу кількість різних дільників виду 4k+1 — 11.
// Тому для зберігання факторизації будь-якого числа, що нас цікавлять, достатньо масиву з 11 елементів
#define MAX_FACTORES_CNT 11
// Число 614889782588491410 = 2*3*5*7*11*13*17*19*23*29*31*37*41*43*47 має найбільшу кількість різних дільників
// серед чисел менших за 2^63, тобто 15. Добуток двох чисел може містити максимум 25 дільників
// якщо друге число є 3749562977351496827 = 53*59*61*67*71*73*79*83*89*97, що додає ще 10 різних дільників
uint32_t max_squares_cnt = 0;
typedef struct { uint64_t Prime, A, B; uint8_t Power ;} TFactor;
TFactor * Factors[MAX_FACTORES_CNT];
//Декомпозиція діагоналі на суми квадратів. Q тримає результат; O та R - робочі
typedef struct { uint64_t A, B;} TSquare;
TSquare * O = NULL, * R = NULL;
typedef struct { uint64_t A, B; __uint128_t AA, BB;} TGirard;
TGirard * Q = NULL;
uint32_t oSize = 0, rSize = 0, qSize = 0;
uint32_t
pcCnt = 0 // кількість ідеальних кубоїдів
,ecCnt = 0 // кількість майже ідеальних кубоїдів з нецілим ребром
,fcCnt = 0 // кількість майже ідеальних кубоїдів з нецілою лицьовою діагоналлю
,ccCnt = 0 // кількість ідеальних кубоїдів з комплексним ребром
,icCnt = 0 // кількість уявних кубоїдів з комплексним ребром
,tcCnt = 0 // кількість уявних кубоїдів з комплексною лицьовою діагоналлю
,cnCnt = 0 // кількість кандидатів, що потрапили на перевірку
,toCnt = 0; // загальна кількість знайдених кубоїдів
uint64_t Ini, Fin, Cur, Task_Ini, Task_Fin;
char repfname[256] = "rep", outfname[256] = "out", chkfname[256] = "chk";
static __inline__ uint64_t string_to_u64(const char * s) {
uint64_t i;
char c ;
int scanned = sscanf(s, "%" SCNu64 "%c", &i, &c);
if (scanned == 1) return i;
if (scanned > 1) {
// TBD about extra data found
return i;
}
// TBD failed to scan;
return 0;
}
static __inline__ void mul_u64(uint64_t a, uint64_t b, uint64_t *h, uint64_t *l)
{
__uint128_t c = (__uint128_t)a * (__uint128_t)b;
*h = c >> 64;
*l = (uint64_t)c;
}
void u128_to_string(const __uint128_t n, char * s)
{
uint64_t d4, d3, d2, d1, d0, q;
const int size = 40; // floor(log10(2^128-1))
char u[40];
char * t = u;
// n = d3*2^96 + d2*2^64 + d1*2^32 + d0
// n = d3*79228162514264337593543950336 + d2*18446744073709551616 + d1*4294967296 + d0
// n = d3*79_228162514_264337593_543950336 + d2*18_446744073_709551616 + d1*4_294967296 + d0
// n = d3*79*10^27 + d3*228162514*10^18 + d3*264337593*10^9 + d3*543950336
// + d2* 18*10^18 + d2*446744073*10^9 + d2*709551616
// + d1* 4*10^9 + d1*294967296
// + d0*000000001
// define constants
const uint32_t k3 = 79;
const uint32_t k2 = 228162514;
const uint32_t k1 = 264337593;
const uint32_t k0 = 543950336;
const uint32_t l2 = 18;
const uint32_t l1 = 446744073;
const uint32_t l0 = 709551616;
const uint32_t m1 = 4;
const uint32_t m0 = 294967296;
const uint32_t dec_unit = 1000000000;
d0 = (uint32_t)n;
d1 = (uint32_t)(n >> 32);
d2 = (uint32_t)(n >> 64);
d3 = n >> 96;
d0 = (k0 * d3) + (l0 * d2) + (m0 * d1) + d0;
q = d0 / dec_unit;
d0 = d0 % dec_unit;
d1 = q + (k1 * d3) + (l1 * d2) + (m1 * d1);
q = d1 / dec_unit;
d1 = d1 % dec_unit;
d2 = q + (k2 * d3) + (l2 * d2);
q = d2 / dec_unit;
d2 = d2 % dec_unit;
d3 = q + (k3 * d3);
q = d3 / dec_unit;
d3 = d3 % dec_unit;
d4 = q;
bzero(t, size); // zero the buffer
sprintf(t,"%u%.9u%.9u%.9u%.9u",(uint32_t)d4,(uint32_t)d3,(uint32_t)d2,(uint32_t)d1,(uint32_t)d0);
// trim leading zeros
while (*t && *t == '0') t++;
if ( *t == 0x0 ) t--; // in case number = 0
strcpy(s, t);
}
// Евклідів алгоритм обчислення НСД (Найбільший спільний дільник)
uint64_t gcd(uint64_t a, uint64_t b)
{
if (!b) return a;
uint64_t c;
while (a)
{
c = a;
a = b%a;
b = c;
}
return b;
}
/* Функція обчислює (a*b) % m */
// Модуль m не має перевищувати 2^63
static __inline__ uint64_t mod_mul(uint64_t a, uint64_t b, const uint64_t m)
{
return (uint64_t)(((__uint128_t)a * b) % m);
}
/* Функція обчислює (a^b) % m */
static __inline__ uint64_t mod_pow(uint64_t a, uint64_t b, const uint64_t m)
{
uint64_t r = 1;
while (b > 0) {
if(b & 1)
r = mod_mul(r, a, m);
b = b >> 1;
a = mod_mul(a, a, m);
}
return r;
}
// Miller-Rabin primality test
// This function returns false if n is composite
//and returns false if n is probably prime.
int Miller_Rabin(const uint64_t a, uint64_t d, const uint64_t n)
{
// Corner cases make sure that n > 4
// Compute a^d % n
uint64_t x = mod_pow(a, d, n);
if (x == 1 || x == n-1) return 1;
// Keep squaring x while one of the following doesn't happen
// (i) d does not reach n-1
// (ii) (x^2) % n is not 1
// (iii) (x^2) % n is not n-1
while (d != n-1)
{
x = mod_mul(x, x, n);
d *= 2;
if (x == 1) return 0;
if (x == n-1) return 1;
}
// Return composite
return 0;
}
// It returns false if n is composite and returns true if n
// is probably prime. k is an input parameter that determines
// accuracy level. Higher value of k indicates more accuracy.
int is_prime(const uint64_t n)
{
// Corner cases
//if (n <= 1 || n == 4) return 0;
// Trial division by first small primes <= 37
// All other cases will be tested by Miller-Rabin primality test
for (int i=0; i<12; i++)
if (n == SmallPrimes[i]) return 1;
// Find r such that n = 2^d * r + 1 for some r >= 1
uint64_t d = n - 1;
while (!(d % 2)) d /= 2;
// Iterate given number of 'k' times
// it is enough to test a = 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, and 37
// for n < 2^64
if (!Miller_Rabin(2 , d, n)) return 0;
if (!Miller_Rabin(3 , d, n)) return 0;
if (!Miller_Rabin(5 , d, n)) return 0;
if (!Miller_Rabin(7 , d, n)) return 0;
if (!Miller_Rabin(11, d, n)) return 0;
if (!Miller_Rabin(13, d, n)) return 0;
if (!Miller_Rabin(17, d, n)) return 0;
if (!Miller_Rabin(19, d, n)) return 0;
if (!Miller_Rabin(23, d, n)) return 0;
if (!Miller_Rabin(29, d, n)) return 0;
if (!Miller_Rabin(31, d, n)) return 0;
if (!Miller_Rabin(37, d, n)) return 0;
return 1;
}
static __inline__ uint64_t root4(const uint64_t n)
{
uint64_t a, b, k = n/4;
for (uint16_t i=0; ; i++) {
uint16_t j = SmallPrimes[i];
a = mod_pow(j, k, n);
b = mod_mul(a, a, n);
if (b == n-1)
return a;
}
}
static __inline__ void decompose_4kp1(uint64_t n, uint64_t *x, uint64_t *y)
{
int64_t c, r, s;
s = rintl(sqrtl(n));
c = root4(n);
r = n % c;
while (c > s) {
n = c;
c = r;
r = n % c;
};
(*x) = r;
(*y) = c;
if (*x > *y)
xchgu64(x, y);
}
void factorize_range(void)
{
uint32_t j, i, k, MaxFactor = rintl(sqrtl(Block[(bSize)-1].Number));
uint64_t d;
for (j = 0; j < p4k3Size && Primes4k3[j] <= MaxFactor; j++) {
d = Primes4k3[j];
k = Block[0].Number % d;
if (k) k = d - ((Block[0].Number + d)/4) % d;
for (i = k; i < bSize; i += d)
Block[i].Div3Cnt++;
}
for (j = 0; j < p4k1Size && Primes4k1[j] <= MaxFactor; j++) {
d = Primes4k1[j];
k = Block[0].Number % d;
if (k) {
if (Block[0].Number > d)
k = d - ((Block[0].Number - d)/4) % d;
else
k = d + ((d - Block[0].Number)/4) % d;
}
for (i = k; i < bSize; i += d)
Factors[Block[i].Div1Cnt++][i].Prime = d;
}
for (i = 0; i < bSize; i++) {
if (Block[i].Div1Cnt && (Block[i].Number == Factors[0][i].Prime))
Block[i].Div1Cnt = 0;
if (Block[i].Div1Cnt && !(Block[i].Div3Cnt)) {
d = Block[i].Number;
for (k = 0; k < Block[i].Div1Cnt; k++) {
while (!(d % Factors[k][i].Prime)) {
d /= Factors[k][i].Prime;
Factors[k][i].Power++;
}
}
if (d != 1) {
Factors[Block[i].Div1Cnt++][i].Prime = d;
Factors[k][i].Power = 1;
}
}
}
}
#define MOD48MASK ((1ULL << 48) - 1)
#define MOD56MASK ((1ULL << 56) - 1)
static __inline__ uint64_t is_square(__uint128_t p)
{
if ((int64_t)(0xC840C04048404040ULL << (p & 63))>=0) return 0;
uint64_t m48 = (uint64_t)(p >> 96) + ((uint64_t)(p >> 48) & MOD48MASK) + ((uint64_t)p & MOD48MASK);
m48 = (m48 & MOD48MASK) + (m48 >> 48);
m48 = (m48 & MOD48MASK) + (m48 >> 48); //important repetition
uint64_t res, res1;
// mod 63 & 65, try to cue the compiler to get out-of-order instructions to use two ALUs
res = (m48 * 0x4104104104105ULL) & MOD56MASK;
res1 = (m48 * 0x3F03F03F03F04ULL) & MOD56MASK;
res = (res << 6) - res;
res1 += (res1 << 6);
res >>= 56;
res1 >>= 56;
if ((int64_t)(0xC940A2480C124020ULL << res) >= 0) return 0;
if ((int64_t)(0xC862806619805184ULL << res1) > 0) return 0;
// mod 17
res = (m48 * 0xF0F0F0F0F0F10ULL) & MOD56MASK;
res += (res << 4);
res >>= 56;
if ((int64_t)(0xE8C5800000000000ULL << res) >= 0) return 0;
check_sum++;
uint64_t c = rintl(sqrtl(p));
return (p == (__uint128_t)c*(__uint128_t)c) ? c : 0;
}
static __inline__ uint64_t is_sub_of_squares_square_too(__uint128_t a, __uint128_t b)
{
if (a == b) return 0;
if (a < b) xchgu128(&a, &b);
if ((a + 1) & b & 1) return 0;
return is_square(a - b);
}
static __inline__ uint64_t is_sum_of_squares_square_too(__uint128_t a, __uint128_t b)
{
if (a & b & 1) return 0;
return is_square(a + b);
}
static __inline__ void decompose_factors(void)
{
for (uint32_t i = 0; i < bSize; i++)
if (!Block[i].Div3Cnt && Block[i].Div1Cnt)
for (uint8_t j = 0; j < Block[i].Div1Cnt; j++)
decompose_4kp1(Factors[j][i].Prime, &Factors[j][i].A, &Factors[j][i].B);
}
static __inline__ uint32_t double_powers(void)
{
uint32_t mi, m = 1;
for (uint32_t i = 0; i < bSize; i++) {
if (!Block[i].Div3Cnt && Block[i].Div1Cnt) {
mi = 1;
for (int j = 0; j < Block[i].Div1Cnt; j++) {
Factors[j][i].Power *= 2;
mi *= Factors[j][i].Power+1;
}
mi = (mi+1) / 2;
if (mi > m) m = mi;
}
}
return m;
}
static __inline__ void produce_R_from_O(TFactor f)
{
uint64_t x, y;
for (uint32_t i = 0; i < oSize; i++) {
x = llabs(O[i].A*f.B - O[i].B*f.A);
y = O[i].A*f.A + O[i].B*f.B;
if (x > y)
xchgu64(&x, &y);
R[rSize].A = x;
R[rSize].B = y;
rSize++;
x = llabs(O[i].A*f.A - O[i].B*f.B);
y = O[i].A*f.B + O[i].B*f.A;
if (x > y)
xchgu64(&x, &y);
R[rSize].A = x;
R[rSize].B = y;
rSize++;
}
}
static __inline__ void produce_R_from_Q(TFactor f)
{
uint64_t x, y;
for (uint32_t i = 0; i < qSize; i++) {
x = llabs(Q[i].A*f.B - Q[i].B*f.A);
y = Q[i].A*f.A + Q[i].B*f.B;
if (x > y)
xchgu64(&x, &y);
R[rSize].A = x;
R[rSize].B = y;
rSize++;
x = llabs(Q[i].A*f.A - Q[i].B*f.B);
y = Q[i].A*f.B + Q[i].B*f.A;
if (x > y)
xchgu64(&x, &y);
R[rSize].A = x;
R[rSize].B = y;
rSize++;
}
}
static __inline__ void enrich_Q_by_R(void)
{
int found, addnew;
for (uint32_t i = 0, j = 0; i < rSize; i++) {
if (!(i > 0 && R[i].A == R[i-1].A)) {
found = addnew = 0;
while (!found && !addnew && j < qSize) {
found = (Q[j].A == R[i].A && Q[j].B == R[i].B);
if (!found) {
if (Q[j].A < R[i].A) j++;
else addnew = 1;
}
}
if (!found) {
Q[qSize].A = R[i].A;
Q[qSize].B = R[i].B;
qSize++;
}
}
}
}
static __inline__ void enrich_O_by_R(void)
{
int found, addnew;
for (uint32_t i = 0, j = 0; i < rSize; i++) {
if (!(i > 0 && R[i].A == R[i-1].A)) {
found = addnew = 0;
while (!found && !addnew && j < oSize) {
found = (O[j].A == R[i].A && O[j].B == R[i].B);
if (!found) {
if (O[j].A < R[i].A) j++;
else addnew = 1;
}
}
if (!found) {
O[oSize].A = R[i].A;
O[oSize].B = R[i].B;
oSize++;
}
}
}
}
void sort_squares_by_A(TSquare * s, int32_t l, int32_t h)
{
int32_t i, j, k;
TSquare t;
do {
i = l;
j = h;
k = (l+h) >> 1;
do {
while (s[i].A < s[k].A) i++;
while (s[j].A > s[k].A) j--;
if (i <= j) {
t = s[i];
s[i] = s[j];
s[j] = t;
if (k == i) k = j;
else if (k == j) k = i;
i++;
j--;
}
} while (i <= j);
if (l < j) sort_squares_by_A(s, l, j);
l = i;
} while (i < h);
}
static __inline__ void copy_squares(void)
{
for (uint32_t i=0; i < oSize; i++) {
Q[i].A = O[i].A;
Q[i].B = O[i].B;
Q[i].AA = (__uint128_t)Q[i].A * (__uint128_t)Q[i].A;
Q[i].BB = (__uint128_t)Q[i].B * (__uint128_t)Q[i].B;
}
qSize = oSize;
}
void find_all_squares(uint32_t i)
{
qSize = 0;
O[0].A = 0;
O[0].B = 1;
oSize = 1;
for (int j = 0; j < Block[i].Div1Cnt ; j++) {
for (int k = 1; k <= Factors[j][i].Power ; k++) {
if (k & 1) {
qSize = 0;
rSize = 0;
produce_R_from_O(Factors[j][i]);
sort_squares_by_A(R, 0, rSize-1);
enrich_Q_by_R();
}
else {
for (uint32_t l=0; l < oSize; l++) {
O[l].A *= Factors[j][i].Prime;
O[l].B *= Factors[j][i].Prime;
}
rSize = 0;
produce_R_from_Q(Factors[j][i]);
sort_squares_by_A(R, 0, rSize-1);
enrich_O_by_R();
sort_squares_by_A(O, 0, oSize-1);
}
}
}
copy_squares();
}
// Сортуємо ребра A, B, C за зростанням
void sort_by_ABC(int64_t A, int64_t B, int64_t C, char * sA, char * sB, char * sC, char * sD, char * sE, char * sF)
{
char T[36] = "";
char * sT = T;
int64_t I;
if (A > B) {
strcpy(sT, sA);
strcpy(sA, sB);
strcpy(sB, sT);
strcpy(sT, sF);
strcpy(sF, sE);
strcpy(sE, sT);
I = A;
A = B;
B = I;
}
if (A > C) {
strcpy(sT, sA);
strcpy(sA, sC);
strcpy(sC, sT);
strcpy(sT, sF);
strcpy(sF, sD);
strcpy(sD, sT);
I = A;
A = C;
C = I;
}
if (B > C) {
strcpy(sT, sB);
strcpy(sB, sC);
strcpy(sC, sT);
strcpy(sT, sE);
strcpy(sE, sD);
strcpy(sD, sT);
}
}
// Сортуємо лицьові діагоналі D, E, F за зростанням
void sort_by_DEF(int64_t D, int64_t E, int64_t F, char * sA, char * sB, char * sC, char * sD, char * sE, char * sF)
{
char T[36] = "";
char * sT = T;
int64_t I;
if (E > F) {
strcpy(sT, sA);
strcpy(sA, sB);
strcpy(sB, sT);
strcpy(sT, sF);
strcpy(sF, sE);
strcpy(sE, sT);
I = E;
E = F;
F = I;
}
if (D > F) {
strcpy(sT, sA);
strcpy(sA, sC);
strcpy(sC, sT);
strcpy(sT, sF);
strcpy(sF, sD);
strcpy(sD, sT);
I = D;
D = F;
F = I;
}
if (D > E) {
strcpy(sT, sB);
strcpy(sB, sC);
strcpy(sC, sT);
strcpy(sT, sE);
strcpy(sE, sD);
strcpy(sD, sT);
}
}
void save_checkpoint(uint64_t pos)
{
fchk = fopen(chkfname, "w");
if(fchk == NULL) {
#ifdef BOINC
boinc_finish(EXIT_FAILURE);
#endif
exit(EXIT_FAILURE);
}
struct timeb curtime;
ftime(&curtime);
uint64_t dif = (curtime.time - starttime.time) * 1000 + (curtime.millitm - starttime.millitm);
fprintf(fchk, "%" PRIu64
",%" PRIu64
",%" PRIu64
",%" PRIu64
",%d,%" PRIu64
",%" PRIu32
",%" PRIu32
",%" PRIu32
",%" PRIu32
",%" PRIu32
",%" PRIu32
",%" PRIu32
,Ini
,Fin
,pos
,check_sum
,backward
,dif
,cnCnt
,pcCnt
,ecCnt
,fcCnt
,ccCnt
,icCnt
,tcCnt
);
fflush(fchk);
fclose(fchk);
#if defined BOINC
boinc_checkpoint_completed();
#endif
}
int read_checkpoint(void)
{
fchk = fopen(chkfname, "r");
if(fchk == NULL)
return (EXIT_FAILURE);
char c;
uint64_t dif;
int scanned = fscanf(fchk, "%" SCNu64
",%" SCNu64
",%" SCNu64
",%" SCNu64
",%d,%" SCNu64
",%" PRIu32
",%" PRIu32
",%" PRIu32
",%" PRIu32
",%" PRIu32
",%" PRIu32
",%" PRIu32
",%c"
, &Ini
, &Fin
, &Cur
, &check_sum
, &backward
, &dif
, &cnCnt
, &pcCnt
, &ecCnt
, &fcCnt
, &ccCnt
, &icCnt
, &tcCnt
, &c);
fclose(fchk);
if (scanned != 13) {
#ifdef BOINC
boinc_finish(EXIT_FAILURE);
#endif
exit(EXIT_FAILURE);
}
if (!Cur) return 1;
else Cur = ((Cur / Step) + 1) * Step + 1;
starttime.time -= dif / 1000;
long int millisec = (dif % 1000);
if (starttime.millitm < millisec) {
starttime.millitm += 1000 - millisec;
starttime.time--;
} else starttime.millitm -= millisec;
toCnt = pcCnt + ecCnt + fcCnt + ccCnt + icCnt + tcCnt/* + mcCnt*/;
return 0;
}
void print_extend_out(char * msg, char * A, char * B, char * C, char * D, char * E, char * F, char * G)
{
fprintf(stderr, "%s\n", msg);
fprintf(stderr, "A = %s\n", A);
fprintf(stderr, "B = %s\n", B);
fprintf(stderr, "C = %s\n", C);
fprintf(stderr, "D = %s\n", D);
fprintf(stderr, "E = %s\n", E);
fprintf(stderr, "F = %s\n", F);
fprintf(stderr, "G = %s\n", G);
fprintf(stderr, "-------------------------------------------------\n");
}
void search_almost(uint64_t G)
{
//Q має бути впорядкованим за A
uint32_t i, j;
uint64_t A, B, E, F, K, L, M, N, P, R;
__uint128_t AA, BB, EE, FF;
char sA[40], sB[40], sC[40], sD[40], sE[40], sF[40], sG[40];
char rtsn[] = "√";
char s128[40];
for (i = 1; i < qSize-1; i++) {
A = Q[i].A;
F = Q[i].B;
AA = Q[i].AA;
FF = Q[i].BB;
for (j = i + 1; j < qSize; j++) {
B = Q[j].A;
E = Q[j].B;
BB = Q[j].AA;
EE = Q[j].BB;
K = is_sum_of_squares_square_too(AA, BB);
L = is_sum_of_squares_square_too(AA, EE);
M = is_sum_of_squares_square_too(BB, FF);
N = is_sum_of_squares_square_too(EE, FF);
P = is_sub_of_squares_square_too(BB, AA);
R = is_sub_of_squares_square_too(EE, AA);
if (P) {
if (gcd(gcd(gcd(A, E), G), P)==1) {
if (L) {
pcCnt++;
toCnt++;
sprintf(sG, "%" PRIu64, G);
sprintf(sA, "%" PRIu64, A);
sprintf(sB, "%" PRIu64, E);
sprintf(sC, "%" PRIu64, P);
sprintf(sD, "%" PRIu64, L);
sprintf(sE, "%" PRIu64, B);
sprintf(sF, "%" PRIu64, F);
sort_by_ABC(A, E, P, sA, sB, sC, sD, sE, sF);
if (!quiet) fprintf(stdout, "P,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (output) fprintf(fout, "P,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (verbose && !progress && !(cnCnt % verbose_step))
print_extend_out("Perfect Cuboid", sA, sB, sC, sD, sE, sF, sG);
ccCnt++;
toCnt++;
sprintf(sG, "%" PRIu64, E);
sprintf(sA, "%" PRIu64 "i", A);
sprintf(sB, "%" PRIu64 "i", P);
sprintf(sC, "%" PRIu64, G);
sprintf(sD, "%" PRIu64 "i", B);
sprintf(sE, "%" PRIu64, F);
sprintf(sF, "%" PRIu64, L);
sort_by_ABC(-A, -P, G, sA, sB, sC, sD, sE, sF);
if (!quiet) fprintf(stdout, "C,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (output) fprintf(fout, "C,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (verbose && !progress && !(cnCnt % verbose_step))
print_extend_out("Complex Cuboid", sA, sB, sC, sD, sE, sF, sG);
ccCnt++;
toCnt++;
sprintf(sG, "%" PRIu64, A);
sprintf(sA, "%" PRIu64 "i", E);
sprintf(sB, "%" PRIu64 "i", P);
sprintf(sC, "%" PRIu64, G);
sprintf(sD, "%" PRIu64 "i", F);
sprintf(sE, "%" PRIu64, B);
sprintf(sF, "%" PRIu64, L);
sort_by_ABC(-E, -P, G, sA, sB, sC, sD, sE, sF);
if (!quiet) fprintf(stdout, "C,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (output) fprintf(fout, "C,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (verbose && !progress && !(cnCnt % verbose_step))
print_extend_out("Complex Cuboid", sA, sB, sC, sD, sE, sF, sG);
ccCnt++;
toCnt++;
sprintf(sG, "%" PRIu64, P);
sprintf(sA, "%" PRIu64 "i", E);
sprintf(sB, "%" PRIu64 "i", A);
sprintf(sC, "%" PRIu64, G);
sprintf(sD, "%" PRIu64 "i", L);
sprintf(sE, "%" PRIu64, B);
sprintf(sF, "%" PRIu64, F);
sort_by_ABC(-E, -A, G, sA, sB, sC, sD, sE, sF);
if (!quiet) fprintf(stdout, "C,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (output) fprintf(fout, "C,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (verbose && !progress && !(cnCnt % verbose_step))
print_extend_out("Complex Cuboid", sA, sB, sC, sD, sE, sF, sG);
} else {
u128_to_string((__uint128_t)A*(__uint128_t)A + (__uint128_t)E*(__uint128_t)E, s128);
fcCnt++;
toCnt++;
sprintf(sG, "%" PRIu64, G);
sprintf(sA, "%" PRIu64, A);
sprintf(sB, "%" PRIu64, E);
sprintf(sC, "%" PRIu64, P);
sprintf(sD, "%s%s", rtsn, s128);
sprintf(sE, "%" PRIu64, B);
sprintf(sF, "%" PRIu64, F);
sort_by_ABC(A, E, P, sA, sB, sC, sD, sE, sF);
if (!quiet) fprintf(stdout, "F,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (output) fprintf(fout, "F,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (verbose && !progress && !(cnCnt % verbose_step))
print_extend_out("Face Cuboid", sA, sB, sC, sD, sE, sF, sG);
tcCnt++;
toCnt++;
sprintf(sG, "%" PRIu64, E);
sprintf(sA, "%" PRIu64 "i", A);
sprintf(sB, "%" PRIu64 "i", P);
sprintf(sC, "%" PRIu64, G);
sprintf(sD, "%" PRIu64 "i", B);
sprintf(sE, "%" PRIu64, F);
sprintf(sF, "%s%s", rtsn, s128);
sort_by_ABC(-A, -P, G, sA, sB, sC, sD, sE, sF);
if (!quiet) fprintf(stdout, "T,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (output) fprintf(fout, "T,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (verbose && !progress && !(cnCnt % verbose_step))
print_extend_out("Twilight Cuboid", sA, sB, sC, sD, sE, sF, sG);
tcCnt++;
toCnt++;
sprintf(sG, "%" PRIu64, A);
sprintf(sA, "%" PRIu64 "i", E);
sprintf(sB, "%" PRIu64 "i", P);
sprintf(sC, "%" PRIu64, G);
sprintf(sD, "%" PRIu64 "i", F);
sprintf(sE, "%" PRIu64, B);
sprintf(sF, "%s%s", rtsn, s128);
sort_by_ABC(-E, -P, G, sA, sB, sC, sD, sE, sF);
if (!quiet) fprintf(stdout, "T,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (output) fprintf(fout, "T,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (verbose && !progress && !(cnCnt % verbose_step))
print_extend_out("Twilight Cuboid", sA, sB, sC, sD, sE, sF, sG);
tcCnt++;
toCnt++;
sprintf(sG, "%" PRIu64, P);
sprintf(sA, "%" PRIu64 "i", E);
sprintf(sB, "%" PRIu64 "i", A);
sprintf(sC, "%" PRIu64, G);
sprintf(sD, "%s-%s", rtsn, s128);
sprintf(sE, "%" PRIu64, B);
sprintf(sF, "%" PRIu64, F);
sort_by_ABC(-E, -A, G, sA, sB, sC, sD, sE, sF);
if (!quiet) fprintf(stdout, "T,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (output) fprintf(fout, "T,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (verbose && !progress && !(cnCnt % verbose_step))
print_extend_out("Twilight Cuboid", sA, sB, sC, sD, sE, sF, sG);
}
}
if (gcd(gcd(gcd(B, F), G), P)==1) {
if (M) {
ccCnt++;
toCnt++;
sprintf(sG, "%" PRIu64, G);
sprintf(sA, "%" PRIu64 "i", P);
sprintf(sB, "%" PRIu64, B);
sprintf(sC, "%" PRIu64, F);
sprintf(sD, "%" PRIu64, A);
sprintf(sE, "%" PRIu64, E);
sprintf(sF, "%" PRIu64, M);
sort_by_ABC(-P, B, F, sA, sB, sC, sD, sE, sF);
if (!quiet) fprintf(stdout, "C,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (output) fprintf(fout, "C,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (verbose && !progress && !(cnCnt % verbose_step))
print_extend_out("Complex Cuboid", sA, sB, sC, sD, sE, sF, sG);
ccCnt++;
toCnt++;
sprintf(sG, "%" PRIu64, F);
sprintf(sA, "%" PRIu64 "i", B);
sprintf(sB, "%" PRIu64, P);
sprintf(sC, "%" PRIu64, G);
sprintf(sD, "%" PRIu64 "i", A);
sprintf(sE, "%" PRIu64, E);
sprintf(sF, "%" PRIu64, M);
sort_by_ABC(-B, P, G, sA, sB, sC, sD, sE, sF);
if (!quiet) fprintf(stdout, "C,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (output) fprintf(fout, "C,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (verbose && !progress && !(cnCnt % verbose_step))
print_extend_out("Complex Cuboid", sA, sB, sC, sD, sE, sF, sG);
ccCnt++;
toCnt++;
sprintf(sG, "%" PRIu64, B);
sprintf(sA, "%" PRIu64 "i", F);
sprintf(sB, "%" PRIu64, P);
sprintf(sC, "%" PRIu64, G);
sprintf(sD, "%" PRIu64 "i", E);
sprintf(sE, "%" PRIu64, A);
sprintf(sF, "%" PRIu64, M);
sort_by_ABC(-F, P, G, sA, sB, sC, sD, sE, sF);
if (!quiet) fprintf(stdout, "C,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (output) fprintf(fout, "C,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (verbose && !progress && !(cnCnt % verbose_step))
print_extend_out("Complex Cuboid", sA, sB, sC, sD, sE, sF, sG);
ccCnt++;
toCnt++;
sprintf(sG, "%" PRIu64, P);
sprintf(sA, "%" PRIu64 "i", G);
sprintf(sB, "%" PRIu64, B);
sprintf(sC, "%" PRIu64, F);
sprintf(sD, "%" PRIu64 "i", E);
sprintf(sE, "%" PRIu64 "i", A);
sprintf(sF, "%" PRIu64, M);
sort_by_ABC(-G, B, F, sA, sB, sC, sD, sE, sF);
if (!quiet) fprintf(stdout, "C,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (output) fprintf(fout, "C,%s,%s,%s,%s,%s,%s,%s\n", sG, sA, sB, sC, sD, sE, sF);
if (verbose && !progress && !(cnCnt % verbose_step))
print_extend_out("Complex Cuboid", sA, sB, sC, sD, sE, sF, sG);
} else {
u128_to_string((__uint128_t)B*(__uint128_t)B + (__uint128_t)F*(__uint128_t)F, s128);
icCnt++;
toCnt++;
sprintf(sG, "%" PRIu64, G);
sprintf(sA, "%" PRIu64 "i", P);
sprintf(sB, "%" PRIu64, B);
sprintf(sC, "%" PRIu64, F);
sprintf(sD, "%" PRIu64, A);