I am currently trying to vectorize a modulo calculation using AVX512.
Because there is no AVX modulo operation (except SVML) or an integer division, I am using the following formula d % p = d - int(float(d)/float(p))*p.
However, for very large inputs, I get negative results.
#include <bits/stdc++.h>
#include <immintrin.h>
int main() {
const auto SIZE = 1024;
int64_t input[SIZE];
int64_t output[SIZE] = {};
const auto p = 1'536; // 1.5 * 1024
std::iota(input, input + SIZE, 15596705878733779060ULL);
__m512i _divider_512 = _mm512_set1_epi64(p);
for (size_t idx = 0; idx < SIZE; idx += 8) {
__m512i _inputs = _mm512_loadu_si512(&input[idx]);
__m512i _e = _mm512_cvt_roundpd_epi64(_mm512_div_pd(_mm512_cvtepi64_pd(_inputs), _mm512_cvtepi64_pd(_divider_512)), _MM_FROUND_TO_NEG_INF | _MM_FROUND_NO_EXC);
__m512i _mult = _mm512_mullo_epi64(_e, _divider_512);
__m512i _modulo_result = _mm512_sub_epi64(_inputs, _mult);
_mm512_storeu_si512(&output[idx], _modulo_result);
}
for (auto i = 0; i< SIZE; ++i) {
std::cout << output[i] << std::endl;
}
}
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
-255
-254
-253
-252
-251
-250
-249
-248
-247
-246
-245
-244
-243
-242
-241
-240
-239
-238
-237
-236
-235
-234
-233
-232
-231
-230
-229
-228
-227
-226
-225
-224
-223
-222
-221
-220
-219
-218
-217
-216
-215
-214
-213
-212
-211
-210
-209
-208
-207
-206
-205
-204
-203
-202
-201
-200
-199
-198
-197
-196
-195
-194
-193
-192
-191
-190
-189
-188
-187
-186
-185
-184
-183
-182
-181
-180
-179
-178
-177
-176
-175
-174
-173
-172
-171
-170
-169
-168
-167
-166
-165
-164
-163
-162
-161
-160
-159
-158
-157
-156
-155
-154
-153
-152
-151
-150
-149
-148
-147
-146
-145
-144
-143
-142
-141
-140
-139
-138
-137
-136
-135
-134
-133
-132
-131
-130
-129
-128
-127
-126
-125
-124
-123
-122
-121
-120
-119
-118
-117
-116
-115
-114
-113
-112
-111
-110
-109
-108
-107
-106
-105
-104
-103
-102
-101
-100
-99
-98
-97
-96
-95
-94
-93
-92
-91
-90
-89
-88
-87
-86
-85
-84
-83
-82
-81
-80
-79
-78
-77
-76
-75
-74
-73
-72
-71
-70
-69
-68
-67
-66
-65
-64
-63
-62
-61
-60
-59
-58
-57
-56
-55
-54
-53
-52
-51
-50
-49
-48
-47
-46
-45
-44
-43
-42
-41
-40
-39
-38
-37
-36
-35
-34
-33
-32
-31
-30
-29
-28
-27
-26
-25
-24
-23
-22
-21
-20
-19
-18
-17
-16
-15
-14
-13
-12
-11
-10
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
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
Is this a rounding error between ints and doubles? Or am I missing something else?
And how could I workaround it?
Related
I create an AVL-Tree with 25 random integers, and I hope to delete each integer in its original order. So every time I delete a number, I take a InOrderTraverse. But it causes some interrupts and I find that the list of InOrderTraverse does not decrease one by one, it has some chasms like this( see delete 143 and 564):
Create AVL-Tree:
279 302 225 801 299 416 72 91 738 150 629 265 343 456 63 347 143 564 908 930 616 715 169 36 67
InOrderTraverse:
36 63 67 72 91 143 150 169 225 265 279 299 302 343 347 416 456 564 616 629 715 738 801 908 930
Remove:
delete:279 Traverse: 36 63 67 72 91 143 150 169 225 265 299 302 343 347 416 456 564 616 629 715 738 801 908 930
delete:302 Traverse: 36 63 67 72 91 143 150 169 225 265 299 343 347 416 456 564 616 629 715 738 801 908 930
delete:225 Traverse: 36 63 67 72 91 143 150 169 265 299 343 347 416 456 564 616 629 715 738 801 908 930
delete:801 Traverse: 36 63 67 72 91 143 150 169 265 299 343 347 416 456 564 616 629 715 738 908 930
delete:299 Traverse: 36 63 67 72 91 143 150 169 265 343 347 416 456 564 616 629 715 738 908 930
delete:416 Traverse: 36 63 67 72 91 143 150 169 265 343 347 456 564 616 629 715 738 908 930
delete:72 Traverse: 36 63 67 91 143 150 169 265 343 347 456 564 616 629 715 738 908 930
delete:91 Traverse: 36 63 67 143 150 169 265 343 347 456 564 616 629 715 738 908 930
delete:738 Traverse: 36 63 67 143 150 169 265 343 347 456 564 616 629 715 908 930
delete:150 Traverse: 36 63 67 143 169 265 343 347 456 564 616 629 715 908 930
delete:629 Traverse: 36 63 67 143 169 265 343 347 456 564 616 715 908 930
delete:265 Traverse: 36 63 67 143 169 343 347 456 564 616 715 908 930
delete:343 Traverse: 36 63 67 143 169 347 456 564 616 715 908 930
delete:456 Traverse: 36 63 67 143 169 347 564 616 715 908 930
delete:63 Traverse: 36 67 143 169 347 564 616 715 908 930
delete:347 Traverse: 36 67 143 169 564 616 715 908 930
delete:143 Traverse: 36 67 169 564 616 715 908 930
delete:564 Traverse: 36 67 169 616 715
delete:908 Traverse: 36 67 169 616 715
delete:930 Traverse: 36 67 169 616 715
delete:616 Traverse: 36 67 169 715
delete:715 Traverse: 36 67 169
delete:169 Traverse: 36 67
delete:36 Traverse:
I have checked my textbook and searched on the Internet, but it still has not been resolved.
I hope you can help me. Many thanks!
Below is the complete code:https://paste.ubuntu.com/p/SD3pDnYNJY/
Hi I am a beginner in programming, and I want to read many integers distributed in row and columns, and how many integer is there. Whenever I try infile>>x it just take the first value.
I would appreciate it so much ,if you can just explain how can you do it to me.
the file looks like this:(much longer but I want you to get the idea):
41 467 334 500 169 724 478 358 962 464 705 145 281 827 961 491 995 942 827 436 391 604 902 153 292 382 421 716 718 895 447 726 771 538 869 912 667 299 35 894 703 811 322 333 673 664 141 711 253 868 547 644 662 757 37 859 723 741 529 778 316 35 190 842 288 106 40 942 264 648 446 805 890 729 370 350 6 101 393 548 629 623 84 954 756 840 966 376 931 308 944 439 626 323 537 538 118 82 929 541 833 115 639 658 704 930 977 306 673 386 21 745 924 72 270 829 777 573 97 512 986 290 161 636 355 767 655 574 31 52 350 150 941 724 966 430 107 191 7 337 457 287 753 383 945 909 209 758 221 588 422 946 506 30 413 168 900 591 762 655 410 359 624 537 548 483 595 41 602 350 291 836 374 20 596 21 348 199 668 484 281 734 53 999 418 938 900 788 127 467 728 893 648 483 807 421 310 617 813 514 309 616 935 451 600 249 519 556 798 303 224 8 844 609 989 702 195 485 93 343 523 587 314 503 448 200 458 618 580 796 798 281 589 798 9 157 472 622 538 292 38 179 190 657 958 191 815 888 156 511 202 634 272 55 328 646 362 886 875 433 869 142 844 416 881 998 322 651 21 699 557 476 892 389 75 712 600 510 3 869 861 688 401 789 255 423 2 585 182 285 88 426 617 757 832 932 169 154 721 189 976 329 368 692 425 555 434 549 441 512 145 60 718 753 139 423 279 996 687 529 549 437 866 949 193 195 297 416 286 105 488 282 455 734 114 701 316 671 786 263 313 355 185 53 912 808 832 945 313 756 321 558 646 982 481 144 196 222 129 161 535 450 173 466 44 659 292 439 253 24 154 510 745 649 186 313 474 22 168 18 787 905 958 391 202 625 477 414 314 824 334 874 372 159 833 70 487 297 518 177 773 270 763 668 192 985 102 480 213 627 802 99 527 625 543 924 23 972 61 181 3 432 505 593 725 31 492 142 222 286 64 900 187 360 413 974 270 170 235 833 711 760 896 667 285 550 140 694 695 624 19 125 576 694 658 302 371 466 678 593 851 484 18 464 119 152 800 87 60 926 10 757 170 315 576
Try using a while to loop through every integer in the file:
while(infile>>x) {
// Do something with the integer
}
Note that if you want it in a 2 dimensional array of rows and columns, you could do something more elaborate like this using a std::stringstream:
#include <sstream>
#include <string>
//...
std::vector<std::vector<int>> result;
std::string tmp;
while(getline(infile,tmp)) {
stringstream stream;
stream << tmp;
// add new row
std::vector<int> row;
while (ss >> x) {
// add x to the new row
}
result.push_back(row);
}
Why does this code return 10 on the test machine for the input data:
5
4 2 3 3 3
while on my PC it correctly returns 12?
#include <iostream>
#include <cstdlib>
using namespace std;
int n;
int b[99];
int money = 0;
int min()
{
int min = b[0];
int index = 0;
for (int i = 1; i < n; i++)
if (b[i] < min && b[i] % 2 != 0) {
min = b[i];
index = i;
}
for (int i = index; i < n - 1; i++)
{
b[i] = b[i + 1];
}
b[n - 1] = 0;
return min;
}
int main()
{
money = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> b[i];
money += b[i];
}
while (money % 2 != 0)
{
money -= min();
}
if (money % 2 == 0)
if (money != 0)
cout << money;
else cout << "NIESTETY";
system("pause");
}
Thanks!
Edit: It happened on my PC too, once. But for other input data it works perfectly. And i'm not talking about some simple values. I've run some recommended tests for this problem, this for example:
581
906 369 899 998 73 717 269 51 875 61 123 237 50 924 576 52 693 394 952 24 534 452 634 139 642 570 20 643 128 165 144 867 86 256 981 26 344 912 489 524 667 375 502 944 961 61 586 834 62 240 644 602 563 758 587 973 440 920 977 785 999 186 384 231 414 184 84 492 277 787 737 312 849 595 238 892 377 103 275 797 847 652 263 526 473 643 972 701 209 70 95 852 337 387 651 451 843 649 445 172 592 299 110 479 191 148 324 600 928 258 354 693 522 637 977 180 394 306 846 885 85 339 678 355 799 297 232 929 133 173 698 362 592 426 319 690 195 944 143 571 831 765 847 106 293 460 650 4 876 891 272 987 171 767 495 486 161 243 959 431 707 761 634 574 774 176 84 903 102 223 7 759 886 113 626 917 873 177 307 46 56 415 162 537 845 956 704 725 551 266 673 255 567 520 278 1000 450 573 205 447 656 946 460 331 317 125 340 372 246 635 667 642 422 766 669 894 217 759 396 160 985 46 61 832 375 272 276 49 820 440 259 482 569 97 903 135 164 570 412 320 827 403 610 577 350 543 328 481 136 595 584 935 830 134 587 658 604 865 742 111 668 313 669 233 592 451 735 539 890 319 882 674 306 376 945 313 872 683 915 92 954 598 750 106 576 259 563 293 158 708 716 259 434 833 867 754 678 576 981 55 8 421 31 176 641 707 175 531 299 65 55 118 990 696 533 713 829 20 310 201 748 697 397 550 909 584 366 6 515 298 128 483 454 564 986 171 225 35 733 664 368 674 146 479 70 610 734 708 550 962 227 606 520 751 449 254 6 253 757 942 63 926 592 768 516 843 187 836 722 329 745 54 703 437 671 463 288 548 847 563 354 964 27 100 120 351 928 557 757 937 872 417 18 335 181 68 834 994 83 178 67 45 965 31 976 155 883 726 173 813 66 199 211 619 429 643 237 464 586 929 546 675 392 350 811 187 936 64 167 394 969 36 630 430 377 905 283 36 352 221 293 125 975 214 903 622 769 955 759 83 104 988 876 612 308 495 612 634 288 581 164 595 685 620 817 988 355 134 71 159 493 730 952 70 620 961 407 78 159 660 542 991 858 101 140 531 156 667 323 621 365 193 825 252 848 897 5 16 277 461 909 549 194 638 422 718 979 705 515 499 488 447 249 229 382 315 851 625 249 818 889 580 224 926 270 734 689 275 842 834 838 774 949 101 728 930 478 397 188 228 675 36 259 834 745 629 374 664 335 30 745 963 918 38 848 482 739 745 912 154 408 250 271 480 767 120 142 652 169 764 541 228 919 482 984 942 631 46 529 395 576
//Output: 289826
Suppose I have this list of prime (totally 100):
(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541)
I want to print them in rows, with 5 numbers each row:
( 2 3 5 7 11
13 17 19 23 29
31 37 41 43 47
....
(Please note that the numbers in column are right aligned.)
This is based on another answer above.
(require '[clojure.string :as string])
user=> (println
(str \(
(string/join "\n "
(map #(apply format "%3d %3d %3d %3d %3d" %)
(partition 5 primes)))
\)))
( 2 3 5 7 11
13 17 19 23 29
31 37 41 43 47
53 59 61 67 71
73 79 83 89 97
101 103 107 109 113
127 131 137 139 149
151 157 163 167 173
179 181 191 193 197
199 211 223 227 229
233 239 241 251 257
263 269 271 277 281
283 293 307 311 313
317 331 337 347 349
353 359 367 373 379
383 389 397 401 409
419 421 431 433 439
443 449 457 461 463
467 479 487 491 499
503 509 521 523 541)
I'm solving a problem over at project euler which requires me to find the sum of all primes under 2 million. I tried to implement sieve of atkin and strangely it sets numbers like 65,85 as primes. I looked at the code and algorithm for over a a day but can't find anything wrong. I'm sure it must be something silly but i can't find it. Thanks in advance
i'm using visual studio express 2012.
here's the code:
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <vector>
#include <fstream>
#include <conio.h>
int main(){
long long int limit,n;
std::cout<<"Enter a number...."<<std::endl;
std::cin>>limit;
std::vector<bool> prime;
for(long long int k=0;k<limit;k++){ //sets all entries in the vector 'prime' to false
prime.push_back(false);
}
long long int root_limit= ceil(sqrt(limit));
//sive of atkin implementation
for(long long int x=1;x<=root_limit;x++){
for(long long int y=1;y<=root_limit;y++){
n=(4*x*x)+(y*y);
if(n<=limit && (n%12==1 || n%12==5)){
prime[n]=true;
}
n=(3*x*x)+(y*y);
if(n<=limit && n%12==7){
prime[n]=true;
}
n=(3*x*x)-(y*y);
if(x>y && n<=limit && n%12==11){
prime[n]=true;
}
}
}
//loop to eliminate squares of the primes(making them square free)
for(long long int i=5;i<=root_limit;i++){
if(prime[i]==true){
for(long long int j=i*i;j<limit;j+=(i*i)){
prime[j]=false;
}
}
}
unsigned long long int sum=0;
//print values to a seperate text file
std::ofstream outputfile("data.txt");
outputfile<<"2"<<std::endl;
outputfile<<"3"<<std::endl;
for(long long int l=5;l<limit;l++){
if(prime[l]==true){
sum+=l;
outputfile<<l<<std::endl;;
}
}
outputfile.close();
std::cout<<"The sum is...."<<sum+5<<std::endl;
prime.clear();
return 0;
}
and hers the data.txt i pointed out few errors
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
65<-----
67
71
73
79
83
85<-----
89
91
97
101
103
107
109
113
127
131
137
139
143
145<----
149
151
157
163
167
173
179
181
185<----
191
193
197
199
205
211
221
223
227
229
233
239
241
247
251
257
259
263
265
269
271
277
281
283
293
299
305
307
311
313
317
331
337
347
349
353
359
365
367
373
377
379
383
389
397
401
403
407
409
419
421
427
431
433
439
443
445
449
457
461
463
467
479
481
485
487
491
493
499
503
505
509
511
521
523
533
541
545
547
557
559
563
565
569
571
577
587
593
599
601
607
611
613
617
619
629
631
641
643
647
653
659
661
671
673
677
679
683
685
689
691
697
701
703
709
719
727
733
739
743
745
751
757
761
763
767
769
773
785
787
793
797
803
809
811
821
823
827
829
839
851
853
857
859
863
865
871
877
881
883
887
901
905
907
911
919
923
929
937
941
947
949
953
965
967
971
977
983
985
991
997
1009
1013
1019
1021
1027
1031
1033
1037
1039
1049
1051
1061
1063
1067
1069
1073
1079
1087
1091
1093
1097
1099
1103
1105
1109
1117
1123
1129
1145
1147
1151
1153
1157
1159
1163
1165
1171
1181
1187
1189
1193
1199
1201
1205
1213
1217
1223
1229
1231
1237
1241
1249
1259
1261
1267
1277
1279
1283
1285
1289
1291
1297
1301
1303
1307
1313
1319
1321
1327
1339
1345
1351
1361
1367
1373
1381
1385
1387
1391
1399
1403
1405
1409
1417
1423
1427
1429
1433
1439
1447
1451
1453
1459
1465
1469
1471
1481
1483
1487
1489
1493
1499
1511
1513
1517
1523
1531
1537
1543
1549
1553
1559
1565
1567
1571
1579
1583
1585
1591
1597
1601
1603
1607
1609
1613
1619
1621
1627
1637
1649
1651
1657
1663
1667
1669
1679
1685
1687
1693
1697
1699
1703
1709
1717
1721
1723
1727
1733
1739
1741
1745
1747
1753
1759
1765
1769
1777
1781
1783
1787
1789
1801
1807
1811
1823
1831
1843
1847
1853
1861
1865
1867
1871
1873
1877
1879
1885
1889
1891
1901
1907
1913
1921
1931
1933
1937
1939
1945
1949
1951
1961
1963
1973
1979
1985
1987
1991
1993
1997
1999
You're supposed to flip the entries to the sieve list. In the first nested for loops instead of prime[n]=true; you should have prime[n]=!prime[n];