-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinputnum.cpp
More file actions
1942 lines (1845 loc) · 63.8 KB
/
inputnum.cpp
File metadata and controls
1942 lines (1845 loc) · 63.8 KB
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 <climits>
#include <cctype>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <stdlib.h>
#include <map>
#include "gwnum.h"
#include "cpuid.h"
#include "inputnum.h"
#include "file.h"
#include "edwards.h"
#include "integer.h"
#include "exception.h"
using namespace arithmetic;
bool InputNum::read(File& file)
{
std::unique_ptr<Reader> reader(file.get_reader());
if (!reader)
return false;
if (reader->type() != 0)
return false;
if (!reader->read(_gb))
return false;
_type = GENERIC;
_gk = 1;
_n = 0;
_c = 0;
_input_text = file.filename();
_display_text = file.filename();
return true;
}
void InputNum::write(File& file)
{
std::unique_ptr<Writer> writer(file.get_writer(0, 0));
if (_type != KBNC)
writer->write(_gk*_gb + _c);
else
writer->write(_gk*power(_gb, _n) + _c);
file.commit_writer(*writer);
}
void add_factor(std::vector<std::pair<arithmetic::Giant, int>>& factors, const Giant& factor, int power)
{
auto it = factors.begin();
for (; it != factors.end() && it->first != factor; it++);
if (it != factors.end())
{
it->second += power;
if (it->second == 0)
factors.erase(it);
}
else
factors.emplace_back(factor, power);
}
void add_factor(std::vector<std::pair<arithmetic::Giant, int>>& factors, uint32_t factor, int power)
{
Giant tmp;
tmp = factor;
add_factor(factors, tmp, power);
}
void InputNum::add_factor(Giant& factor)
{
if (!_b_cofactor.empty() && _b_cofactor%factor == 0)
{
::add_factor(_b_factors, factor, 1);
if (std::abs(_c) == 1)
::add_factor(_factors, factor, _n);
_b_cofactor /= factor;
if (_b_cofactor == 1)
_b_cofactor.arithmetic().free(_b_cofactor);
if (!_cofactor.empty())
{
_cofactor /= power(factor, _n);
if (_cofactor == 1)
_cofactor.arithmetic().free(_cofactor);
}
}
else if (!_cofactor.empty() && _cofactor%factor == 0)
{
::add_factor(_factors, factor, 1);
_cofactor /= factor;
if (_cofactor == 1)
_cofactor.arithmetic().free(_cofactor);
}
}
void factorize(Giant& N, std::vector<std::pair<arithmetic::Giant, int>>& factors, Giant& cofactor, std::function<bool(Giant&, uint32_t)> is_factor = nullptr, uint32_t s = 10)
{
uint32_t i, j;
int power;
Giant tmp = N;
for (i = 0; !tmp.bit(i); i++);
if (i > 0)
{
add_factor(factors, 2, i);
tmp >>= i;
}
std::vector<bool> bitmap;
if (tmp > 1)
{
bitmap.resize((size_t)1 << (s - 1), false);
std::vector<std::pair<int, int>> smallprimes;
for (i = 1; i < bitmap.size(); i++)
if (!bitmap[i])
{
smallprimes.emplace_back(i*2 + 1, (i*2 + 1)*(i*2 + 1)/2);
if (i < ((size_t)1 << (s/2 - 1)))
for (; smallprimes.back().second < bitmap.size(); smallprimes.back().second += smallprimes.back().first)
bitmap[smallprimes.back().second] = true;
if ((is_factor != nullptr && is_factor(tmp, i*2 + 1)) || (is_factor == nullptr && tmp%(i*2 + 1) == 0))
{
for (power = 1, tmp /= i*2 + 1; tmp%(i*2 + 1) == 0; power++, tmp /= i*2 + 1);
add_factor(factors, i*2 + 1, power);
}
}
if (tmp > 1 && tmp < (1 << (2*s)))
{
add_factor(factors, tmp, 1);
tmp = 1;
}
for (j = 0; j < s && tmp > 1; j += 5)
{
bitmap.resize((size_t)1 << (s - 1 + (j + 5 < s ? j + 5 : s)), false);
for (auto it = smallprimes.begin(); it != smallprimes.end(); it++)
for (; it->second < bitmap.size(); it->second += it->first)
bitmap[it->second] = true;
for (i = 1 << (s - 1 + j); i < bitmap.size(); i++)
if (!bitmap[i] && ((is_factor != nullptr && is_factor(tmp, i*2 + 1)) || (is_factor == nullptr && tmp%(i*2 + 1) == 0)))
{
for (power = 1, tmp /= i*2 + 1; tmp%(i*2 + 1) == 0; power++, tmp /= i*2 + 1);
add_factor(factors, i*2 + 1, power);
}
if (tmp > 1 && (uint32_t)tmp.bitlen() <= 2*(s + (j + 5 < s ? j + 5 : s)))
{
add_factor(factors, tmp, 1);
tmp = 1;
}
}
}
if (tmp > 1)
{
if (cofactor.empty())
cofactor = std::move(tmp);
else
cofactor *= tmp;
}
/* if (_b_cofactor)
{
int len;
GWState gwstate;
gwstate.setup(*_b_cofactor);
GWArithmetic gw(gwstate);
{
GWNum y(gw);
y = 3;
gw.setmulbyconst(3);
len = _b_cofactor->bitlen() - 1;
for (i = 1; i <= len; i++)
gw.mul(y, y, y, _b_cofactor->bit(len - i) ? GWMUL_MULBYCONST : 0);
if (y == 3)
{
add_factor(*_b_cofactor, 1);
_b_cofactor.reset();
}
}
}
if (_b_cofactor)
{
int len = _b_cofactor->bitlen()/10;
if (len > 2*s)
len = 2*s;
tmp.arithmetic().alloc(tmp, (1 << (len - 5))/0.69);
Giant tmp2(tmp.arithmetic(), tmp.capacity() < 8192 ? tmp.capacity() : 8192);
tmp = 0;
tmp2 = 1 << len;
for (i = 1; i < bitmap.size() && i < (1 << len); i++)
if (!bitmap[i])
{
uint32_t p = i*2 + 1;
j = p;
if (p <= (1 << (len/2 + 1)))
{
uint32_t pp = (1 << len)/p;
while (j <= pp)
j *= p;
}
tmp2 *= j;
if (tmp2.size() > 8190)
{
if (tmp == 0)
tmp = tmp2;
else
tmp *= tmp2;
tmp2 = 1;
}
}
if (tmp == 0)
tmp = std::move(tmp2);
else
tmp *= tmp2;
len = tmp.bitlen();
int W;
for (W = 2; W < 16 && (14 << (W - 2)) + len/0.69*(7 + 7/(W + 1.0)) >(14 << (W - 1)) + len/0.69*(7 + 7/(W + 2.0)); W++);
std::vector<int16_t> naf_w;
get_NAF_W(W, tmp, naf_w);
GWState gwstate;
gwstate.setup(*_b_cofactor);
GWArithmetic gw(gwstate);
for (j = 0; true; j++)
{
double timer = getHighResTimer();
{
EdwardsArithmetic ed(gw);
GWNum ed_d(gw);
EdPoint P = ed.gen_curve(*(int *)&timer, &ed_d);
ed.mul(P, W, naf_w, P);
if (!ed.on_curve(P, ed_d))
{
gwstate.done();
gwstate.next_fft_count++;
gwstate.setup(*_b_cofactor);
}
tmp = gcd(*P.X, *_b_cofactor);
}
if (tmp != 1)
{
GWState rgwstate;
rgwstate.setup(tmp);
GWArithmetic rgw(rgwstate);
GWNum x(rgw);
x = 3;
rgw.setmulbyconst(3);
len = tmp.bitlen() - 1;
for (i = 1; i <= len; i++)
rgw.mul(x, x, x, tmp.bit(len - i) ? GWMUL_MULBYCONST : 0);
if (x == 3)
{
add_factor(tmp, 1);
*_b_cofactor /= tmp;
gwstate.done();
gwstate.setup(*_b_cofactor);
GWNum y(gw);
y = 3;
gw.setmulbyconst(3);
len = _b_cofactor->bitlen() - 1;
for (i = 1; i <= len; i++)
gw.mul(y, y, y, _b_cofactor->bit(len - i) ? GWMUL_MULBYCONST : 0);
if (y == 3)
{
add_factor(*_b_cofactor, 1);
_b_cofactor.reset();
break;
}
}
}
}
}*/
}
class Expr
{
public:
Expr()
{
value = 0;
}
bool evaluate(bool factorize = true)
{
if (str_func.empty())
{
if (args.size() != 1 || str_args(0).empty())
return error(0, "value expected");
value = str_args(0);
if (factorize)
::factorize(value, factors, cofactor);
}
else if (str_func == "p")
{
if (args.size() != 1 || str_args(0).empty())
return error(0, "prime index expected");
std::string::const_iterator it;
for (it = str_args(0).begin(); it != str_args(0).end() && std::isdigit(*it); it++);
if (it != str_args(0).end())
{
InputNum recursive;
InputNum::ParseResult res = recursive.parse(str_args(0), false);
if (!res)
return error(args[0].first + res.pos, res.message);
value = recursive.value();
}
else
value = str_args(0);
if (value > 105097565)
return error(args[0].first, "prime index too big");
if (value == 0)
value = 1;
else
{
value = *PrimeIterator::get(value.data()[0] - 1);
if (factorize)
::add_factor(factors, value, 1);
}
}
else if (str_func == "()")
{
if (args.size() != 1 || str_args(0).empty())
return error(0, "expression expected");
InputNum recursive;
InputNum::ParseResult res = recursive.parse(str_args(0), false);
if (!res)
return error(args[0].first + res.pos, res.message);
value = recursive.value();
str_value = "(" + recursive.input_text() + ")";
if (factorize)
{
if (recursive.c() == 0)
{
factors = std::move(recursive.factors());
cofactor = std::move(recursive.cofactor());
}
else
::factorize(value, factors, cofactor);
}
}
else
return error(0, "unknown function");
if (value.empty() || value == 0)
return error(0, "zero value");
return true;
}
void merge_factors(std::vector<std::pair<arithmetic::Giant, int>>& factors_, Giant& cofactor_, int power)
{
for (auto& factor : factors)
::add_factor(factors_, factor.first, factor.second*power);
if (!cofactor.empty())
{
cofactor.power(abs(power));
if (power < 0)
{
if (cofactor_.empty() || cofactor_%cofactor != 0)
throw ArithmeticException();
cofactor_ /= cofactor;
}
else if (!cofactor_.empty())
cofactor_ *= cofactor;
else
cofactor_ = cofactor;
}
}
std::string& str_args(int index) { return args[index].second; }
private:
bool error(int pos, const char* msg) { error_pos = pos; error_msg = msg; return false; }
bool error(int pos, std::string& msg) { error_pos = pos; error_msg = std::move(msg); return false; }
public:
std::string str_func;
std::vector<std::pair<int, std::string>> args;
Giant value;
std::string str_value;
std::vector<std::pair<arithmetic::Giant, int>> factors;
Giant cofactor;
int error_pos = -1;
std::string error_msg;
};
class Token
{
public:
Token() { }
bool iterate(const std::string& str, std::string::const_iterator& it)
{
pos = (int)(it - str.begin());
if (it == str.end())
return false;
if (*it == '+' || *it == '-' || *it == '*' || *it == '/' || *it == '^' || *it == '!' || *it == '#')
{
oper = *it;
it++;
return true;
}
std::string::const_iterator it_pos = it;
std::string::const_iterator it_s;
if (std::isdigit(*it))
{
expr.reset(new Expr());
for (it_s = it; it != str.end() && std::isdigit(*it); it++);
expr->args.emplace_back((int)(it_s - it_pos), std::string(it_s, it));
return true;
}
if (std::isalpha(*it))
{
expr.reset(new Expr());
for (it_s = it; it != str.end() && std::isalpha(*it); it++);
expr->str_func = std::string(it_s, it);
if (it == str.end())
return true;
if (std::isdigit(*it))
{
for (it_s = it; it != str.end() && std::isdigit(*it); it++);
expr->args.emplace_back((int)(it_s - it_pos), std::string(it_s, it));
return true;
}
}
if (*it == '(')
{
if (!expr)
{
expr.reset(new Expr());
expr->str_func = "()";
}
while (it != str.end() && *it != ')')
{
it++;
int recursion = 0;
for (it_s = it; it != str.end() && (recursion > 0 || (*it != ')' && *it != ',')); it++)
{
if (*it == '(')
recursion++;
if (*it == ')')
recursion--;
}
expr->args.emplace_back((int)(it_s - it_pos), std::string(it_s, it));
}
if (it != str.end() && *it == ')')
it++;
return true;
}
return false;
}
public:
int pos = 0;
char oper = 0;
std::unique_ptr<Expr> expr;
};
Giant factorial(Giant& factor)
{
uint32_t n = factor.data()[0];
uint32_t multifactorial = factor.data()[1];
Giant res, tmp;
res = 1;
tmp = 1;
uint32_t i = n%multifactorial;
while (i < 2)
i += multifactorial;
for (; i <= n; i += multifactorial)
{
tmp *= i;
if (tmp.size() > 8192 || i == n)
{
res *= tmp;
tmp = 1;
}
}
return res;
}
Giant primorial(Giant& factor)
{
int n = (int)factor.data()[0];
Giant res, tmp;
res = 1;
tmp = 1;
int last = 1;
for (auto it = PrimeIterator::get(); *it <= n; it++)
{
last = *it;
tmp *= last;
if (tmp.size() > 8192)
{
res *= tmp;
tmp = 1;
}
}
if (tmp != 1)
res *= tmp;
factor.data()[0] = last;
return res;
}
InputNum::ParseResult InputNum::parse(const std::string& s, bool c_required)
{
Giant tmp;
int type = KBNC;
Giant gk, gb, gd;
uint32_t n = 0;
int64_t c = 0;
std::string custom_k;
std::string custom_b;
std::string custom_d;
uint32_t multifactorial = 0;
int algebraic_type = ALGEBRAIC_SIMPLE;
int32_t algebraic_k = 0;
std::vector<std::pair<arithmetic::Giant, int>> factors;
Giant cofactor;
std::vector<std::pair<arithmetic::Giant, int>> b_factors;
Giant b_cofactor;
std::vector<Token> tokens;
std::string::const_iterator its;
for (its = s.begin(); its != s.end() && std::isspace(*its); its++);
if (its != s.end() && *its == '\"')
its++;
while (its != s.end())
{
Token token;
if (!token.iterate(s, its))
break;
tokens.push_back(std::move(token));
}
if (its != s.end() && *its == '\"')
its++;
for (; its != s.end() && std::isspace(*its); its++);
if (its != s.end())
return InputNum::ParseResult(false, (int)(its - s.begin()), "excess symbols");
auto it_expr = tokens.begin();
if (it_expr == tokens.end())
return InputNum::ParseResult(false, 0, "no value");
auto it_oper = it_expr + 1;
if (it_expr->expr && it_expr->expr->str_func == "()")
{
bool bf = false;
auto itF = it_oper;
while (itF != tokens.end() && itF->oper == '/')
{
itF++;
if (itF == tokens.end() || !itF->expr)
{
bf = false;
break;
}
bf = true;
itF++;
}
if (itF != tokens.end())
bf = false;
if (bf)
{
InputNum recursive;
InputNum::ParseResult res = recursive.parse(it_expr->expr->str_args(0));
if (!res)
return InputNum::ParseResult(false, it_expr->pos + it_expr->expr->args[0].first + res.pos, res.message);
it_expr++;
Giant gf;
std::string custom_f;
while (it_expr != tokens.end())
{
it_expr++;
if (!it_expr->expr->evaluate(false))
return InputNum::ParseResult(false, it_expr->pos + it_expr->expr->error_pos, it_expr->expr->error_msg);
if (gf.empty())
{
gf = std::move(it_expr->expr->value);
custom_f = std::move(it_expr->expr->str_value);
}
else
{
if (!custom_f.empty() || !it_expr->expr->str_value.empty())
{
if (custom_f.empty())
custom_f = gf.to_string();
if (it_expr->expr->str_value.empty())
it_expr->expr->str_value = it_expr->expr->value.to_string();
custom_f += "/";
custom_f += it_expr->expr->str_value;
}
gf *= it_expr->expr->value;
}
it_expr++;
}
if (recursive.value()%gf != 0)
return InputNum::ParseResult(false, (int)s.size(), "not divisible");
_type = recursive._type;
_gk = std::move(recursive._gk);
_gb = std::move(recursive._gb);
_n = recursive._n;
_c = recursive._c;
_custom_k = std::move(recursive._custom_k);
_custom_b = std::move(recursive._custom_b);
_custom_d = std::move(recursive._custom_d);
_multifactorial = recursive._multifactorial;
_algebraic_type = recursive._algebraic_type;
_algebraic_k = recursive._algebraic_k;
_factors = std::move(recursive._factors);
_cofactor = std::move(recursive._cofactor);
_b_factors = std::move(recursive._b_factors);
_b_cofactor = std::move(recursive._b_cofactor);
_input_text = "(" + recursive._input_text + ")/" + (!custom_f.empty() ? custom_f : gf.to_string());
if (recursive._gf != 1 && (!custom_f.empty() || !recursive._custom_f.empty()))
{
if (custom_f.empty())
custom_f = gf.to_string();
if (recursive._custom_f.empty())
recursive._custom_f = recursive._gf.to_string();
custom_f = recursive._custom_f + "/" + custom_f;
}
if (_type == GENERIC)
{
if (!_custom_b.empty())
_custom_b = "(" + _custom_b + ")/" + (!custom_f.empty() ? custom_f : gf.to_string());
custom_f.clear();
_gb /= gf;
gf = 1;
}
_gf = std::move(gf)*recursive._gf;
_custom_f = std::move(custom_f);
_display_text = build_text(30);
return InputNum::ParseResult(true);
}
}
if (it_expr->expr && !it_expr->expr->str_func.empty() && it_expr->expr->str_func != "p" && (it_oper == tokens.end()))
{
if (it_expr->expr->str_func == "Phi") // (X +- 1)*X + 1
{
if (it_expr->expr->args.size() != 2)
return InputNum::ParseResult(false, it_expr->pos, "two arguments expected");
int cyclotomic = stoi(it_expr->expr->str_args(0));
if (cyclotomic != 3 && cyclotomic != 6)
return InputNum::ParseResult(false, it_expr->pos + it_expr->expr->args[0].first, "only 3 and 6 are supported");
if (!it_expr->expr->str_args(1).empty() && it_expr->expr->str_args(1)[0] == '-')
{
cyclotomic = (cyclotomic == 3 ? 6 : 3);
it_expr->expr->str_args(1) = it_expr->expr->str_args(1).substr(1);
}
InputNum recursive;
InputNum::ParseResult res = recursive.parse(it_expr->expr->str_args(1) + "+1");
if (!res)
return InputNum::ParseResult(false, it_expr->pos + it_expr->expr->args[1].first + res.pos, res.message);
type = recursive.type();
gk = recursive.value();
custom_k = "(" + recursive.input_text() + ")";
if (cyclotomic == 6)
{
gk -= 2;
custom_k[custom_k.size() - 3] = '-';
}
factors = std::move(recursive.factors());
cofactor = std::move(recursive.cofactor());
b_factors = std::move(recursive.b_factors());
b_cofactor = std::move(recursive.b_cofactor());
::factorize(gk, factors, cofactor);
if (recursive.k() != 1)
{
gk *= recursive.gk();
custom_k += "*" + recursive.gk().to_string();
}
gb = recursive.gb();
custom_b = recursive._custom_b;
n = recursive._n;
multifactorial = recursive._multifactorial;
custom_d = recursive._custom_d;
c = 1;
if (recursive.k() > 0 && recursive.k() < 1000)
{
algebraic_type = ALGEBRAIC_CYCLOTOMIC;
algebraic_k = (cyclotomic == 6 ? -1 : 1)*(int32_t)recursive.k();
}
}
else if (it_expr->expr->str_func == "Quad" || // (1/2 X +- 1)*X + 1
it_expr->expr->str_func == "Hex") // (1/3 X +- 1)*X + 1
{
if (it_expr->expr->args.size() != 1)
return InputNum::ParseResult(false, it_expr->pos, "one argument expected");
bool neg = false;
if (!it_expr->expr->str_args(0).empty() && it_expr->expr->str_args(0)[0] == '-')
{
neg = true;
it_expr->expr->str_args(0) = it_expr->expr->str_args(0).substr(1);
}
int divisor = (it_expr->expr->str_func == "Quad" ? 2 : 3);
InputNum recursive;
InputNum::ParseResult res = recursive.parse(it_expr->expr->str_args(0) + "+1");
if (!res)
return InputNum::ParseResult(false, it_expr->pos + it_expr->expr->args[0].first + res.pos, res.message);
type = recursive.type();
gk = recursive.value();
gk -= 1;
if (gk%divisor != 0)
return InputNum::ParseResult(false, it_expr->pos + it_expr->expr->args[0].first, "should be divisible by " + std::to_string(divisor));
gk /= divisor;
recursive._c = (neg ? -1 : 1);
gk += recursive._c;
factors = std::move(recursive.factors());
cofactor = std::move(recursive.cofactor());
b_factors = std::move(recursive.b_factors());
b_cofactor = std::move(recursive.b_cofactor());
::factorize(gk, factors, cofactor);
if (recursive.k() != 1)
{
gk *= recursive.gk();
custom_k = "*" + recursive.gk().to_string();
}
gb = recursive.gb();
custom_b = recursive._custom_b;
n = recursive._n;
multifactorial = recursive._multifactorial;
custom_d = recursive._custom_d;
c = 1;
if (it_expr->expr->str_func == "Quad" && recursive.k() > 0 && recursive.k() < 100)
{
algebraic_type = ALGEBRAIC_QUAD;
algebraic_k = (int32_t)(recursive.k()*recursive._c);
}
if (it_expr->expr->str_func == "Hex" && recursive.k() > 0 && recursive.k() < 32)
{
algebraic_type = ALGEBRAIC_HEX;
algebraic_k = (int32_t)(recursive.k()*recursive._c);
}
if (type == KBNC)
{
if (recursive.gk()%divisor == 0)
recursive.gk() /= divisor;
else
{
recursive.gk() *= recursive.gb()/divisor;
recursive._n--;
}
custom_k = "(" + recursive.build_text() + ")" + custom_k;
}
else if (type == FACTORIAL || type == PRIMORIAL)
{
std::string st = recursive.build_text();
custom_k = "(" + st.substr(0, st.size() - 2) + "/" + std::to_string(divisor) + st.substr(st.size() - 2) + ")" + custom_k;
}
}
else
return InputNum::ParseResult(false, it_expr->pos, "unknown function");
it_expr++;
}
else
{
gk = 1;
while (it_oper != tokens.end() && it_oper->oper == '*' && it_expr->expr)
{
if (!it_expr->expr->evaluate())
return InputNum::ParseResult(false, it_expr->pos + it_expr->expr->error_pos, it_expr->expr->error_msg);
if (gk == 1)
{
gk = std::move(it_expr->expr->value);
custom_k = std::move(it_expr->expr->str_value);
it_expr->expr->merge_factors(factors, cofactor, 1);
}
else
{
if (!custom_k.empty() || !it_expr->expr->str_value.empty())
{
if (custom_k.empty())
custom_k = gk.to_string();
if (it_expr->expr->str_value.empty())
it_expr->expr->str_value = it_expr->expr->value.to_string();
custom_k += "*";
custom_k += it_expr->expr->str_value;
}
gk *= it_expr->expr->value;
it_expr->expr->merge_factors(factors, cofactor, 1);
}
it_oper++;
it_expr = it_oper;
if (it_oper != tokens.end())
it_oper++;
}
if (it_expr == tokens.end())
return InputNum::ParseResult(false, (int)s.size(), "unexpected end");
if (!it_expr->expr)
return InputNum::ParseResult(false, it_expr->pos, "unexpected operator");
if (it_oper != tokens.end() && it_oper->expr)
return InputNum::ParseResult(false, it_oper->pos, "unexpected value");
if (it_oper == tokens.end() || it_oper->oper == '^' || it_oper->oper == '+' || it_oper->oper == '-' || it_oper->oper == '/')
{
if (!it_expr->expr->evaluate())
return InputNum::ParseResult(false, it_expr->pos + it_expr->expr->error_pos, it_expr->expr->error_msg);
gb = std::move(it_expr->expr->value);
custom_b = std::move(it_expr->expr->str_value);
it_expr->expr->merge_factors(b_factors, b_cofactor, 1);
Expr* b_expr = it_expr->expr.get();
it_expr++;
if (it_expr != tokens.end())
it_expr++;
if (it_oper != tokens.end() && it_oper->oper == '^')
{
if (it_expr == tokens.end())
return InputNum::ParseResult(false, (int)s.size(), "unexpected end");
if (!it_expr->expr)
return InputNum::ParseResult(false, it_expr->pos, "unexpected operator");
if (!it_expr->expr->evaluate(false))
return InputNum::ParseResult(false, it_expr->pos + it_expr->expr->error_pos, it_expr->expr->error_msg);
if (it_expr->expr->value.size() > 1)
return InputNum::ParseResult(false, it_expr->pos, "exponent too big");
n = it_expr->expr->value.data()[0];
it_expr++;
it_oper = it_expr;
if (it_expr != tokens.end())
it_expr++;
}
else
n = 1;
b_expr->merge_factors(factors, cofactor, (int)n);
}
else if (it_oper->oper == '!')
{
type = FACTORIAL;
if (!it_expr->expr->evaluate(false))
return InputNum::ParseResult(false, it_expr->pos + it_expr->expr->error_pos, it_expr->expr->error_msg);
if (it_expr->expr->value.size() > 1)
return InputNum::ParseResult(false, it_expr->pos, "factorial too big");
n = it_expr->expr->value.data()[0];
it_oper++;
for (multifactorial = 1; it_oper != tokens.end() && it_oper->oper == '!'; it_oper++, multifactorial++);
it_expr = it_oper;
if (multifactorial == 1 && it_expr != tokens.end() && it_expr->expr)
{
if (!it_expr->expr->evaluate(false))
return InputNum::ParseResult(false, it_expr->pos + it_expr->expr->error_pos, it_expr->expr->error_msg);
if (it_expr->expr->value > n)
return InputNum::ParseResult(false, it_expr->pos, "multifactorial step too big");
multifactorial = it_expr->expr->value.data()[0];
it_expr++;
it_oper = it_expr;
}
if (it_expr != tokens.end())
it_expr++;
Giant factor;
factor = ((uint64_t)multifactorial << 32) + n;
factor.arithmetic().neg(factor, factor);
gb = factorial(factor);
::add_factor(factors, factor, 1);
}
else if (it_oper->oper == '#')
{
type = PRIMORIAL;
if (!it_expr->expr->evaluate(false))
return InputNum::ParseResult(false, it_expr->pos + it_expr->expr->error_pos, it_expr->expr->error_msg);
if (it_expr->expr->value.bitlen() > 31)
return InputNum::ParseResult(false, it_expr->pos, "primorial too big");
Giant factor;
factor.arithmetic().neg(it_expr->expr->value, factor);
gb = primorial(factor);
n = factor.data()[0];
::add_factor(factors, factor, 1);
it_oper++;
it_expr = it_oper;
if (it_expr != tokens.end())
it_expr++;
}
else
return InputNum::ParseResult(false, it_oper->pos, "unexpected symbol");
if (it_oper != tokens.end())
{
if (it_oper->expr)
return InputNum::ParseResult(false, it_oper->pos, "unexpected value");
if (it_expr == tokens.end())
return InputNum::ParseResult(false, (int)s.size(), "unexpected end");
if (!it_expr->expr)
return InputNum::ParseResult(false, it_expr->pos, "unexpected operator");
}
gd = 1;
while (it_expr != tokens.end() && it_expr->expr && it_oper->oper == '/')
{
if (!it_expr->expr->evaluate())
return InputNum::ParseResult(false, it_expr->pos + it_expr->expr->error_pos, it_expr->expr->error_msg);
if (gd == 1)
{
gd = std::move(it_expr->expr->value);
custom_d = std::move(it_expr->expr->str_value);
it_expr->expr->merge_factors(factors, cofactor, -1);
}
else
{
if (!custom_d.empty() || !it_expr->expr->str_value.empty())
{
if (custom_d.empty())
custom_d = gd.to_string();
if (it_expr->expr->str_value.empty())
it_expr->expr->str_value = it_expr->expr->value.to_string();
custom_d += "/";
custom_d += it_expr->expr->str_value;
}
gd *= it_expr->expr->value;
it_expr->expr->merge_factors(factors, cofactor, -1);
}
it_expr++;
it_oper = it_expr;
if (it_expr != tokens.end())
it_expr++;
}
if (gd > 1 && gk*(type == KBNC ? power(gb, n) : gb)%gd != 0)
return InputNum::ParseResult(false, it_oper != tokens.end() ? it_oper->pos : (int)s.size(), "not divisible");
if (it_oper != tokens.end())
{
if (it_oper->expr)
return InputNum::ParseResult(false, it_oper->pos, "unexpected value");
if (it_expr == tokens.end())
return InputNum::ParseResult(false, (int)s.size(), "unexpected end");
if (!it_expr->expr)
return InputNum::ParseResult(false, it_expr->pos, "unexpected operator");
}
if (it_expr != tokens.end() && (it_oper->oper == '+' || it_oper->oper == '-'))
{
bool minus = (it_oper->oper == '-');
if (!it_expr->expr->evaluate(false))
return InputNum::ParseResult(false, it_expr->pos + it_expr->expr->error_pos, it_expr->expr->error_msg);
if (it_expr->expr->value.bitlen() > 63)
return InputNum::ParseResult(false, it_expr->pos, "C too big");
if (it_expr->expr->value.size() == 1)
c = it_expr->expr->value.data()[0];
if (it_expr->expr->value.size() == 2)
c = *(int64_t*)it_expr->expr->value.data();
if (minus)
c = -c;
it_expr++;
it_oper = it_expr;
}
else if (it_oper == tokens.end() && type == KBNC && gk == 1 && n == 1 && gd == 1)
{
type = GENERIC;
n = 0;
}
else if (c_required)
return InputNum::ParseResult(false, it_oper != tokens.end() ? it_oper->pos : (int)s.size(), "C expected");
}
if (it_oper != tokens.end())
return InputNum::ParseResult(false, it_oper->pos, it_oper->expr ? "unexpected value" : "unexpected operator");
if (it_expr != tokens.end())
return InputNum::ParseResult(false, it_expr->pos, it_expr->expr ? "unexpected value" : "unexpected operator");
_type = type;
_gk = std::move(gk);
_gb = std::move(gb);
_n = n;
_c = c;
_gf = 1;
_custom_k = std::move(custom_k);
_custom_b = std::move(custom_b);
_custom_d = std::move(custom_d);
_custom_f.clear();
_multifactorial = multifactorial;
_algebraic_type = algebraic_type;
_algebraic_k = algebraic_k;
_factors = std::move(factors);
_cofactor = std::move(cofactor);
_b_factors = std::move(b_factors);
_b_cofactor = std::move(b_cofactor);
std::sort(_factors.begin(), _factors.end(), [](std::pair<arithmetic::Giant, int>& a, std::pair<arithmetic::Giant, int>& b) { return (a.second > 0 && b.second < 0) || a.first < b.first; });
process(gd, true);
return InputNum::ParseResult(true);
}
void InputNum::process()
{
arithmetic::Giant gd;
gd = 1;
process(gd, false);
}