Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@timo
Created March 4, 2018 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timo/c6f33191957d56cec1b3c7978b50de7e to your computer and use it in GitHub Desktop.
Save timo/c6f33191957d56cec1b3c7978b50de7e to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl6
# The Expat License
#
# Copyright (c) 2018, Shlomi Fish
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
my $BASE = 1000000000;
sub calc_S(int $n, $token='foo')
{
my $out = run('./primesieve', $n, '-p1', :out).out.slurp-rest;
my int @primes = map { Int($_) }, split /\n/, $out;
return 0 if !@primes;
my int @d;
my int $r = 1;
for 0..$n -> $m
{
if $m +& 1
{
@d.push(0);
}
else
{
@d.push($r);
$r = (($r +< 1) % $BASE);
}
}
@primes.shift;
while @primes {
my int $p = @primes.shift;
say $token, ' ', $p;
for $p .. $n -> $m {
my int $one = @d.AT-POS($m);
my int $two = $p * @d.AT-POS($m-$p);
my int $three = $one + $two;
@d.ASSIGN-POS($m, $three % $BASE);
}
}
return @d;
}
my $a = 1;
my $b = 1;
my @Fk;
for 2.. 19 -> $i
{
@Fk.push($b);
($a, $b) = ($b, $a+$b);
}
say $a;
my int @ret := calc_S($a, "$a");
# assert ret[8] == 49
# assert ret[1] == 0
# assert ret[2] == 2
# assert ret[3] == 3
# assert ret[5] == 11
my $r = sum(@ret[@Fk]);
printf("ret = %d ; %09d", $r, $r % $BASE);
4181
4181 3
4181 5
4181 7
4181 11
4181 13
4181 17
4181 19
4181 23
4181 29
4181 31
4181 37
4181 41
4181 43
4181 47
4181 53
4181 59
4181 61
4181 67
4181 71
4181 73
4181 79
4181 83
4181 89
4181 97
4181 101
4181 103
4181 107
4181 109
4181 113
4181 127
4181 131
4181 137
4181 139
4181 149
4181 151
4181 157
4181 163
4181 167
4181 173
4181 179
4181 181
4181 191
4181 193
4181 197
4181 199
4181 211
4181 223
4181 227
4181 229
4181 233
4181 239
4181 241
4181 251
4181 257
4181 263
4181 269
4181 271
4181 277
4181 281
4181 283
4181 293
4181 307
4181 311
4181 313
4181 317
4181 331
4181 337
4181 347
4181 349
4181 353
4181 359
4181 367
4181 373
4181 379
4181 383
4181 389
4181 397
4181 401
4181 409
4181 419
4181 421
4181 431
4181 433
4181 439
4181 443
4181 449
4181 457
4181 461
4181 463
4181 467
4181 479
4181 487
4181 491
4181 499
4181 503
4181 509
4181 521
4181 523
4181 541
4181 547
4181 557
4181 563
4181 569
4181 571
4181 577
4181 587
4181 593
4181 599
4181 601
4181 607
4181 613
4181 617
4181 619
4181 631
4181 641
4181 643
4181 647
4181 653
4181 659
4181 661
4181 673
4181 677
4181 683
4181 691
4181 701
4181 709
4181 719
4181 727
4181 733
4181 739
4181 743
4181 751
4181 757
4181 761
4181 769
4181 773
4181 787
4181 797
4181 809
4181 811
4181 821
4181 823
4181 827
4181 829
4181 839
4181 853
4181 857
4181 859
4181 863
4181 877
4181 881
4181 883
4181 887
4181 907
4181 911
4181 919
4181 929
4181 937
4181 941
4181 947
4181 953
4181 967
4181 971
4181 977
4181 983
4181 991
4181 997
4181 1009
4181 1013
4181 1019
4181 1021
4181 1031
4181 1033
4181 1039
4181 1049
4181 1051
4181 1061
4181 1063
4181 1069
4181 1087
4181 1091
4181 1093
4181 1097
4181 1103
4181 1109
4181 1117
4181 1123
4181 1129
4181 1151
4181 1153
4181 1163
4181 1171
4181 1181
4181 1187
4181 1193
4181 1201
4181 1213
4181 1217
4181 1223
4181 1229
4181 1231
4181 1237
4181 1249
4181 1259
4181 1277
4181 1279
4181 1283
4181 1289
4181 1291
4181 1297
4181 1301
4181 1303
4181 1307
4181 1319
4181 1321
4181 1327
4181 1361
4181 1367
4181 1373
4181 1381
4181 1399
4181 1409
4181 1423
4181 1427
4181 1429
4181 1433
4181 1439
4181 1447
4181 1451
4181 1453
4181 1459
4181 1471
4181 1481
4181 1483
4181 1487
4181 1489
4181 1493
4181 1499
4181 1511
4181 1523
4181 1531
4181 1543
4181 1549
4181 1553
4181 1559
4181 1567
4181 1571
4181 1579
4181 1583
4181 1597
4181 1601
4181 1607
4181 1609
4181 1613
4181 1619
4181 1621
4181 1627
4181 1637
4181 1657
4181 1663
4181 1667
4181 1669
4181 1693
4181 1697
4181 1699
4181 1709
4181 1721
4181 1723
4181 1733
4181 1741
4181 1747
4181 1753
4181 1759
4181 1777
4181 1783
4181 1787
4181 1789
4181 1801
4181 1811
4181 1823
4181 1831
4181 1847
4181 1861
4181 1867
4181 1871
4181 1873
4181 1877
4181 1879
4181 1889
4181 1901
4181 1907
4181 1913
4181 1931
4181 1933
4181 1949
4181 1951
4181 1973
4181 1979
4181 1987
4181 1993
4181 1997
4181 1999
4181 2003
4181 2011
4181 2017
4181 2027
4181 2029
4181 2039
4181 2053
4181 2063
4181 2069
4181 2081
4181 2083
4181 2087
4181 2089
4181 2099
4181 2111
4181 2113
4181 2129
4181 2131
4181 2137
4181 2141
4181 2143
4181 2153
4181 2161
4181 2179
4181 2203
4181 2207
4181 2213
4181 2221
4181 2237
4181 2239
4181 2243
4181 2251
4181 2267
4181 2269
4181 2273
4181 2281
4181 2287
4181 2293
4181 2297
4181 2309
4181 2311
4181 2333
4181 2339
4181 2341
4181 2347
4181 2351
4181 2357
4181 2371
4181 2377
4181 2381
4181 2383
4181 2389
4181 2393
4181 2399
4181 2411
4181 2417
4181 2423
4181 2437
4181 2441
4181 2447
4181 2459
4181 2467
4181 2473
4181 2477
4181 2503
4181 2521
4181 2531
4181 2539
4181 2543
4181 2549
4181 2551
4181 2557
4181 2579
4181 2591
4181 2593
4181 2609
4181 2617
4181 2621
4181 2633
4181 2647
4181 2657
4181 2659
4181 2663
4181 2671
4181 2677
4181 2683
4181 2687
4181 2689
4181 2693
4181 2699
4181 2707
4181 2711
4181 2713
4181 2719
4181 2729
4181 2731
4181 2741
4181 2749
4181 2753
4181 2767
4181 2777
4181 2789
4181 2791
4181 2797
4181 2801
4181 2803
4181 2819
4181 2833
4181 2837
4181 2843
4181 2851
4181 2857
4181 2861
4181 2879
4181 2887
4181 2897
4181 2903
4181 2909
4181 2917
4181 2927
4181 2939
4181 2953
4181 2957
4181 2963
4181 2969
4181 2971
4181 2999
4181 3001
4181 3011
4181 3019
4181 3023
4181 3037
4181 3041
4181 3049
4181 3061
4181 3067
4181 3079
4181 3083
4181 3089
4181 3109
4181 3119
4181 3121
4181 3137
4181 3163
4181 3167
4181 3169
4181 3181
4181 3187
4181 3191
4181 3203
4181 3209
4181 3217
4181 3221
4181 3229
4181 3251
4181 3253
4181 3257
4181 3259
4181 3271
4181 3299
4181 3301
4181 3307
4181 3313
4181 3319
4181 3323
4181 3329
4181 3331
4181 3343
4181 3347
4181 3359
4181 3361
4181 3371
4181 3373
4181 3389
4181 3391
4181 3407
4181 3413
4181 3433
4181 3449
4181 3457
4181 3461
4181 3463
4181 3467
4181 3469
4181 3491
4181 3499
4181 3511
4181 3517
4181 3527
4181 3529
4181 3533
4181 3539
4181 3541
4181 3547
4181 3557
4181 3559
4181 3571
4181 3581
4181 3583
4181 3593
4181 3607
4181 3613
4181 3617
4181 3623
4181 3631
4181 3637
4181 3643
4181 3659
4181 3671
4181 3673
4181 3677
4181 3691
4181 3697
4181 3701
4181 3709
4181 3719
4181 3727
4181 3733
4181 3739
4181 3761
4181 3767
4181 3769
4181 3779
4181 3793
4181 3797
4181 3803
4181 3821
4181 3823
4181 3833
4181 3847
4181 3851
4181 3853
4181 3863
4181 3877
4181 3881
4181 3889
4181 3907
4181 3911
4181 3917
4181 3919
4181 3923
4181 3929
4181 3931
4181 3943
4181 3947
4181 3967
4181 3989
4181 4001
4181 4003
4181 4007
4181 4013
4181 4019
4181 4021
4181 4027
4181 4049
4181 4051
4181 4057
4181 4073
4181 4079
4181 4091
4181 4093
4181 4099
4181 4111
4181 4127
4181 4129
4181 4133
4181 4139
4181 4153
4181 4157
4181 4159
4181 4177
4181 0
ret = 5468808766 ; 468808766
#!/usr/bin/env perl6
# The Expat License
#
# Copyright (c) 2018, Shlomi Fish
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
my $BASE = 1000000000;
sub calc_S(int $n, $token='foo')
{
my $out = run('./primesieve', $n, '-p1', :out).out.slurp-rest;
my int @primes = map { Int($_) }, split /\n/, $out;
return 0 if !@primes;
my @d;
my $r = 1;
for 0..$n -> $m
{
if $m +& 1
{
@d.push(0);
}
else
{
@d.push($r);
$r = (($r +< 1) % $BASE);
}
}
@primes.shift;
while @primes {
my $p = @primes.shift;
say $token, ' ', $p;
for $p .. $n -> $m {
@d.ASSIGN-POS($m, (@d.AT-POS($m) + $p * @d.AT-POS($m-$p)) % $BASE);
}
}
return @d;
}
my $a = 1;
my $b = 1;
my @Fk;
for 2.. 19 -> $i
{
@Fk.push($b);
($a, $b) = ($b, $a+$b);
}
say $a;
my @ret := calc_S($a, "$a");
# assert ret[8] == 49
# assert ret[1] == 0
# assert ret[2] == 2
# assert ret[3] == 3
# assert ret[5] == 11
my $r = sum(@ret[@Fk]);
printf("ret = %d ; %09d", $r, $r % $BASE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment