-
Notifications
You must be signed in to change notification settings - Fork 1
/
project-euler-1.html
1226 lines (1143 loc) · 142 KB
/
project-euler-1.html
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
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.13.1: http://docutils.sourceforge.net/" />
<title>Project Euler 题解第一页</title>
<link rel="stylesheet" href="style.css" type="text/css" />
<link rel="stylesheet" href="math.css" type="text/css" />
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div class="document" id="project-euler">
<div id="top"></div>
<h1 class="title">Project Euler 题解第一页</h1>
<p>用 <a class="reference external" href="http://julialang.org/">Julia</a> 做 <a class="reference external" href="http://projecteuler.net">Project Euler</a> 上的题目简直比作弊还简单 ...</p>
<div class="section" id="id1">
<h1>1</h1>
<p>找出 1000 以下 3 或 5 的倍数的和</p>
<p>不用讲解 - -</p>
<pre class="code julia literal-block">
<span class="n">println</span><span class="p">(</span><span class="n">sum</span><span class="p">(</span><span class="n">filter</span><span class="p">(</span><span class="n">i</span><span class="o">-></span><span class="n">i</span><span class="o">%</span><span class="mi">3</span><span class="o">==</span><span class="mi">0</span> <span class="o">||</span> <span class="n">i</span><span class="o">%</span><span class="mi">5</span><span class="o">==</span><span class="mi">0</span><span class="p">,</span> <span class="p">[</span><span class="mi">3</span><span class="o">:</span><span class="mi">999</span><span class="p">])))</span>
</pre>
</div>
<div class="section" id="id2">
<h1>2</h1>
<p>斐波拉切数列的偶数项和</p>
<p>还是不用讲解 - -</p>
<pre class="code julia literal-block">
<span class="n">a</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">b</span> <span class="o">=</span> <span class="mi">2</span>
<span class="n">sum</span> <span class="o">=</span> <span class="mi">2</span>
<span class="k">while</span> <span class="kc">true</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
<span class="k">if</span> <span class="n">c</span><span class="o">></span><span class="mi">4000000</span> <span class="n">then</span>
<span class="k">break</span>
<span class="k">end</span>
<span class="k">if</span> <span class="n">c</span><span class="o">%</span><span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span> <span class="n">then</span>
<span class="n">sum</span> <span class="o">=</span> <span class="n">sum</span> <span class="o">+</span> <span class="n">c</span>
<span class="k">end</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">b</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">c</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">sum</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id3">
<h1>3</h1>
<p>600851475143 的最大质数因子</p>
<p>对于 julia 这么一种 IMBA 的语言 …… 还是不用讲解</p>
<pre class="code julia literal-block">
<span class="n">v</span> <span class="o">=</span> <span class="mi">600851475143</span>
<span class="n">println</span><span class="p">(</span><span class="n">maximum</span><span class="p">([</span><span class="n">f</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">f</span> <span class="kp">in</span> <span class="n">factor</span><span class="p">(</span><span class="n">v</span><span class="p">)]))</span>
</pre>
</div>
<div class="section" id="id4">
<h1>4</h1>
<p>找出三位数相乘组成的最大回文数</p>
<p>依然不用讲解</p>
<pre class="code julia literal-block">
<span class="n">isPalindrome</span><span class="p">(</span><span class="n">s</span><span class="o">::</span><span class="n">String</span><span class="p">)</span> <span class="o">=</span>
<span class="k">if</span> <span class="n">length</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="o">></span><span class="mi">2</span>
<span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">==</span><span class="n">s</span><span class="p">[</span><span class="k">end</span><span class="p">]</span> <span class="o">&&</span> <span class="n">isPalindrome</span><span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="mi">2</span><span class="o">:</span><span class="k">end</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="k">else</span>
<span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="n">s</span><span class="p">[</span><span class="k">end</span><span class="p">]</span>
<span class="k">end</span>
<span class="n">isPalindrome</span><span class="p">(</span><span class="n">i</span><span class="o">::</span><span class="kt">Int</span><span class="p">)</span> <span class="o">=</span> <span class="n">isPalindrome</span><span class="p">(</span><span class="n">string</span><span class="p">(</span><span class="n">i</span><span class="p">))</span>
<span class="n">println</span><span class="p">(</span><span class="n">maximum</span><span class="p">(</span><span class="n">filter</span><span class="p">(</span><span class="n">isPalindrome</span><span class="p">,</span> <span class="p">[</span><span class="n">a</span><span class="o">*</span><span class="n">b</span> <span class="k">for</span> <span class="n">a</span> <span class="kp">in</span> <span class="mi">100</span><span class="o">:</span><span class="mi">999</span><span class="p">,</span> <span class="n">b</span> <span class="kp">in</span> <span class="mi">100</span><span class="o">:</span><span class="mi">999</span><span class="p">])))</span>
</pre>
</div>
<div class="section" id="id5">
<h1>5</h1>
<p>1 .. 20 的最小公倍数</p>
<pre class="code julia literal-block">
<span class="n">println</span><span class="p">(</span><span class="n">lcm</span><span class="p">([</span><span class="mi">1</span><span class="o">:</span><span class="mi">20</span><span class="p">]</span><span class="o">...</span><span class="p">))</span>
</pre>
</div>
<div class="section" id="id6">
<h1>6</h1>
<p>1 .. 100 和的平方与平方和的差</p>
<pre class="code julia literal-block">
<span class="n">println</span><span class="p">(</span><span class="n">sum</span><span class="p">([</span><span class="mi">1</span><span class="o">:</span><span class="mi">100</span><span class="p">])</span><span class="o">^</span><span class="mi">2</span> <span class="o">-</span> <span class="n">sum</span><span class="p">(</span><span class="n">map</span><span class="p">(</span><span class="n">x</span><span class="o">-></span><span class="n">x</span><span class="o">^</span><span class="mi">2</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="o">:</span><span class="mi">100</span><span class="p">])))</span>
</pre>
</div>
<div class="section" id="id7">
<h1>7</h1>
<p>第 10001 个质数是什么呢</p>
<pre class="code julia literal-block">
<span class="n">println</span><span class="p">(</span><span class="n">primes</span><span class="p">(</span><span class="mi">500000</span><span class="p">)[</span><span class="mi">10001</span><span class="p">])</span>
</pre>
</div>
<div class="section" id="id8">
<h1>8</h1>
<p>在那一串数字中找到最大的五个连续的数字的积 ……</p>
<pre class="code julia literal-block">
<span class="n">s</span> <span class="o">=</span> <span class="n">replace</span><span class="p">(</span>
<span class="s">"
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
"</span>
<span class="p">,</span> <span class="sr">r"\s"</span><span class="p">,</span> <span class="s">""</span><span class="p">)</span>
<span class="n">v</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">5</span><span class="o">:</span><span class="n">length</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
<span class="n">v</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span> <span class="n">v</span><span class="p">,</span> <span class="n">reduce</span><span class="p">((</span><span class="n">x</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span><span class="o">-></span><span class="n">x</span><span class="o">*</span><span class="p">(</span><span class="n">c</span><span class="o">-</span><span class="sc">'0'</span><span class="p">),</span> <span class="mi">1</span><span class="p">,</span> <span class="n">s</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">4</span><span class="o">:</span><span class="n">i</span><span class="p">])</span> <span class="p">)</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id9">
<h1>9</h1>
<p>找到满足 <span class="formula"><i>a</i><sup>2</sup> + <i>b</i><sup>2</sup> = <i>c</i><sup>2</sup></span> 且 <span class="formula"><i>a</i> + <i>b</i> + <i>c</i> = 1000</span> 的 <span class="formula"><i>a</i>, <i>b</i>, <i>c</i></span></p>
<pre class="code julia literal-block">
<span class="k">for</span> <span class="n">a</span> <span class="kp">in</span> <span class="p">[</span><span class="mi">1</span><span class="o">:</span><span class="mi">999</span><span class="p">]</span>
<span class="k">for</span> <span class="n">b</span> <span class="kp">in</span> <span class="p">[</span><span class="n">a</span><span class="o">:</span><span class="mi">999</span><span class="p">]</span>
<span class="k">if</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span> <span class="o">></span> <span class="mi">1000</span>
<span class="k">continue</span>
<span class="k">end</span>
<span class="k">for</span> <span class="n">c</span> <span class="kp">in</span> <span class="p">[</span><span class="n">b</span><span class="o">:</span><span class="mi">999</span><span class="p">]</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="o">+</span> <span class="n">c</span>
<span class="k">if</span> <span class="n">s</span> <span class="o">></span> <span class="mi">1000</span>
<span class="k">break</span>
<span class="k">end</span>
<span class="k">if</span> <span class="n">a</span><span class="o">^</span><span class="mi">2</span> <span class="o">+</span> <span class="n">b</span><span class="o">^</span><span class="mi">2</span> <span class="o">==</span> <span class="n">c</span><span class="o">^</span><span class="mi">2</span> <span class="o">&&</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="o">+</span> <span class="n">c</span> <span class="o">==</span> <span class="mi">1000</span>
<span class="n">println</span><span class="p">(</span><span class="s">"</span><span class="si">$a</span><span class="s"> * </span><span class="si">$b</span><span class="s"> * </span><span class="si">$c</span><span class="s"> = </span><span class="si">$</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="o">*</span><span class="n">c</span><span class="p">)</span><span class="s">"</span><span class="p">)</span>
<span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</pre>
</div>
<div class="section" id="id10">
<h1>10</h1>
<p>求 2000000 以下的质数的和</p>
<pre class="code julia literal-block">
<span class="n">println</span><span class="p">(</span><span class="n">sum</span><span class="p">(</span><span class="n">primes</span><span class="p">(</span><span class="mi">2000000</span><span class="o">-</span><span class="mi">1</span><span class="p">)))</span>
</pre>
</div>
<div class="section" id="id11">
<h1>11</h1>
<p>求一个矩阵中沿某一方向连续的四个数的最大乘积</p>
<pre class="code julia literal-block">
<span class="n">m</span> <span class="o">=</span> <span class="p">[</span>
<span class="mi">08</span> <span class="mi">02</span> <span class="mi">22</span> <span class="mi">97</span> <span class="mi">38</span> <span class="mi">15</span> <span class="mi">00</span> <span class="mi">40</span> <span class="mi">00</span> <span class="mi">75</span> <span class="mi">04</span> <span class="mi">05</span> <span class="mi">07</span> <span class="mi">78</span> <span class="mi">52</span> <span class="mi">12</span> <span class="mi">50</span> <span class="mi">77</span> <span class="mi">91</span> <span class="mi">08</span>
<span class="mi">49</span> <span class="mi">49</span> <span class="mi">99</span> <span class="mi">40</span> <span class="mi">17</span> <span class="mi">81</span> <span class="mi">18</span> <span class="mi">57</span> <span class="mi">60</span> <span class="mi">87</span> <span class="mi">17</span> <span class="mi">40</span> <span class="mi">98</span> <span class="mi">43</span> <span class="mi">69</span> <span class="mi">48</span> <span class="mi">04</span> <span class="mi">56</span> <span class="mi">62</span> <span class="mi">00</span>
<span class="mi">81</span> <span class="mi">49</span> <span class="mi">31</span> <span class="mi">73</span> <span class="mi">55</span> <span class="mi">79</span> <span class="mi">14</span> <span class="mi">29</span> <span class="mi">93</span> <span class="mi">71</span> <span class="mi">40</span> <span class="mi">67</span> <span class="mi">53</span> <span class="mi">88</span> <span class="mi">30</span> <span class="mi">03</span> <span class="mi">49</span> <span class="mi">13</span> <span class="mi">36</span> <span class="mi">65</span>
<span class="mi">52</span> <span class="mi">70</span> <span class="mi">95</span> <span class="mi">23</span> <span class="mi">04</span> <span class="mi">60</span> <span class="mi">11</span> <span class="mi">42</span> <span class="mi">69</span> <span class="mi">24</span> <span class="mi">68</span> <span class="mi">56</span> <span class="mi">01</span> <span class="mi">32</span> <span class="mi">56</span> <span class="mi">71</span> <span class="mi">37</span> <span class="mi">02</span> <span class="mi">36</span> <span class="mi">91</span>
<span class="mi">22</span> <span class="mi">31</span> <span class="mi">16</span> <span class="mi">71</span> <span class="mi">51</span> <span class="mi">67</span> <span class="mi">63</span> <span class="mi">89</span> <span class="mi">41</span> <span class="mi">92</span> <span class="mi">36</span> <span class="mi">54</span> <span class="mi">22</span> <span class="mi">40</span> <span class="mi">40</span> <span class="mi">28</span> <span class="mi">66</span> <span class="mi">33</span> <span class="mi">13</span> <span class="mi">80</span>
<span class="mi">24</span> <span class="mi">47</span> <span class="mi">32</span> <span class="mi">60</span> <span class="mi">99</span> <span class="mi">03</span> <span class="mi">45</span> <span class="mi">02</span> <span class="mi">44</span> <span class="mi">75</span> <span class="mi">33</span> <span class="mi">53</span> <span class="mi">78</span> <span class="mi">36</span> <span class="mi">84</span> <span class="mi">20</span> <span class="mi">35</span> <span class="mi">17</span> <span class="mi">12</span> <span class="mi">50</span>
<span class="mi">32</span> <span class="mi">98</span> <span class="mi">81</span> <span class="mi">28</span> <span class="mi">64</span> <span class="mi">23</span> <span class="mi">67</span> <span class="mi">10</span> <span class="mi">26</span> <span class="mi">38</span> <span class="mi">40</span> <span class="mi">67</span> <span class="mi">59</span> <span class="mi">54</span> <span class="mi">70</span> <span class="mi">66</span> <span class="mi">18</span> <span class="mi">38</span> <span class="mi">64</span> <span class="mi">70</span>
<span class="mi">67</span> <span class="mi">26</span> <span class="mi">20</span> <span class="mi">68</span> <span class="mi">02</span> <span class="mi">62</span> <span class="mi">12</span> <span class="mi">20</span> <span class="mi">95</span> <span class="mi">63</span> <span class="mi">94</span> <span class="mi">39</span> <span class="mi">63</span> <span class="mi">08</span> <span class="mi">40</span> <span class="mi">91</span> <span class="mi">66</span> <span class="mi">49</span> <span class="mi">94</span> <span class="mi">21</span>
<span class="mi">24</span> <span class="mi">55</span> <span class="mi">58</span> <span class="mi">05</span> <span class="mi">66</span> <span class="mi">73</span> <span class="mi">99</span> <span class="mi">26</span> <span class="mi">97</span> <span class="mi">17</span> <span class="mi">78</span> <span class="mi">78</span> <span class="mi">96</span> <span class="mi">83</span> <span class="mi">14</span> <span class="mi">88</span> <span class="mi">34</span> <span class="mi">89</span> <span class="mi">63</span> <span class="mi">72</span>
<span class="mi">21</span> <span class="mi">36</span> <span class="mi">23</span> <span class="mi">09</span> <span class="mi">75</span> <span class="mi">00</span> <span class="mi">76</span> <span class="mi">44</span> <span class="mi">20</span> <span class="mi">45</span> <span class="mi">35</span> <span class="mi">14</span> <span class="mi">00</span> <span class="mi">61</span> <span class="mi">33</span> <span class="mi">97</span> <span class="mi">34</span> <span class="mi">31</span> <span class="mi">33</span> <span class="mi">95</span>
<span class="mi">78</span> <span class="mi">17</span> <span class="mi">53</span> <span class="mi">28</span> <span class="mi">22</span> <span class="mi">75</span> <span class="mi">31</span> <span class="mi">67</span> <span class="mi">15</span> <span class="mi">94</span> <span class="mi">03</span> <span class="mi">80</span> <span class="mi">04</span> <span class="mi">62</span> <span class="mi">16</span> <span class="mi">14</span> <span class="mi">09</span> <span class="mi">53</span> <span class="mi">56</span> <span class="mi">92</span>
<span class="mi">16</span> <span class="mi">39</span> <span class="mi">05</span> <span class="mi">42</span> <span class="mi">96</span> <span class="mi">35</span> <span class="mi">31</span> <span class="mi">47</span> <span class="mi">55</span> <span class="mi">58</span> <span class="mi">88</span> <span class="mi">24</span> <span class="mi">00</span> <span class="mi">17</span> <span class="mi">54</span> <span class="mi">24</span> <span class="mi">36</span> <span class="mi">29</span> <span class="mi">85</span> <span class="mi">57</span>
<span class="mi">86</span> <span class="mi">56</span> <span class="mi">00</span> <span class="mi">48</span> <span class="mi">35</span> <span class="mi">71</span> <span class="mi">89</span> <span class="mi">07</span> <span class="mi">05</span> <span class="mi">44</span> <span class="mi">44</span> <span class="mi">37</span> <span class="mi">44</span> <span class="mi">60</span> <span class="mi">21</span> <span class="mi">58</span> <span class="mi">51</span> <span class="mi">54</span> <span class="mi">17</span> <span class="mi">58</span>
<span class="mi">19</span> <span class="mi">80</span> <span class="mi">81</span> <span class="mi">68</span> <span class="mi">05</span> <span class="mi">94</span> <span class="mi">47</span> <span class="mi">69</span> <span class="mi">28</span> <span class="mi">73</span> <span class="mi">92</span> <span class="mi">13</span> <span class="mi">86</span> <span class="mi">52</span> <span class="mi">17</span> <span class="mi">77</span> <span class="mi">04</span> <span class="mi">89</span> <span class="mi">55</span> <span class="mi">40</span>
<span class="mi">04</span> <span class="mi">52</span> <span class="mi">08</span> <span class="mi">83</span> <span class="mi">97</span> <span class="mi">35</span> <span class="mi">99</span> <span class="mi">16</span> <span class="mi">07</span> <span class="mi">97</span> <span class="mi">57</span> <span class="mi">32</span> <span class="mi">16</span> <span class="mi">26</span> <span class="mi">26</span> <span class="mi">79</span> <span class="mi">33</span> <span class="mi">27</span> <span class="mi">98</span> <span class="mi">66</span>
<span class="mi">88</span> <span class="mi">36</span> <span class="mi">68</span> <span class="mi">87</span> <span class="mi">57</span> <span class="mi">62</span> <span class="mi">20</span> <span class="mi">72</span> <span class="mi">03</span> <span class="mi">46</span> <span class="mi">33</span> <span class="mi">67</span> <span class="mi">46</span> <span class="mi">55</span> <span class="mi">12</span> <span class="mi">32</span> <span class="mi">63</span> <span class="mi">93</span> <span class="mi">53</span> <span class="mi">69</span>
<span class="mi">04</span> <span class="mi">42</span> <span class="mi">16</span> <span class="mi">73</span> <span class="mi">38</span> <span class="mi">25</span> <span class="mi">39</span> <span class="mi">11</span> <span class="mi">24</span> <span class="mi">94</span> <span class="mi">72</span> <span class="mi">18</span> <span class="mi">08</span> <span class="mi">46</span> <span class="mi">29</span> <span class="mi">32</span> <span class="mi">40</span> <span class="mi">62</span> <span class="mi">76</span> <span class="mi">36</span>
<span class="mi">20</span> <span class="mi">69</span> <span class="mi">36</span> <span class="mi">41</span> <span class="mi">72</span> <span class="mi">30</span> <span class="mi">23</span> <span class="mi">88</span> <span class="mi">34</span> <span class="mi">62</span> <span class="mi">99</span> <span class="mi">69</span> <span class="mi">82</span> <span class="mi">67</span> <span class="mi">59</span> <span class="mi">85</span> <span class="mi">74</span> <span class="mi">04</span> <span class="mi">36</span> <span class="mi">16</span>
<span class="mi">20</span> <span class="mi">73</span> <span class="mi">35</span> <span class="mi">29</span> <span class="mi">78</span> <span class="mi">31</span> <span class="mi">90</span> <span class="mi">01</span> <span class="mi">74</span> <span class="mi">31</span> <span class="mi">49</span> <span class="mi">71</span> <span class="mi">48</span> <span class="mi">86</span> <span class="mi">81</span> <span class="mi">16</span> <span class="mi">23</span> <span class="mi">57</span> <span class="mi">05</span> <span class="mi">54</span>
<span class="mi">01</span> <span class="mi">70</span> <span class="mi">54</span> <span class="mi">71</span> <span class="mi">83</span> <span class="mi">51</span> <span class="mi">54</span> <span class="mi">69</span> <span class="mi">16</span> <span class="mi">92</span> <span class="mi">33</span> <span class="mi">48</span> <span class="mi">61</span> <span class="mi">43</span> <span class="mi">52</span> <span class="mi">01</span> <span class="mi">89</span> <span class="mi">19</span> <span class="mi">67</span> <span class="mi">48</span>
<span class="p">]</span>
<span class="n">d</span> <span class="o">=</span> <span class="mi">20</span> <span class="c"># dimension</span>
<span class="n">v</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">r</span> <span class="kp">in</span> <span class="p">[</span><span class="mi">4</span><span class="o">:</span><span class="n">d</span><span class="p">]</span>
<span class="k">for</span> <span class="n">c</span> <span class="kp">in</span> <span class="p">[</span><span class="mi">4</span><span class="o">:</span><span class="n">d</span><span class="p">]</span>
<span class="n">v</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">v</span><span class="p">,</span>
<span class="n">prod</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="n">r</span> <span class="p">,</span> <span class="n">c</span><span class="o">-</span><span class="mi">3</span><span class="o">:</span><span class="n">c</span><span class="p">]),</span>
<span class="n">prod</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="n">r</span><span class="o">-</span><span class="mi">3</span><span class="o">:</span><span class="n">r</span> <span class="p">,</span> <span class="n">c</span><span class="p">]),</span>
<span class="n">m</span><span class="p">[</span><span class="n">r</span> <span class="p">,</span> <span class="n">c</span><span class="p">]</span> <span class="o">*</span> <span class="n">m</span><span class="p">[</span><span class="n">r</span><span class="o">-</span><span class="mi">1</span> <span class="p">,</span> <span class="n">c</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="n">m</span><span class="p">[</span><span class="n">r</span><span class="o">-</span><span class="mi">2</span> <span class="p">,</span> <span class="n">c</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span> <span class="o">*</span> <span class="n">m</span><span class="p">[</span><span class="n">r</span><span class="o">-</span><span class="mi">3</span> <span class="p">,</span> <span class="n">c</span><span class="o">-</span><span class="mi">3</span><span class="p">],</span>
<span class="n">m</span><span class="p">[</span><span class="n">r</span> <span class="p">,</span> <span class="n">c</span><span class="o">-</span><span class="mi">3</span><span class="p">]</span> <span class="o">*</span> <span class="n">m</span><span class="p">[</span><span class="n">r</span><span class="o">-</span><span class="mi">1</span> <span class="p">,</span> <span class="n">c</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span> <span class="o">*</span> <span class="n">m</span><span class="p">[</span><span class="n">r</span><span class="o">-</span><span class="mi">2</span> <span class="p">,</span> <span class="n">c</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="n">m</span><span class="p">[</span><span class="n">r</span><span class="o">-</span><span class="mi">3</span> <span class="p">,</span> <span class="n">c</span><span class="p">]</span>
<span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id12">
<h1>12</h1>
<p>求因数的数量 // 即质因数的组合方式个数</p>
<p>精髓在这一句 <tt class="docutils literal"><span class="pre">reduce((a,</span> <span class="pre">f)->a</span> * <span class="pre">(f[2]+1),</span> 1, factor(t))</tt></p>
<p><tt class="docutils literal">factor(x)</tt> 返回 <tt class="docutils literal">x</tt> 的质因数及其次数列表</p>
<pre class="code julia literal-block">
<span class="n">t</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">100000</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">t</span><span class="o">+</span><span class="n">i</span>
<span class="k">if</span> <span class="n">reduce</span><span class="p">((</span><span class="n">a</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span><span class="o">-></span><span class="n">a</span> <span class="o">*</span> <span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">+</span><span class="mi">1</span><span class="p">),</span> <span class="mi">1</span><span class="p">,</span> <span class="n">factor</span><span class="p">(</span><span class="n">t</span><span class="p">))</span><span class="o">></span><span class="mi">500</span>
<span class="n">println</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
<span class="k">break</span>
<span class="k">end</span>
<span class="k">end</span>
</pre>
</div>
<div class="section" id="id13">
<h1>13</h1>
<p>求一串大数的和</p>
<pre class="code julia literal-block">
<span class="n">nums</span> <span class="o">=</span> <span class="n">map</span><span class="p">(</span><span class="kt">BigInt</span><span class="p">,[</span>
<span class="s">"37107287533902102798797998220837590246510135740250"</span>
<span class="o">...</span>
<span class="s">"53503534226472524250874054075591789781264330331690"</span>
<span class="p">])</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">string</span><span class="p">(</span><span class="n">sum</span><span class="p">(</span><span class="n">nums</span><span class="p">))</span>
<span class="n">println</span><span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="o">:</span><span class="mi">10</span><span class="p">])</span>
</pre>
</div>
<div class="section" id="id14">
<h1>14</h1>
<p>求使得 3n+1 数列最长的起点</p>
<p>——本该用 DP 做的 …… 不过既然直接算时间也能接受 …… 那就不管了</p>
<pre class="code julia literal-block">
<span class="k">function</span> <span class="n">chainlen</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="k">function</span> <span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="k">if</span> <span class="n">x</span><span class="o">%</span><span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span>
<span class="k">return</span> <span class="n">x</span><span class="o">/</span><span class="mi">2</span>
<span class="k">else</span>
<span class="k">return</span> <span class="mi">3</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">l</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">while</span> <span class="kc">true</span>
<span class="n">l</span> <span class="o">=</span> <span class="n">l</span><span class="o">+</span><span class="mi">1</span>
<span class="k">if</span> <span class="n">n</span><span class="o">==</span><span class="mi">1</span>
<span class="k">break</span>
<span class="k">end</span>
<span class="n">n</span> <span class="o">=</span> <span class="n">f</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">return</span> <span class="n">l</span>
<span class="k">end</span>
<span class="n">n</span><span class="p">,</span> <span class="n">l</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">3</span><span class="o">:</span><span class="mi">1000000</span><span class="o">-</span><span class="mi">1</span>
<span class="n">cl</span> <span class="o">=</span> <span class="n">chainlen</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="k">if</span> <span class="n">cl</span><span class="o">></span><span class="n">l</span>
<span class="n">n</span><span class="p">,</span> <span class="n">l</span> <span class="o">=</span> <span class="n">i</span><span class="p">,</span> <span class="n">cl</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id15">
<h1>15</h1>
<p>求在 20x20 的格子上从一个角走到对角不走回头路的路径个数</p>
<p>从一行 n 列开始递推 n 行 n 列的情况就很明确了</p>
<p>需要留意的是 20x20 的格子有 21x21 行列 - -|||</p>
<pre class="code julia literal-block">
<span class="n">row</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span> <span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">21</span><span class="p">]</span>
<span class="n">nextrow</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span> <span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">21</span><span class="p">]</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">20</span>
<span class="k">for</span> <span class="n">j</span> <span class="kp">in</span> <span class="mi">2</span><span class="o">:</span><span class="mi">21</span>
<span class="n">nextrow</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">nextrow</span><span class="p">[</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">row</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
<span class="k">end</span>
<span class="n">row</span><span class="p">,</span> <span class="n">nextrow</span> <span class="o">=</span> <span class="n">nextrow</span><span class="p">,</span> <span class="n">row</span>
<span class="c"># println(row)</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span> <span class="n">row</span><span class="p">[</span><span class="mi">21</span><span class="p">]</span> <span class="p">)</span>
</pre>
</div>
<div class="section" id="id16">
<h1>16</h1>
<p>求 <span class="formula">2<sup>1000</sup></span> 的每位数字的和</p>
<pre class="code julia literal-block">
<span class="n">println</span><span class="p">(</span><span class="n">reduce</span><span class="p">((</span><span class="n">a</span><span class="p">,</span><span class="n">c</span><span class="p">)</span><span class="o">-></span><span class="n">a</span><span class="o">+</span><span class="p">(</span><span class="n">c</span><span class="o">-</span><span class="sc">'0'</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="n">string</span><span class="p">(</span><span class="kt">BigInt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">^</span><span class="mi">1000</span><span class="p">)))</span>
</pre>
</div>
<div class="section" id="id17">
<h1>17</h1>
<p>求用英语写出 1 .. 1000 每一个数字,一共有多少个字母</p>
<pre class="code julia literal-block">
<span class="n">dic</span> <span class="o">=</span> <span class="p">[</span>
<span class="mi">1</span><span class="o">=></span><span class="s">"one"</span><span class="p">,</span>
<span class="mi">2</span><span class="o">=></span><span class="s">"two"</span><span class="p">,</span>
<span class="mi">3</span><span class="o">=></span><span class="s">"three"</span><span class="p">,</span>
<span class="mi">4</span><span class="o">=></span><span class="s">"four"</span><span class="p">,</span>
<span class="mi">5</span><span class="o">=></span><span class="s">"five"</span><span class="p">,</span>
<span class="mi">6</span><span class="o">=></span><span class="s">"six"</span><span class="p">,</span>
<span class="mi">7</span><span class="o">=></span><span class="s">"seven"</span><span class="p">,</span>
<span class="mi">8</span><span class="o">=></span><span class="s">"eight"</span><span class="p">,</span>
<span class="mi">9</span><span class="o">=></span><span class="s">"nine"</span><span class="p">,</span>
<span class="mi">10</span><span class="o">=></span><span class="s">"ten"</span><span class="p">,</span>
<span class="mi">11</span><span class="o">=></span><span class="s">"eleven"</span><span class="p">,</span>
<span class="mi">12</span><span class="o">=></span><span class="s">"twelve"</span><span class="p">,</span>
<span class="mi">13</span><span class="o">=></span><span class="s">"thirteen"</span><span class="p">,</span>
<span class="mi">14</span><span class="o">=></span><span class="s">"fourteen"</span><span class="p">,</span>
<span class="mi">15</span><span class="o">=></span><span class="s">"fifteen"</span><span class="p">,</span>
<span class="mi">16</span><span class="o">=></span><span class="s">"sixteen"</span><span class="p">,</span>
<span class="mi">17</span><span class="o">=></span><span class="s">"seventeen"</span><span class="p">,</span>
<span class="mi">18</span><span class="o">=></span><span class="s">"eighteen"</span><span class="p">,</span>
<span class="mi">19</span><span class="o">=></span><span class="s">"nineteen"</span><span class="p">,</span>
<span class="mi">20</span><span class="o">=></span><span class="s">"twenty"</span><span class="p">,</span>
<span class="mi">30</span><span class="o">=></span><span class="s">"thirty"</span><span class="p">,</span>
<span class="mi">40</span><span class="o">=></span><span class="s">"forty"</span><span class="p">,</span>
<span class="mi">50</span><span class="o">=></span><span class="s">"fifty"</span><span class="p">,</span>
<span class="mi">60</span><span class="o">=></span><span class="s">"sixty"</span><span class="p">,</span>
<span class="mi">70</span><span class="o">=></span><span class="s">"seventy"</span><span class="p">,</span>
<span class="mi">80</span><span class="o">=></span><span class="s">"eighty"</span><span class="p">,</span>
<span class="mi">90</span><span class="o">=></span><span class="s">"ninety"</span><span class="p">,</span>
<span class="mi">100</span><span class="o">=></span><span class="s">"hundred"</span><span class="p">,</span>
<span class="mi">1000</span><span class="o">=></span><span class="s">"thousand"</span><span class="p">,</span>
<span class="p">]</span>
<span class="o">+</span><span class="p">(</span><span class="n">s1</span><span class="o">::</span><span class="n">String</span><span class="p">,</span> <span class="n">s2</span><span class="o">::</span><span class="n">String</span><span class="p">)</span> <span class="o">=</span> <span class="s">"</span><span class="si">$s1$s2</span><span class="s">"</span>
<span class="k">function</span> <span class="n">toeng</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="n">assert</span><span class="p">(</span><span class="n">n</span><span class="o">></span><span class="mi">0</span> <span class="o">&&</span> <span class="n">n</span><span class="o"><=</span><span class="mi">1000</span><span class="p">)</span>
<span class="k">if</span> <span class="n">n</span><span class="o"><=</span><span class="mi">20</span>
<span class="k">return</span> <span class="n">dic</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>
<span class="k">end</span>
<span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1000</span>
<span class="k">return</span> <span class="s">"one thousand"</span>
<span class="k">end</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">int</span><span class="p">(</span><span class="n">floor</span><span class="p">(</span><span class="n">n</span><span class="o">/</span><span class="mi">100</span><span class="p">))</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">int</span><span class="p">(</span><span class="n">floor</span><span class="p">((</span><span class="n">n</span><span class="o">-</span><span class="n">a</span><span class="o">*</span><span class="mi">100</span><span class="p">)</span><span class="o">/</span><span class="mi">10</span><span class="p">))</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">int</span><span class="p">(</span><span class="n">floor</span><span class="p">((</span><span class="n">n</span><span class="o">-</span><span class="n">a</span><span class="o">*</span><span class="mi">100</span><span class="o">-</span><span class="n">b</span><span class="o">*</span><span class="mi">10</span><span class="p">)))</span>
<span class="n">en</span> <span class="o">=</span> <span class="s">""</span>
<span class="k">if</span> <span class="n">a</span> <span class="o">></span> <span class="mi">0</span>
<span class="n">en</span> <span class="o">=</span> <span class="n">dic</span><span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="o">+</span> <span class="s">" "</span> <span class="o">+</span> <span class="n">dic</span><span class="p">[</span><span class="mi">100</span><span class="p">]</span>
<span class="k">if</span> <span class="n">b</span> <span class="o">></span> <span class="mi">0</span> <span class="o">||</span> <span class="n">c</span> <span class="o">></span> <span class="mi">0</span>
<span class="n">en</span> <span class="o">=</span> <span class="n">en</span> <span class="o">+</span> <span class="s">" and "</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">if</span> <span class="n">b</span> <span class="o">==</span> <span class="mi">1</span> <span class="o">||</span> <span class="n">b</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">&&</span> <span class="n">c</span> <span class="o">></span> <span class="mi">0</span>
<span class="n">en</span> <span class="o">=</span> <span class="n">en</span> <span class="o">+</span> <span class="n">dic</span><span class="p">[</span><span class="n">b</span><span class="o">*</span><span class="mi">10</span><span class="o">+</span><span class="n">c</span><span class="p">]</span>
<span class="k">elseif</span> <span class="n">b</span> <span class="o">></span> <span class="mi">1</span>
<span class="n">en</span> <span class="o">=</span> <span class="n">en</span> <span class="o">+</span> <span class="n">dic</span><span class="p">[</span><span class="n">b</span><span class="o">*</span><span class="mi">10</span><span class="p">]</span>
<span class="k">if</span> <span class="n">c</span> <span class="o">></span> <span class="mi">0</span>
<span class="n">en</span> <span class="o">=</span> <span class="n">en</span> <span class="o">+</span> <span class="s">"-"</span> <span class="o">+</span> <span class="n">dic</span><span class="p">[</span><span class="n">c</span><span class="p">]</span>
<span class="k">end</span>
<span class="k">elseif</span> <span class="n">c</span><span class="o">></span><span class="mi">0</span>
<span class="n">en</span> <span class="o">=</span> <span class="n">en</span> <span class="o">+</span> <span class="n">dic</span><span class="p">[</span><span class="n">c</span><span class="p">]</span>
<span class="k">end</span>
<span class="k">return</span> <span class="n">en</span>
<span class="k">end</span>
<span class="c"># println(toeng(342))</span>
<span class="n">n</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">1000</span>
<span class="n">n</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span><span class="o">-></span><span class="k">if</span> <span class="n">c</span><span class="o">>=</span><span class="sc">'a'</span> <span class="o">&&</span> <span class="n">c</span><span class="o"><=</span><span class="sc">'z'</span> <span class="n">n</span><span class="o">+</span><span class="mi">1</span> <span class="k">else</span> <span class="n">n</span> <span class="k">end</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">toeng</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="p">)</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id18">
<h1>18</h1>
<p>求从数字三角形顶部走到底,路过的每一位数字相加所能得到的最大值</p>
<p>从底部向上递推就好了</p>
<pre class="code julia literal-block">
<span class="n">m</span> <span class="o">=</span> <span class="p">[</span>
<span class="mi">75</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span>
<span class="mi">95</span> <span class="mi">64</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span>
<span class="mi">17</span> <span class="mi">47</span> <span class="mi">82</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span>
<span class="mi">18</span> <span class="mi">35</span> <span class="mi">87</span> <span class="mi">10</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span>
<span class="mi">20</span> <span class="mi">04</span> <span class="mi">82</span> <span class="mi">47</span> <span class="mi">65</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span>
<span class="mi">19</span> <span class="mi">01</span> <span class="mi">23</span> <span class="mi">75</span> <span class="mi">03</span> <span class="mi">34</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span>
<span class="mi">88</span> <span class="mi">02</span> <span class="mi">77</span> <span class="mi">73</span> <span class="mi">07</span> <span class="mi">63</span> <span class="mi">67</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span>
<span class="mi">99</span> <span class="mi">65</span> <span class="mi">04</span> <span class="mi">28</span> <span class="mi">06</span> <span class="mi">16</span> <span class="mi">70</span> <span class="mi">92</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span>
<span class="mi">41</span> <span class="mi">41</span> <span class="mi">26</span> <span class="mi">56</span> <span class="mi">83</span> <span class="mi">40</span> <span class="mi">80</span> <span class="mi">70</span> <span class="mi">33</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span>
<span class="mi">41</span> <span class="mi">48</span> <span class="mi">72</span> <span class="mi">33</span> <span class="mi">47</span> <span class="mi">32</span> <span class="mi">37</span> <span class="mi">16</span> <span class="mi">94</span> <span class="mi">29</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span>
<span class="mi">53</span> <span class="mi">71</span> <span class="mi">44</span> <span class="mi">65</span> <span class="mi">25</span> <span class="mi">43</span> <span class="mi">91</span> <span class="mi">52</span> <span class="mi">97</span> <span class="mi">51</span> <span class="mi">14</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span>
<span class="mi">70</span> <span class="mi">11</span> <span class="mi">33</span> <span class="mi">28</span> <span class="mi">77</span> <span class="mi">73</span> <span class="mi">17</span> <span class="mi">78</span> <span class="mi">39</span> <span class="mi">68</span> <span class="mi">17</span> <span class="mi">57</span> <span class="mi">00</span> <span class="mi">00</span> <span class="mi">00</span>
<span class="mi">91</span> <span class="mi">71</span> <span class="mi">52</span> <span class="mi">38</span> <span class="mi">17</span> <span class="mi">14</span> <span class="mi">91</span> <span class="mi">43</span> <span class="mi">58</span> <span class="mi">50</span> <span class="mi">27</span> <span class="mi">29</span> <span class="mi">48</span> <span class="mi">00</span> <span class="mi">00</span>
<span class="mi">63</span> <span class="mi">66</span> <span class="mi">04</span> <span class="mi">68</span> <span class="mi">89</span> <span class="mi">53</span> <span class="mi">67</span> <span class="mi">30</span> <span class="mi">73</span> <span class="mi">16</span> <span class="mi">69</span> <span class="mi">87</span> <span class="mi">40</span> <span class="mi">31</span> <span class="mi">00</span>
<span class="mi">04</span> <span class="mi">62</span> <span class="mi">98</span> <span class="mi">27</span> <span class="mi">23</span> <span class="mi">09</span> <span class="mi">70</span> <span class="mi">98</span> <span class="mi">73</span> <span class="mi">93</span> <span class="mi">38</span> <span class="mi">53</span> <span class="mi">60</span> <span class="mi">04</span> <span class="mi">23</span>
<span class="p">]</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">14</span><span class="o">:-</span><span class="mi">1</span><span class="o">:</span><span class="mi">1</span>
<span class="k">for</span> <span class="n">j</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="n">i</span>
<span class="n">m</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span> <span class="o">+=</span> <span class="n">max</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">j</span><span class="p">],</span> <span class="n">m</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">j</span><span class="o">+</span><span class="mi">1</span><span class="p">])</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">])</span>
</pre>
</div>
<div class="section" id="id19">
<h1>19</h1>
<p>求 1901年1月1日 到 2000年12月30日 之间有几个星期天是在月初的</p>
<p>首先呢,Pkg.add("Datetime") // 这真的是作弊好吗!</p>
<pre class="code julia literal-block">
<span class="k">using</span> <span class="n">Datetime</span>
<span class="n">println</span><span class="p">(</span><span class="n">length</span><span class="p">(</span><span class="n">filter</span><span class="p">(</span><span class="n">d</span><span class="o">-></span><span class="n">dayofweek</span><span class="p">(</span><span class="n">d</span><span class="p">)</span><span class="o">==</span><span class="mi">0</span><span class="p">,</span> <span class="n">date</span><span class="p">(</span><span class="mi">1901</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span><span class="o">:</span><span class="n">months</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">:</span><span class="n">date</span><span class="p">(</span><span class="mi">2000</span><span class="p">,</span><span class="mi">12</span><span class="p">,</span><span class="mi">31</span><span class="p">))))</span>
</pre>
</div>
<div class="section" id="id20">
<h1>20</h1>
<p>求 200! 的每位数字之和</p>
<pre class="code julia literal-block">
<span class="n">println</span><span class="p">(</span><span class="n">sum</span><span class="p">(</span><span class="n">map</span><span class="p">(</span><span class="n">c</span><span class="o">-></span><span class="n">c</span><span class="o">-</span><span class="sc">'0'</span><span class="p">,</span> <span class="n">collect</span><span class="p">(</span><span class="n">string</span><span class="p">(</span><span class="n">factorial</span><span class="p">(</span><span class="kt">BigInt</span><span class="p">(</span><span class="mi">100</span><span class="p">)))))))</span>
</pre>
</div>
<div class="section" id="id21">
<h1>21</h1>
<p>求 1000 以下满足 <tt class="docutils literal">a</tt> 的所有因数加起来等于 <tt class="docutils literal">b</tt> 且 <tt class="docutils literal">b</tt> 的所有因数加起来等于 <tt class="docutils literal">a</tt> 的 <tt class="docutils literal">a</tt>、<tt class="docutils literal">b</tt> 之和</p>
<pre class="code julia literal-block">
<span class="k">function</span> <span class="n">factors</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">f</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="k">for</span> <span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="n">n</span><span class="p">)</span> <span class="kp">in</span> <span class="n">factor</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">f</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span> <span class="n">vcat</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span>
<span class="p">[</span><span class="n">f</span> <span class="o">*</span> <span class="n">p</span><span class="o">^</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="n">n</span><span class="p">]</span> <span class="p">)</span>
<span class="k">end</span>
<span class="k">return</span> <span class="n">f</span>
<span class="k">end</span>
<span class="n">d</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="o">=</span> <span class="n">sum</span><span class="p">(</span><span class="n">factors</span><span class="p">(</span><span class="n">n</span><span class="p">))</span> <span class="o">-</span> <span class="n">n</span>
<span class="n">s</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">9999</span>
<span class="n">v</span> <span class="o">=</span> <span class="n">d</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="k">if</span> <span class="mi">0</span><span class="o"><</span><span class="n">v</span><span class="o"><=</span><span class="mi">9999</span> <span class="o">&&</span> <span class="n">i!</span><span class="o">=</span><span class="n">v</span> <span class="o">&&</span> <span class="n">i</span><span class="o">==</span><span class="n">d</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
<span class="n">s</span> <span class="o">+=</span> <span class="n">i</span> <span class="c"># v will be calculated later</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id22">
<h1>22</h1>
<p>求一个名字列表里面(每个名字的(字母序之和)与(它在列表中的字典序)的积)的和</p>
<pre class="code julia literal-block">
<span class="n">names</span><span class="o">=</span><span class="p">[</span> <span class="s">"MARY"</span><span class="p">,</span> <span class="o">...</span> <span class="p">,</span><span class="s">"ALONSO"</span><span class="p">]</span>
<span class="n">sort!</span><span class="p">(</span><span class="n">names</span><span class="p">)</span>
<span class="n">av</span><span class="p">(</span><span class="n">str</span><span class="p">)</span> <span class="o">=</span> <span class="n">sum</span><span class="p">(</span><span class="n">map</span><span class="p">(</span><span class="n">c</span><span class="o">-></span><span class="n">c</span><span class="o">-</span><span class="sc">'A'</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">collect</span><span class="p">(</span><span class="n">str</span><span class="p">)))</span>
<span class="n">assert</span><span class="p">(</span><span class="n">names</span><span class="p">[</span><span class="mi">938</span><span class="p">]</span><span class="o">==</span><span class="s">"COLIN"</span><span class="p">)</span>
<span class="n">s</span> <span class="o">=</span> <span class="kt">BigInt</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="n">length</span><span class="p">(</span><span class="n">names</span><span class="p">)</span>
<span class="n">s</span> <span class="o">+=</span> <span class="n">i</span> <span class="o">*</span> <span class="n">av</span><span class="p">(</span><span class="n">names</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id23">
<h1>23</h1>
<p>求所有(不能用两个(因子之和大于本身的数)之和表示的数字)的和</p>
<pre class="code julia literal-block">
<span class="k">function</span> <span class="n">factors</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">f</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="k">for</span> <span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="n">n</span><span class="p">)</span> <span class="kp">in</span> <span class="n">factor</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">f</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span> <span class="n">vcat</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span>
<span class="p">[</span><span class="n">f</span> <span class="o">*</span> <span class="n">p</span><span class="o">^</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="n">n</span><span class="p">]</span> <span class="p">)</span>
<span class="k">end</span>
<span class="k">return</span> <span class="n">f</span>
<span class="k">end</span>
<span class="n">d</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="o">=</span> <span class="n">sum</span><span class="p">(</span><span class="n">factors</span><span class="p">(</span><span class="n">n</span><span class="p">))</span> <span class="o">-</span> <span class="n">n</span>
<span class="n">isabundant</span> <span class="o">=</span> <span class="p">[</span><span class="kc">false</span> <span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">28123</span><span class="p">]</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">28123</span>
<span class="k">if</span> <span class="n">d</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">></span><span class="n">i</span>
<span class="n">isabundant</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="kc">true</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">s</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">for</span> <span class="n">i</span><span class="o">=</span><span class="mi">2</span><span class="o">:</span><span class="mi">28123</span>
<span class="n">b</span> <span class="o">=</span> <span class="kc">false</span>
<span class="k">for</span> <span class="n">j</span><span class="o">=</span><span class="mi">1</span><span class="o">:</span><span class="n">int</span><span class="p">(</span><span class="n">ceil</span><span class="p">(</span><span class="n">i</span><span class="o">/</span><span class="mi">2</span><span class="p">))</span>
<span class="k">if</span> <span class="n">isabundant</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">&&</span> <span class="n">isabundant</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="n">j</span><span class="p">]</span>
<span class="n">b</span> <span class="o">=</span> <span class="kc">true</span>
<span class="k">break</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">if</span> <span class="o">!</span><span class="n">b</span>
<span class="n">s</span> <span class="o">+=</span> <span class="n">i</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id24">
<h1>24</h1>
<p>求 0 .. 9 的排列组合中字典序的第 1000000 位是什么</p>
<pre class="code julia literal-block">
<span class="n">ps</span> <span class="o">=</span> <span class="p">[</span><span class="n">p</span> <span class="k">for</span> <span class="n">p</span> <span class="kp">in</span> <span class="n">permutations</span><span class="p">([</span><span class="mi">0</span><span class="o">:</span><span class="mi">9</span><span class="p">])]</span>
<span class="n">println</span><span class="p">(</span><span class="n">ps</span><span class="p">[</span><span class="mi">1000000</span><span class="p">])</span>
</pre>
</div>
<div class="section" id="id25">
<h1>25</h1>
<p>求斐波拉切数列从哪一位开始大于 1000 位数字</p>
<pre class="code julia literal-block">
<span class="n">a</span> <span class="o">=</span> <span class="kt">BigInt</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">b</span> <span class="o">=</span> <span class="kt">BigInt</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">c</span> <span class="o">=</span> <span class="kt">BigInt</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">i</span> <span class="o">=</span> <span class="mi">3</span>
<span class="n">T</span> <span class="o">=</span> <span class="kt">BigInt</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span><span class="o">^</span><span class="mi">999</span>
<span class="k">while</span> <span class="kc">true</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
<span class="k">if</span> <span class="n">c</span><span class="o">>=</span><span class="n">T</span>
<span class="k">break</span>
<span class="k">end</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">b</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">c</span>
<span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id26">
<h1>26</h1>
<p>求在 [2..1000) 区间内使 1/d 的循环节最长的 d</p>
<pre class="code julia literal-block">
<span class="k">function</span> <span class="n">recurringCycleLen</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="c"># length of recurrying cycle of 1/n</span>
<span class="n">R</span> <span class="o">=</span> <span class="kt">Int</span><span class="p">[]</span> <span class="c"># all occured remainders</span>
<span class="n">r</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">c</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="n">len</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="k">while</span> <span class="n">r</span><span class="o">></span><span class="mi">0</span> <span class="o">&&</span> <span class="o">!</span><span class="kp">in</span><span class="p">(</span><span class="n">r</span><span class="p">,</span><span class="n">R</span><span class="p">)</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">int</span><span class="p">(</span><span class="n">floor</span><span class="p">(</span><span class="n">r</span><span class="o">/</span><span class="n">n</span><span class="p">))</span>
<span class="k">if</span> <span class="n">n</span><span class="o"><</span><span class="n">r</span>
<span class="n">push!</span><span class="p">(</span><span class="n">R</span><span class="p">,</span> <span class="n">r</span><span class="p">)</span>
<span class="n">r</span> <span class="o">-=</span> <span class="n">c</span> <span class="o">*</span> <span class="n">n</span>
<span class="k">end</span>
<span class="n">r</span> <span class="o">*=</span> <span class="mi">10</span>
<span class="k">end</span>
<span class="k">if</span> <span class="n">r</span><span class="o">==</span><span class="mi">0</span>
<span class="k">return</span> <span class="mi">0</span>
<span class="k">end</span>
<span class="k">return</span> <span class="n">length</span><span class="p">(</span><span class="n">R</span><span class="p">)</span><span class="o">-</span><span class="n">findfirst</span><span class="p">(</span><span class="n">R</span><span class="p">,</span><span class="n">r</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span>
<span class="k">end</span>
<span class="n">maxn</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">maxl</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">1000</span>
<span class="n">l</span> <span class="o">=</span> <span class="n">recurringCycleLen</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="k">if</span> <span class="n">l</span><span class="o">></span><span class="n">maxl</span>
<span class="n">maxn</span> <span class="o">=</span> <span class="n">i</span>
<span class="n">maxl</span> <span class="o">=</span> <span class="n">l</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">maxn</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id27">
<h1>27</h1>
<p>求使得 <span class="formula">∀<i>i</i> ∈ [0, <i>N</i>] <i>i</i><sup>2</sup> + <i>a</i>*<i>i</i> + <i>b</i><i>为质数</i></span> 中的 N 取值最大的 <span class="formula"><i>a</i>, <i>b</i></span> 的乘积,
其中 <span class="formula">|<i>a</i>| < 1000</span> 且 <span class="formula">|<i>b</i>| < 1000</span></p>
<pre class="code julia literal-block">
<span class="n">maxlen</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">maxa</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">maxb</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">a</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1000</span><span class="o">:</span><span class="mi">1000</span>
<span class="k">for</span> <span class="n">b</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1000</span><span class="o">:</span><span class="mi">1000</span>
<span class="n">len</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">n</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">while</span> <span class="n">isprime</span><span class="p">(</span><span class="n">n</span><span class="o">^</span><span class="mi">2</span> <span class="o">+</span> <span class="n">a</span><span class="o">*</span><span class="n">n</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span>
<span class="n">len</span><span class="o">+=</span><span class="mi">1</span>
<span class="n">n</span><span class="o">+=</span><span class="mi">1</span>
<span class="k">end</span>
<span class="k">if</span> <span class="n">len</span><span class="o">></span><span class="n">maxlen</span>
<span class="n">maxlen</span> <span class="o">=</span> <span class="n">len</span>
<span class="n">maxa</span> <span class="o">=</span> <span class="n">a</span>
<span class="n">maxb</span> <span class="o">=</span> <span class="n">b</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="s">"a = </span><span class="si">$maxa</span><span class="s">, b = </span><span class="si">$maxb</span><span class="s">, maxlen = </span><span class="si">$maxlen</span><span class="s">, a*b=</span><span class="si">$</span><span class="p">(</span><span class="n">maxa</span><span class="o">*</span><span class="n">maxb</span><span class="p">)</span><span class="s">"</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id28">
<h1>28</h1>
<p>求数字螺旋对角线上的值的和</p>
<pre class="code julia literal-block">
<span class="n">c</span> <span class="o">=</span> <span class="mi">1</span> <span class="c"># current</span>
<span class="n">s</span> <span class="o">=</span> <span class="mi">1</span> <span class="c"># sum</span>
<span class="n">step</span> <span class="o">=</span> <span class="mi">2</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">500</span>
<span class="k">for</span> <span class="n">j</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">4</span>
<span class="n">c</span> <span class="o">+=</span> <span class="n">step</span>
<span class="n">s</span> <span class="o">+=</span> <span class="n">c</span>
<span class="k">end</span>
<span class="n">step</span> <span class="o">+=</span> <span class="mi">2</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id29">
<h1>29</h1>
<p>求对于 <span class="formula">2 ≤ <i>a</i> ≤ 100</span> 且 <span class="formula">2 ≤ <i>b</i> ≤ 100</span>,
<span class="formula"><i>a</i><sup><i>b</i></sup></span> 一共有多少个不重复的值</p>
<p>任何一个理智的人都应该算出 <span class="formula">99 × 99</span> 然后减去重复的 …… 然而其实直接算也挺快的,不管了 ……</p>
<pre class="code julia literal-block">
<span class="n">li</span> <span class="o">=</span> <span class="p">[</span><span class="s">"4"</span><span class="o">=></span><span class="mi">1</span><span class="p">]</span>
<span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">2</span><span class="o">:</span><span class="mi">100</span>
<span class="k">for</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">2</span><span class="o">:</span><span class="mi">100</span>
<span class="n">li</span><span class="p">[</span><span class="n">string</span><span class="p">(</span><span class="kt">BigInt</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">^</span><span class="n">j</span><span class="p">)]</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">length</span><span class="p">(</span><span class="n">li</span><span class="p">))</span>
</pre>
</div>
<div class="section" id="id30">
<h1>30</h1>
<p>求所有可以用自身每位数字的五次方相加表示的整数</p>
<p>需要思考的问题只有 —— 上限是多少?</p>
<p>若我们把一个大于 0 的 n 位数字表示为 <span class="formula"><span class="limits"><span class="limit">∑</span></span><span class="scripts"><sup class="script"><i>n</i></sup><sub class="script"><i>i</i> = 1</sub></span><i>d</i><sub><i>i</i></sub> × 10<sup><i>n</i> − <i>i</i></sup></span>,
其中 <span class="formula"><i>d</i><sub><i>i</i></sub></span> 表示其第 i 位的数字,那么不难看出,
每位数字的五次方相加的值域是 <span class="formula">1 ≤ <span class="limits"><span class="limit">∑</span></span><span class="scripts"><sup class="script"><i>n</i></sup><sub class="script"><i>i</i> = 1</sub></span><i>d</i><sub><i>i</i></sub><sup>5</sup> ≤ 9<sup>5</sup> × <i>n</i></span>,
而其自身的值域则是 <span class="formula">10<sup><i>n</i> − 1</sup> ≤ <span class="limits"><span class="limit">∑</span></span><span class="scripts"><sup class="script"><i>n</i></sup><sub class="script"><i>i</i> = 1</sub></span><i>d</i><sub><i>i</i></sub> × 10<sup><i>n</i> − <i>i</i></sup> ≤ 10<sup><i>n</i></sup> − 1</span></p>
<p>于是,当 n>6 的时候, <span class="formula">9<sup>5</sup> × <i>n</i> < 10<sup><i>n</i> − 1</sup></span>, 从这里开始往后不可能有解了</p>
<pre class="code julia literal-block">
<span class="c"># when n>6, 9^5*n < 10^(n-1)</span>
<span class="c"># so set the upper limit to 9^5*6 is safe</span>
<span class="n">ps</span> <span class="o">=</span> <span class="n">map</span><span class="p">(</span><span class="n">x</span><span class="o">-></span><span class="n">x</span><span class="o">^</span><span class="mi">5</span><span class="p">,</span> <span class="p">[</span><span class="mi">0</span><span class="o">:</span><span class="mi">9</span><span class="p">])</span>
<span class="n">s</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">2</span><span class="o">:</span><span class="mi">9</span><span class="o">^</span><span class="mi">5</span><span class="o">*</span><span class="mi">6</span>
<span class="k">if</span> <span class="n">sum</span><span class="p">(</span><span class="n">map</span><span class="p">(</span><span class="n">c</span><span class="o">-></span><span class="n">ps</span><span class="p">[</span><span class="n">c</span><span class="o">-</span><span class="sc">'0'</span><span class="o">+</span><span class="mi">1</span><span class="p">],</span> <span class="n">collect</span><span class="p">(</span><span class="n">string</span><span class="p">(</span><span class="n">i</span><span class="p">))))</span> <span class="o">==</span> <span class="n">i</span>
<span class="c"># println(i)</span>
<span class="n">s</span> <span class="o">+=</span> <span class="n">i</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id31">
<h1>31</h1>
<p>求使用面值 1p, 2p, 5p, 10p, 20p, 50p, £1, £2 面值的硬币,一共有多少种方式组成 £2,
其中 £1 = 100p</p>
<p>经典的 DP 问题 ——若是已知不使用面值 x 的硬币,数字 a 有 N[a] 种表示法,
显然加上 x 面值硬币之后,若 a>=x,数字 a 的表示方法将是 N[a] + N[a-x],否则依然是 N[a]。
于是可以这么递推出来 ——</p>
<pre class="code julia literal-block">
<span class="n">cs</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">50</span><span class="p">,</span><span class="mi">100</span><span class="p">,</span><span class="mi">200</span><span class="p">]</span>
<span class="n">ws</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span> <span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">200</span><span class="p">]</span>
<span class="k">for</span> <span class="n">c</span> <span class="kp">in</span> <span class="n">cs</span>
<span class="n">ws</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span> <span class="c"># just c</span>
<span class="k">for</span> <span class="n">v</span> <span class="kp">in</span> <span class="n">c</span><span class="o">+</span><span class="mi">1</span><span class="o">:</span><span class="mi">200</span>
<span class="n">ws</span><span class="p">[</span><span class="n">v</span><span class="p">]</span> <span class="o">+=</span> <span class="n">ws</span><span class="p">[</span><span class="n">v</span><span class="o">-</span><span class="n">c</span><span class="p">]</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">ws</span><span class="p">[</span><span class="mi">200</span><span class="p">])</span>
</pre>
</div>
<div class="section" id="id32">
<h1>32</h1>
<p>找出所有 <span class="formula"><i>a</i> × <i>b</i> = <i>c</i></span> 且 <span class="formula"><i>a</i>, <i>b</i>, <i>c</i></span> 每一位数字组成的列表正好包含 1 .. 9 每个数字各一次</p>
<pre class="code julia literal-block">
<span class="n">ps</span> <span class="o">=</span> <span class="kt">Int</span><span class="p">[]</span>
<span class="n">one2nine</span> <span class="o">=</span> <span class="n">collect</span><span class="p">(</span><span class="s">"123456789"</span><span class="p">)</span>
<span class="k">function</span> <span class="n">hasrep</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="n">s</span><span class="o">=</span><span class="n">string</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">2</span><span class="o">:</span><span class="n">length</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
<span class="k">if</span> <span class="n">s</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">==</span><span class="n">s</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
<span class="k">return</span> <span class="kc">true</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">return</span> <span class="kc">false</span>
<span class="k">end</span>
<span class="k">for</span> <span class="n">i</span><span class="o">=</span><span class="mi">2</span><span class="o">:</span><span class="mi">2000</span>
<span class="k">if</span> <span class="n">hasrep</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="k">continue</span>
<span class="k">end</span>
<span class="k">for</span> <span class="n">j</span><span class="o">=</span><span class="n">i</span><span class="o">:</span><span class="mi">2000</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">i</span><span class="o">*</span><span class="n">j</span>
<span class="c">#if hasrep(j)</span>
<span class="c"># continue</span>
<span class="c">#end</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">string</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">,</span><span class="n">p</span><span class="p">)</span>
<span class="k">if</span> <span class="n">length</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="o">==</span><span class="mi">9</span> <span class="o">&&</span> <span class="n">sort</span><span class="p">(</span><span class="n">collect</span><span class="p">(</span><span class="n">s</span><span class="p">))</span><span class="o">==</span><span class="n">one2nine</span>
<span class="n">push!</span><span class="p">(</span><span class="n">ps</span><span class="p">,</span><span class="n">p</span><span class="p">)</span>
<span class="n">println</span><span class="p">(</span><span class="s">"</span><span class="si">$i</span><span class="s"> x </span><span class="si">$j</span><span class="s"> = </span><span class="si">$p</span><span class="s">"</span><span class="p">);</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">sort!</span><span class="p">(</span><span class="n">ps</span><span class="p">)</span>
<span class="n">s</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">i</span><span class="o">=</span><span class="mi">2</span><span class="o">:</span><span class="n">length</span><span class="p">(</span><span class="n">ps</span><span class="p">)</span>
<span class="k">if</span> <span class="n">ps</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">!=</span><span class="n">ps</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
<span class="n">s</span> <span class="o">+=</span> <span class="n">ps</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">if</span> <span class="n">ps</span><span class="p">[</span><span class="k">end</span><span class="p">]</span><span class="o">!=</span><span class="n">ps</span><span class="p">[</span><span class="k">end</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="n">s</span> <span class="o">+=</span> <span class="n">ps</span><span class="p">[</span><span class="k">end</span><span class="p">]</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id33">
<h1>33</h1>
<p>找出分子分母各是两位数且能够从分子分母上各删除同一个数字其值不变的分数</p>
<p><span class="formula">11 ⁄ 22 = 1 ⁄ 2</span> 或是 <span class="formula">10 ⁄ 20 = 1 ⁄ 2</span> 这种显然成立的除外</p>
<pre class="code julia literal-block">
<span class="n">fs</span> <span class="o">=</span> <span class="kt">Rational</span><span class="p">{</span><span class="kt">Int64</span><span class="p">}[]</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">12</span><span class="o">:</span><span class="mi">98</span>
<span class="k">for</span> <span class="n">j</span> <span class="kp">in</span> <span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="o">:</span><span class="mi">99</span>
<span class="c"># i = a*10 + b</span>
<span class="c"># j = c*10 + d</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">int</span><span class="p">(</span><span class="n">floor</span><span class="p">(</span><span class="n">i</span><span class="o">/</span><span class="mi">10</span><span class="p">))</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">i</span><span class="o">%</span><span class="mi">10</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">int</span><span class="p">(</span><span class="n">floor</span><span class="p">(</span><span class="n">j</span><span class="o">/</span><span class="mi">10</span><span class="p">))</span>
<span class="n">d</span> <span class="o">=</span> <span class="n">j</span><span class="o">%</span><span class="mi">10</span>
<span class="k">if</span> <span class="n">a</span><span class="o">==</span><span class="n">b</span> <span class="o">||</span> <span class="n">c</span><span class="o">==</span><span class="n">d</span>
<span class="k">continue</span>
<span class="k">end</span>
<span class="k">if</span> <span class="p">(</span><span class="n">i</span><span class="o">//</span><span class="n">j</span> <span class="o">==</span> <span class="n">b</span><span class="o">//</span><span class="n">c</span> <span class="o">&&</span> <span class="n">a</span><span class="o">==</span><span class="n">d</span> <span class="o">||</span>
<span class="n">i</span><span class="o">//</span><span class="n">j</span> <span class="o">==</span> <span class="n">a</span><span class="o">//</span><span class="n">d</span> <span class="o">&&</span> <span class="n">b</span><span class="o">==</span><span class="n">c</span><span class="p">)</span>
<span class="n">push!</span><span class="p">(</span><span class="n">fs</span><span class="p">,</span> <span class="n">i</span><span class="o">//</span><span class="n">j</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">prod</span><span class="p">(</span><span class="n">fs</span><span class="p">))</span>
</pre>
</div>
<div class="section" id="id34">
<h1>34</h1>
<p>找出所有(每一位数字的阶乘的和等于自身)的数字的和</p>
<p>与 <a class="reference internal" href="#id30">30</a> 题相似的是,只要找到值域就好算了</p>
<pre class="code julia literal-block">
<span class="c"># factorial(9) = 362880</span>
<span class="c"># for number has n digits, 1 <= sum of its factorial digits <= 362880n</span>
<span class="c"># for number has n digits, 10^(n-1) <= its value < 10^n</span>
<span class="n">fac</span> <span class="o">=</span> <span class="p">[(</span><span class="n">i</span><span class="o">=></span><span class="n">factorial</span><span class="p">(</span><span class="n">i</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">0</span><span class="o">:</span><span class="mi">9</span><span class="p">]</span>
<span class="k">function</span> <span class="n">digifacsum</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="n">s</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">while</span> <span class="n">n</span><span class="o">>=</span><span class="mi">1</span>
<span class="n">s</span> <span class="o">+=</span> <span class="n">fac</span><span class="p">[</span><span class="n">n</span><span class="o">%</span><span class="mi">10</span><span class="p">]</span>
<span class="n">n</span> <span class="o">=</span> <span class="n">int</span><span class="p">(</span><span class="n">floor</span><span class="p">(</span><span class="n">n</span><span class="o">/</span><span class="mi">10</span><span class="p">))</span>
<span class="k">end</span>
<span class="k">return</span> <span class="n">s</span>
<span class="k">end</span>
<span class="n">s</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">3</span><span class="o">:</span><span class="n">fac</span><span class="p">[</span><span class="mi">9</span><span class="p">]</span><span class="o">*</span><span class="mi">8</span>
<span class="k">if</span> <span class="n">digifacsum</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">==</span><span class="n">i</span>
<span class="n">println</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="n">s</span> <span class="o">+=</span> <span class="n">i</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id35">
<h1>35</h1>
<p><span class="formula">10<sup>6</sup></span> 以下有几个质数随意循环移位还是质数</p>
<pre class="code julia literal-block">
<span class="k">function</span> <span class="n">tonum</span><span class="p">(</span><span class="n">carr</span><span class="p">)</span>
<span class="n">v</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">d</span> <span class="kp">in</span> <span class="n">carr</span>
<span class="n">v</span> <span class="o">*=</span> <span class="mi">10</span>
<span class="n">v</span> <span class="o">+=</span> <span class="n">d</span><span class="o">-</span><span class="sc">'0'</span>
<span class="k">end</span>
<span class="k">return</span> <span class="n">v</span>
<span class="k">end</span>
<span class="k">function</span> <span class="n">circular</span><span class="p">(</span><span class="n">prm</span><span class="p">)</span>
<span class="n">res</span> <span class="o">=</span> <span class="kt">Int</span><span class="p">[]</span>
<span class="n">col</span> <span class="o">=</span> <span class="n">collect</span><span class="p">(</span><span class="n">string</span><span class="p">(</span><span class="n">prm</span><span class="p">))</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">2</span><span class="o">:</span><span class="n">length</span><span class="p">(</span><span class="n">col</span><span class="p">)</span>
<span class="n">push!</span><span class="p">(</span><span class="n">res</span><span class="p">,</span> <span class="n">tonum</span><span class="p">(</span><span class="n">vcat</span><span class="p">(</span><span class="n">col</span><span class="p">[</span><span class="n">i</span><span class="o">:</span><span class="k">end</span><span class="p">],</span> <span class="n">col</span><span class="p">[</span><span class="mi">1</span><span class="o">:</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">])))</span>
<span class="k">end</span>
<span class="k">return</span> <span class="n">res</span>
<span class="k">end</span>
<span class="n">cnt</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">p</span> <span class="kp">in</span> <span class="n">primes</span><span class="p">(</span><span class="mi">1000000</span><span class="p">)</span>
<span class="n">iscircular</span> <span class="o">=</span> <span class="kc">true</span>
<span class="k">for</span> <span class="n">x</span> <span class="kp">in</span> <span class="n">circular</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
<span class="k">if</span> <span class="o">!</span><span class="n">isprime</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">iscircular</span> <span class="o">=</span> <span class="kc">false</span>
<span class="k">break</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">if</span> <span class="n">iscircular</span>
<span class="c"># println(p)</span>
<span class="n">cnt</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">cnt</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id36">
<h1>36</h1>
<p>求 <span class="formula">10<sup>6</sup></span> 以下在十进制与二进制下都是回文数的数字之和</p>
<pre class="code julia literal-block">
<span class="k">function</span> <span class="n">ispalindrome</span><span class="p">(</span><span class="n">s</span><span class="o">::</span><span class="n">String</span><span class="p">)</span>
<span class="k">if</span> <span class="n">length</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="o"><=</span><span class="mi">2</span>
<span class="k">return</span> <span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">==</span><span class="n">s</span><span class="p">[</span><span class="k">end</span><span class="p">]</span>
<span class="k">end</span>
<span class="k">return</span> <span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">==</span><span class="n">s</span><span class="p">[</span><span class="k">end</span><span class="p">]</span> <span class="o">&&</span> <span class="n">ispalindrome</span><span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="mi">2</span><span class="o">:</span><span class="k">end</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">sum</span><span class="p">(</span><span class="n">filter</span><span class="p">(</span><span class="n">i</span><span class="o">-></span><span class="n">ispalindrome</span><span class="p">(</span><span class="n">string</span><span class="p">(</span><span class="n">i</span><span class="p">))</span> <span class="o">&&</span> <span class="n">ispalindrome</span><span class="p">(</span><span class="n">base</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="n">i</span><span class="p">)),</span>
<span class="p">[</span><span class="mi">1</span><span class="o">:</span><span class="mi">1000000</span><span class="p">])))</span>
</pre>
</div>
<div class="section" id="id37">
<h1>37</h1>
<p>求从左到由每次删掉一位数字依然是质数的质数之和,已知一共有 11 个这样的质数</p>
<pre class="code julia literal-block">
<span class="n">ps</span> <span class="o">=</span> <span class="n">primes</span><span class="p">(</span><span class="mi">1000000</span><span class="p">)</span> <span class="c"># just guess</span>
<span class="k">function</span> <span class="n">istruncatable</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="k">if</span> <span class="n">n</span><span class="o"><</span><span class="mi">10</span>
<span class="k">return</span> <span class="kc">false</span>
<span class="k">end</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">n</span>
<span class="k">while</span> <span class="n">x</span><span class="o">>=</span><span class="mi">1</span>
<span class="k">if</span> <span class="o">!</span><span class="n">isprime</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">return</span> <span class="kc">false</span> <span class="k">end</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">int</span><span class="p">(</span><span class="n">floor</span><span class="p">(</span><span class="n">x</span><span class="o">/</span><span class="mi">10</span><span class="p">))</span>
<span class="k">end</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">string</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span><span class="o">=</span><span class="mi">2</span><span class="o">:</span><span class="n">length</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
<span class="k">if</span> <span class="o">!</span><span class="n">isprime</span><span class="p">(</span><span class="n">int</span><span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="n">i</span><span class="o">:</span><span class="k">end</span><span class="p">]))</span> <span class="k">return</span> <span class="kc">false</span> <span class="k">end</span>
<span class="k">end</span>
<span class="k">return</span> <span class="kc">true</span>
<span class="k">end</span>
<span class="n">cnt</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">s</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">p</span> <span class="kp">in</span> <span class="n">ps</span>
<span class="k">if</span> <span class="n">istruncatable</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
<span class="n">println</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
<span class="n">s</span> <span class="o">+=</span> <span class="n">p</span>
<span class="o">++</span><span class="n">cnt</span>
<span class="k">if</span> <span class="n">cnt</span><span class="o">==</span><span class="mi">11</span>
<span class="k">break</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id38">
<h1>38</h1>
<pre class="code julia literal-block">
<span class="n">ispandigital</span><span class="p">(</span><span class="n">s</span><span class="o">::</span><span class="n">String</span><span class="p">)</span> <span class="o">=</span> <span class="n">sort</span><span class="p">(</span><span class="n">collect</span><span class="p">(</span><span class="n">s</span><span class="p">))</span> <span class="o">==</span> <span class="n">collect</span><span class="p">(</span><span class="s">"123456789"</span><span class="p">)</span>
<span class="o">+</span><span class="p">(</span><span class="n">s1</span><span class="o">::</span><span class="n">String</span><span class="p">,</span><span class="n">s2</span><span class="o">::</span><span class="n">String</span><span class="p">)</span> <span class="o">=</span> <span class="n">string</span><span class="p">(</span><span class="n">s1</span><span class="p">,</span><span class="n">s2</span><span class="p">)</span>
<span class="k">function</span> <span class="n">guesspandigital</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">string</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">i</span> <span class="o">=</span> <span class="mi">2</span>
<span class="k">while</span> <span class="n">length</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="o"><</span><span class="mi">9</span>
<span class="n">s</span> <span class="o">+=</span> <span class="n">string</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">i</span><span class="p">)</span>
<span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">end</span>
<span class="k">return</span> <span class="n">s</span>
<span class="k">end</span>
<span class="n">ispandigitalmultiple</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">=</span> <span class="n">ispandigital</span><span class="p">(</span><span class="n">guesspandigital</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="n">largest</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">p</span> <span class="kp">in</span> <span class="mi">2</span><span class="o">:</span><span class="mi">10000</span> <span class="c"># 10000 -> 1000020000</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">guesspandigital</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
<span class="k">if</span> <span class="n">ispandigital</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">&&</span> <span class="n">largest</span><span class="o"><</span><span class="n">int</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
<span class="n">largest</span> <span class="o">=</span> <span class="n">int</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">p</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">largest</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id39">
<h1>39</h1>
<p>问对于 <span class="formula"><i>p</i> ≤ 1000</span>, <span class="formula"><i>p</i></span> 取值多少的时候方程</p>
<div class="formula">
<span class="array"><span class="arrayrow"><span class="bracket align-l">⎧</span></span><span class="arrayrow"><span class="bracket align-l">⎪</span></span><span class="arrayrow"><span class="bracket align-l">⎨</span></span><span class="arrayrow"><span class="bracket align-l">⎪</span></span><span class="arrayrow"><span class="bracket align-l">⎩</span></span></span><span class="bracketcases">
<span class="arrayrow">
<span class="case align-l">
<i>a</i><sup>2</sup> + <i>b</i><sup>2</sup> = <i>c</i><sup>2</sup>
</span>
</span>
<span class="arrayrow">
<span class="case align-l">
</span>
</span>
<span class="arrayrow">
<span class="case align-l">
<i>a</i> < <i>b</i> < <i>c</i>
</span>
</span>
<span class="arrayrow">
<span class="case align-l">
</span>
</span>
<span class="arrayrow">
<span class="case align-l">
<i>a</i> + <i>b</i> + <i>c</i> = <i>p</i>
</span>
</span>
</span>
</div>
<p>有最多的解</p>
<pre class="code julia literal-block">
<span class="k">function</span> <span class="n">numsolution</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
<span class="n">cnt</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">a</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">300</span>
<span class="k">for</span> <span class="n">b</span> <span class="kp">in</span> <span class="n">a</span><span class="o">+</span><span class="mi">1</span><span class="o">:</span><span class="p">(</span><span class="n">p</span><span class="o">-</span><span class="n">a</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="o">+</span><span class="n">b</span><span class="o">*</span><span class="n">b</span><span class="p">)</span>
<span class="k">if</span> <span class="n">isinteger</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="o">&&</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span><span class="o">+</span><span class="n">c</span> <span class="o">==</span> <span class="n">p</span>
<span class="n">cnt</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">return</span> <span class="n">cnt</span>
<span class="k">end</span>
<span class="n">x</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">n</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">1000</span>
<span class="n">ns</span> <span class="o">=</span> <span class="n">numsolution</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="k">if</span> <span class="n">ns</span><span class="o">></span><span class="n">n</span>
<span class="n">n</span> <span class="o">=</span> <span class="n">ns</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">i</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">println</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="id40">
<h1>40</h1>
<p>由于数据量太小(对于我的计算机),我就直接算出来了</p>
<p>出题者意图肯定不是希望这么作弊的 - -</p>
<pre class="code julia literal-block">
<span class="o">+</span><span class="p">(</span><span class="n">s1</span><span class="o">::</span><span class="n">String</span><span class="p">,</span><span class="n">s2</span><span class="o">::</span><span class="n">String</span><span class="p">)</span> <span class="o">=</span> <span class="n">string</span><span class="p">(</span><span class="n">s1</span><span class="p">,</span><span class="n">s2</span><span class="p">)</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">sum</span><span class="p">(</span><span class="n">map</span><span class="p">(</span><span class="n">string</span><span class="p">,[</span><span class="mi">1</span><span class="o">:</span><span class="mi">1000000</span><span class="p">]))</span>
<span class="n">println</span><span class="p">(</span><span class="n">prod</span><span class="p">(</span><span class="n">map</span><span class="p">(</span><span class="n">i</span><span class="o">-></span><span class="n">s</span><span class="p">[</span><span class="mi">10</span><span class="o">^</span><span class="n">i</span><span class="p">]</span><span class="o">-</span><span class="sc">'0'</span><span class="p">,</span> <span class="p">[</span><span class="mi">0</span><span class="o">:</span><span class="mi">6</span><span class="p">])))</span>
</pre>
</div>
<div class="section" id="id41">
<h1>41</h1>
<p>求最大的包含 <span class="formula">[1, <i>n</i>]</span> 每个数字的 n 位质数</p>
<p>显然 n 不会超过 9,直接从后往前找包含 <span class="formula">[1, <i>n</i>]</span> 每个数字的数判断一下</p>
<pre class="code julia literal-block">
<span class="n">ps</span> <span class="o">=</span> <span class="kt">Int</span><span class="p">[]</span>
<span class="n">tonum</span><span class="p">(</span><span class="n">li</span><span class="p">)</span> <span class="o">=</span> <span class="n">sum</span><span class="p">(</span><span class="n">map</span><span class="p">(</span><span class="n">i</span><span class="o">-></span><span class="n">li</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="mi">10</span><span class="o">^</span><span class="p">(</span><span class="n">length</span><span class="p">(</span><span class="n">li</span><span class="p">)</span><span class="o">-</span><span class="n">i</span><span class="p">),</span> <span class="p">[</span><span class="mi">1</span><span class="o">:</span><span class="n">length</span><span class="p">(</span><span class="n">li</span><span class="p">)]))</span>
<span class="k">for</span> <span class="n">d</span> <span class="kp">in</span> <span class="mi">9</span><span class="o">:-</span><span class="mi">1</span><span class="o">:</span><span class="mi">2</span>
<span class="n">found</span> <span class="o">=</span> <span class="kc">false</span>
<span class="k">for</span> <span class="n">p</span> <span class="kp">in</span> <span class="n">permutations</span><span class="p">([</span><span class="n">d</span><span class="o">:-</span><span class="mi">1</span><span class="o">:</span><span class="mi">1</span><span class="p">])</span>
<span class="k">if</span> <span class="n">isprime</span><span class="p">(</span><span class="n">tonum</span><span class="p">(</span><span class="n">p</span><span class="p">))</span>
<span class="n">push!</span><span class="p">(</span><span class="n">ps</span><span class="p">,</span><span class="n">tonum</span><span class="p">(</span><span class="n">p</span><span class="p">))</span>
<span class="n">found</span> <span class="o">=</span> <span class="kc">true</span>
<span class="k">break</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">if</span> <span class="n">found</span>
<span class="k">break</span>