-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
1219 lines (977 loc) · 96.7 KB
/
atom.xml
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"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Keep Hacking :)]]></title>
<link href="http://white1033.github.com/atom.xml" rel="self"/>
<link href="http://white1033.github.com/"/>
<updated>2013-04-20T11:16:56+08:00</updated>
<id>http://white1033.github.com/</id>
<author>
<name><![CDATA[Yen-Ying Lee]]></name>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[初探Go]]></title>
<link href="http://white1033.github.com/blog/2013/03/17/lets-go/"/>
<updated>2013-03-17T17:00:00+08:00</updated>
<id>http://white1033.github.com/blog/2013/03/17/lets-go</id>
<content type="html"><![CDATA[<p>主要想學的原因是因為一些<a href="http://projecteuler.net/">ProjectEuler</a>的題目用<code>Ruby</code>或是<code>Perl</code>寫的時候,因為計算量太大,然後程式沒有辦法在一分鐘內跑完。</p>
<p>本來是想用之前學的<code>C/C++</code>來寫的,不過看到最近<code>golang</code>這麼熱,就來學一下啦!
先來記錄一些學習的資料</p>
<ul>
<li><a href="http://golang.org">官網</a></li>
<li><a href="http://godoc.org">GoDoc</a></li>
<li><a href="http://tour.golang.org/#1">A Tour of Go</a></li>
<li><a href="http://golang.org/doc/effective_go.html">Effective Go</a></li>
<li><a href="http://www.golang-book.com">An Introduction to Programming in Go</a></li>
<li><a href="https://github.com/astaxie/build-web-application-with-golang/blob/master/preface.md">用Go開發網頁應用</a></li>
</ul>
<p>這些應該夠我看一段時間了:)</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[從rvm轉換到rbenv]]></title>
<link href="http://white1033.github.com/blog/2013/03/10/from-rvm-to-rbenv/"/>
<updated>2013-03-10T01:16:00+08:00</updated>
<id>http://white1033.github.com/blog/2013/03/10/from-rvm-to-rbenv</id>
<content type="html"><![CDATA[<p>最近把用了一陣子的<code>rvm</code>給反安裝掉了,然後換成了<code>rbenv</code>來用用。會換過來的原因只是覺得<code>rbenv</code>比較輕量,而不是覺得<code>rvm</code>真的有什麼不好。</p>
<p>來記錄一下要怎麼轉換過來。</p>
<h2>反安裝rvm</h2>
<p>這個應該是最簡單的一步XD</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>rvm implode
</span></code></pre></td></tr></table></div></figure>
<p>然後再將你<code>zsh</code>或<code>bash</code>中的這一句拿掉。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="o">[[</span> -s <span class="s2">"$HOME/.rvm/scripts/rvm"</span> <span class="o">]]</span> <span class="o">&&</span> . <span class="s2">"$HOME/.rvm/scripts/rvm"</span> <span class="c"># Load RVM function</span>
</span></code></pre></td></tr></table></div></figure>
<!-- more -->
<h2>安裝rbenv</h2>
<p>我是透過<a href="http://mxcl.github.com/homebrew/">Homebrew</a>去安裝的。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>brew install rbenv
</span><span class='line'><span class="nv">$ </span>brew install ruby-build
</span></code></pre></td></tr></table></div></figure>
<p>要安裝<code>ruby-build</code>的原因是因為可以比較簡單的去安裝各個版本的ruby。</p>
<p>然後是把<code>~/.rbenv/bin</code>加到你的<code>$PATH</code>中。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span><span class="nb">echo</span> <span class="s1">'export PATH="$HOME/.rbenv/bin:$PATH"'</span> >> ~/.zshrc
</span></code></pre></td></tr></table></div></figure>
<p>再來是將<code>rbenv init</code>加到你的shell中。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span><span class="nb">echo</span> <span class="s1">'eval "$(rbenv init -)"'</span> >> ~/.zshrc
</span></code></pre></td></tr></table></div></figure>
<p>重新載入設定。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span><span class="nb">source</span> ~/.zshrc
</span></code></pre></td></tr></table></div></figure>
<p>裝個好用的plugin <a href="https://github.com/tpope/rbenv-aliases">Aliases for rbenv Ruby versions</a></p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>mkdir -p ~/.rbenv/plugins
</span><span class='line'>git clone git://github.com/tpope/rbenv-aliases.git <span class="se">\</span>
</span><span class='line'> ~/.rbenv/plugins/rbenv-aliases
</span><span class='line'>rbenv <span class="nb">alias</span> --auto
</span></code></pre></td></tr></table></div></figure>
<p>最後當然是來裝個最新的ruby 2.0來玩玩:p</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>rbenv install 2.0.0-p0
</span></code></pre></td></tr></table></div></figure>
<h2>基本指令</h2>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>rbenv install --list <span class="c">#列出所有可安裝版本</span>
</span><span class='line'><span class="nv">$ </span>rbenv versions <span class="c">#列出所有安裝的版本</span>
</span><span class='line'><span class="nv">$ </span>rbenv version <span class="c">#列出目前使用的版本</span>
</span><span class='line'><span class="nv">$ </span>rbenv global 2.0.0 <span class="c">#預設使用2.0.0</span>
</span><span class='line'><span class="nv">$ </span>rbenv shell 2.0.0 <span class="c">#在目前使用的shell使用 2.0.0</span>
</span><span class='line'><span class="nv">$ </span>rbenv <span class="nb">local </span>2.0.0 <span class="c">#在目前的資料夾使用 2.0.0</span>
</span></code></pre></td></tr></table></div></figure>
<p>先寫到這裡,之後有什麼心得再補上來。</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[一些Octopress的小技巧]]></title>
<link href="http://white1033.github.com/blog/2012/12/15/some-usefull-octopress-tips/"/>
<updated>2012-12-15T21:29:00+08:00</updated>
<id>http://white1033.github.com/blog/2012/12/15/some-usefull-octopress-tips</id>
<content type="html"><![CDATA[<p>最近在發現了一些Octopress的小技巧,就放上來跟大家一同分享。</p>
<h3>上傳文章</h3>
<p>大家在上傳文章前必做的兩個指令就是</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>rake generate
</span><span class='line'>rake deploy</span></code></pre></td></tr></table></div></figure>
<p>不過它其實可以簡化成</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>rake gen_deploy</span></code></pre></td></tr></table></div></figure>
<p>就可以把兩個指令併成一個指令了!</p>
<!-- more -->
<h3>新增頁面</h3>
<p>之前都不知道要如何新增一個新的頁面出來,直到最近(遮臉)
其實就跟新增文章的指令一樣<code>new_post[your_post]</code>,新的頁面的指令就是<code>new_page[your_page]</code>,它就會產生一個makedown的檔案讓妳去編輯。之後妳需要做的就是將連結放到妳想要的地方了。</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Project Euler 61的解答]]></title>
<link href="http://white1033.github.com/blog/2012/10/14/project-euler-solution-061/"/>
<updated>2012-10-14T20:42:00+08:00</updated>
<id>http://white1033.github.com/blog/2012/10/14/project-euler-solution-061</id>
<content type="html"><![CDATA[<p><a href="http://projecteuler.net/problem=61">題目</a>介紹了六種數列,分別是三角形數、四角形數、五角形數、六角形數、七角形數和八角形數。現在問題要找出來六個數字,分別在這六個數列中,而且可以頭尾相連連成一圈。網頁上有舉一個長度為四的數列,如果不懂的話可以去看看。</p>
<h2>做表查一下</h2>
<p>這題好像出奇的簡單啊啊啊啊,我的做法是把這六種數列的頭兩位數字做成一個表,然後用後兩位數去查有沒有合的數字,然後就一直查下去,直到有六個數字分別來自六個數列並頭尾相連成一圈。</p>
<!-- more -->
<p>附上程式碼:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1">#!/usr/bin/env ruby -wKU</span>
</span><span class='line'><span class="n">func</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'> <span class="mi">3</span> <span class="o">=></span> <span class="o">-></span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">{</span><span class="n">x</span> <span class="o">*</span> <span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">},</span>
</span><span class='line'> <span class="mi">4</span> <span class="o">=></span> <span class="o">-></span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">{</span><span class="n">x</span> <span class="o">*</span> <span class="n">x</span><span class="p">},</span>
</span><span class='line'> <span class="mi">5</span> <span class="o">=></span> <span class="o">-></span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">{</span><span class="n">x</span> <span class="o">*</span> <span class="p">(</span><span class="mi">3</span> <span class="o">*</span> <span class="n">x</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">},</span>
</span><span class='line'> <span class="mi">6</span> <span class="o">=></span> <span class="o">-></span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">{</span><span class="n">x</span> <span class="o">*</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">x</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)},</span>
</span><span class='line'> <span class="mi">7</span> <span class="o">=></span> <span class="o">-></span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">{</span><span class="n">x</span> <span class="o">*</span> <span class="p">(</span><span class="mi">5</span> <span class="o">*</span> <span class="n">x</span> <span class="o">-</span> <span class="mi">3</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">},</span>
</span><span class='line'> <span class="mi">8</span> <span class="o">=></span> <span class="o">-></span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">{</span><span class="n">x</span> <span class="o">*</span> <span class="p">(</span><span class="mi">3</span> <span class="o">*</span> <span class="n">x</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="n">n</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="vg">$type8</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">[]</span>
</span><span class='line'><span class="vg">$nums</span> <span class="o">=</span> <span class="no">Hash</span><span class="o">.</span><span class="n">new</span> <span class="p">{</span> <span class="o">|</span><span class="nb">hash</span><span class="p">,</span> <span class="n">key</span><span class="o">|</span> <span class="nb">hash</span><span class="o">[</span><span class="n">key</span><span class="o">]</span> <span class="o">=</span> <span class="o">[]</span> <span class="p">}</span>
</span><span class='line'><span class="k">while</span> <span class="kp">true</span>
</span><span class='line'> <span class="n">n</span> <span class="o">+=</span> <span class="mi">1</span>
</span><span class='line'> <span class="p">(</span><span class="mi">3</span> <span class="o">.</span><span class="n">.</span> <span class="mi">8</span><span class="p">)</span><span class="o">.</span><span class="n">reverse_each</span> <span class="k">do</span> <span class="o">|</span><span class="n">x</span><span class="o">|</span>
</span><span class='line'> <span class="n">num</span> <span class="o">=</span> <span class="n">func</span><span class="o">[</span><span class="n">x</span><span class="o">].</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="mi">1000</span> <span class="o">.</span><span class="n">.</span> <span class="mi">9999</span><span class="p">)</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="n">num</span><span class="p">)</span>
</span><span class='line'> <span class="k">if</span> <span class="n">x</span> <span class="o">==</span> <span class="mi">8</span>
</span><span class='line'> <span class="vg">$type8</span> <span class="o"><<</span> <span class="n">num</span>
</span><span class='line'> <span class="k">else</span>
</span><span class='line'> <span class="vg">$nums</span><span class="o">[</span><span class="n">num</span> <span class="o">/</span> <span class="mi">100</span><span class="o">]</span> <span class="o"><<</span> <span class="o">[</span><span class="n">num</span><span class="p">,</span> <span class="n">x</span><span class="o">]</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">break</span> <span class="k">if</span> <span class="n">num</span> <span class="o">></span> <span class="mi">9999</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">solve</span><span class="p">(</span><span class="n">num_set</span><span class="p">,</span> <span class="n">types</span><span class="p">)</span>
</span><span class='line'> <span class="k">if</span> <span class="n">types</span><span class="o">.</span><span class="n">size</span> <span class="o">==</span> <span class="mi">6</span>
</span><span class='line'> <span class="k">if</span> <span class="n">num_set</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span> <span class="o">/</span> <span class="mi">100</span> <span class="o">==</span> <span class="n">num_set</span><span class="o">[-</span><span class="mi">1</span><span class="o">]</span> <span class="o">%</span> <span class="mi">100</span>
</span><span class='line'> <span class="nb">puts</span> <span class="n">num_set</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="ss">:+</span><span class="p">)</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">else</span>
</span><span class='line'> <span class="vg">$nums</span><span class="o">[</span><span class="n">num_set</span><span class="o">[-</span><span class="mi">1</span><span class="o">]</span> <span class="o">%</span> <span class="mi">100</span><span class="o">].</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">n</span><span class="o">|</span>
</span><span class='line'> <span class="k">unless</span> <span class="n">types</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="n">n</span><span class="o">[</span><span class="mi">1</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'> <span class="n">solve</span><span class="p">(</span><span class="n">num_set</span> <span class="o">+</span> <span class="o">[</span><span class="n">n</span><span class="o">[</span><span class="mi">0</span><span class="o">]]</span><span class="p">,</span> <span class="n">types</span> <span class="o">+</span> <span class="o">[</span><span class="n">n</span><span class="o">[</span><span class="mi">1</span><span class="o">]]</span><span class="p">)</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="vg">$type8</span><span class="o">.</span><span class="n">each</span> <span class="p">{</span> <span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="n">solve</span><span class="p">(</span><span class="o">[</span><span class="n">e</span><span class="o">]</span><span class="p">,</span> <span class="o">[</span><span class="mi">8</span><span class="o">]</span><span class="p">)</span> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Project Euler 124的解答]]></title>
<link href="http://white1033.github.com/blog/2012/09/30/project-euler-solution-124/"/>
<updated>2012-09-30T13:29:00+08:00</updated>
<id>http://white1033.github.com/blog/2012/09/30/project-euler-solution-124</id>
<content type="html"><![CDATA[<p><a href="http://projecteuler.net/problem=124">題目</a>是這樣說的,有一個函數叫做<code>rad</code>,它是去運算一個整數的所有的質因數的乘積。題目要問的就是請我們對<code>1~100000</code>中的數字用rad的大小做排序(如果rad一樣大,就再用數字的大小去做排序)之後,問排序後第10000的數字是誰?</p>
<h2>妳被迷惑了嗎?</h2>
<p>其實這題我們並不用去真的對數字去做排序,甚至也不用有個大質數表來做質因數分解:p</p>
<p>我的想法就是去推廣 <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes</a>,但是不是將數字給刪除,反倒是將它建成一個表,去記錄她被哪些質數給整除過,然後在用rad的數字去建一個表,將rad值一樣的數字存入,最後在去算第10000的數字是誰就好了:D</p>
<!-- more -->
<p>附上程式碼:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1">#!/usr/bin/env ruby</span>
</span><span class='line'>
</span><span class='line'><span class="n">limit</span><span class="p">,</span> <span class="n">target</span> <span class="o">=</span> <span class="mi">100_000</span><span class="p">,</span> <span class="mi">10_000</span>
</span><span class='line'><span class="n">rad</span> <span class="o">=</span> <span class="no">Hash</span><span class="o">.</span><span class="n">new</span> <span class="p">{</span> <span class="o">|</span><span class="n">h</span><span class="p">,</span> <span class="n">k</span><span class="o">|</span> <span class="n">h</span><span class="o">[</span><span class="n">k</span><span class="o">]</span> <span class="o">=</span> <span class="mi">1</span> <span class="p">}</span>
</span><span class='line'><span class="n">cache</span> <span class="o">=</span> <span class="no">Hash</span><span class="o">.</span><span class="n">new</span> <span class="p">{</span> <span class="o">|</span><span class="n">h</span><span class="p">,</span> <span class="n">k</span><span class="o">|</span> <span class="n">h</span><span class="o">[</span><span class="n">k</span><span class="o">]</span> <span class="o">=</span> <span class="o">[]</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="mi">2</span> <span class="o">.</span><span class="n">.</span> <span class="n">limit</span><span class="p">)</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">x</span><span class="o">|</span>
</span><span class='line'> <span class="k">if</span> <span class="n">rad</span><span class="o">[</span><span class="n">x</span><span class="o">]</span> <span class="o">==</span> <span class="mi">1</span>
</span><span class='line'> <span class="n">x</span><span class="o">.</span><span class="n">step</span><span class="p">(</span><span class="n">limit</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="o">|</span><span class="n">y</span><span class="o">|</span> <span class="n">rad</span><span class="o">[</span><span class="n">y</span><span class="o">]</span> <span class="o">*=</span> <span class="n">x</span> <span class="p">}</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="n">cache</span><span class="o">[</span><span class="n">rad</span><span class="o">[</span><span class="n">x</span><span class="o">]]</span> <span class="o"><<</span> <span class="n">x</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">cache</span><span class="o">.</span><span class="n">each_value</span> <span class="k">do</span> <span class="o">|</span><span class="n">v</span><span class="o">|</span>
</span><span class='line'> <span class="k">if</span> <span class="n">target</span> <span class="o"><=</span> <span class="n">v</span><span class="o">.</span><span class="n">size</span>
</span><span class='line'> <span class="nb">puts</span> <span class="n">v</span><span class="o">[</span><span class="n">target</span> <span class="o">-</span> <span class="mi">2</span><span class="o">]</span> <span class="c1">#ignore 1 </span>
</span><span class='line'> <span class="k">break</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="n">target</span> <span class="o">-=</span> <span class="n">v</span><span class="o">.</span><span class="n">size</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>這樣子就可以很快的算出答案了:D</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Project Euler 113的解答]]></title>
<link href="http://white1033.github.com/blog/2012/09/19/project-euler-solution-113/"/>
<updated>2012-09-19T22:19:00+08:00</updated>
<id>http://white1033.github.com/blog/2012/09/19/project-euler-solution-113</id>
<content type="html"><![CDATA[<p><a href="http://projecteuler.net/problem=113">題目</a>是這樣說的,如果一個數字從左到右的各個位數是不嚴格遞增的話,那我們就說這種數字叫做遞增數,像是<code>134468</code>。反之,如果一個數字從左到右的各個位數是不嚴格遞減的話,那我們就說這種數字叫做遞減數,像是<code>66420</code>。如果一個數字從左到右的各個位數字,不是屬於上述的兩種狀況之一的話,我們就稱它是彈性數(Bouncy Number)。</p>
<p>現在問題來了,想要請問小於<code>10^100</code>中的數字中,有多少不是彈性數?</p>
<h2>高中排列組合</h2>
<p>好吧,這題其實不是太難,如果妳的高中數學沒有忘的話:)</p>
<p>遞增數的個數是<code>C(109, 9) - 1</code>,遞減數的個數是<code>C(110, 10) - 101</code>,又遞增又遞減的有<code>9 * 100</code>個,所以就只要寫個binomial的函數就可以了:D</p>
<!-- more -->
<p>附上程式碼:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='perl'><span class='line'><span class="c1">#!/usr/bin/env perl</span>
</span><span class='line'><span class="k">use</span> <span class="n">v5</span><span class="mf">.12</span><span class="p">;</span>
</span><span class='line'><span class="k">use</span> <span class="n">warnings</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">sub </span><span class="nf">binomial</span> <span class="p">{</span>
</span><span class='line'> <span class="k">use</span> <span class="n">bigint</span><span class="p">;</span>
</span><span class='line'> <span class="k">my</span> <span class="p">(</span> <span class="nv">$r</span><span class="p">,</span> <span class="nv">$n</span><span class="p">,</span> <span class="nv">$k</span> <span class="p">)</span> <span class="o">=</span> <span class="p">(</span> <span class="mi">1</span><span class="p">,</span> <span class="nv">@_</span> <span class="p">);</span>
</span><span class='line'> <span class="nv">$r</span> <span class="o">=</span> <span class="nv">$r</span> <span class="o">*</span> <span class="p">(</span> <span class="nv">$n</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">-</span> <span class="nv">$_</span> <span class="p">)</span> <span class="o">/</span> <span class="nv">$_</span> <span class="k">foreach</span> <span class="mi">1</span> <span class="o">..</span> <span class="nv">$k</span><span class="p">;</span>
</span><span class='line'> <span class="k">return</span> <span class="nv">$r</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">my</span> <span class="nv">$n</span> <span class="o">=</span> <span class="mi">100</span><span class="p">;</span>
</span><span class='line'><span class="n">say</span> <span class="n">binomial</span><span class="p">(</span> <span class="nv">$n</span> <span class="o">+</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">9</span> <span class="p">)</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">binomial</span><span class="p">(</span> <span class="nv">$n</span> <span class="o">+</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">10</span> <span class="p">)</span> <span class="o">-</span> <span class="mi">101</span> <span class="o">-</span> <span class="mi">9</span> <span class="o">*</span> <span class="nv">$n</span>
</span></code></pre></td></tr></table></div></figure>
<p>所以只要善用數學,題目還是可以用很簡單的方法解出來的:D</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Project Euler 90的解答]]></title>
<link href="http://white1033.github.com/blog/2012/09/09/project-euler-solution-090/"/>
<updated>2012-09-09T22:29:00+08:00</updated>
<id>http://white1033.github.com/blog/2012/09/09/project-euler-solution-090</id>
<content type="html"><![CDATA[<p><a href="http://projecteuler.net/problem=90">題目</a>是這樣說的,有兩個六面骰子,同一個骰子的六個面的數字都不相同(但是不同顆可以有相同的數字)
然後骰子上來的數字都是來自<code>0~9</code>當中,問的是有幾種不同的骰子組合可以組出<code>01, 04, 09, ..., 81</code>,也就是所有的小於一百的平方數。</p>
<p>不過有一點要注意的就是說,題目提到可以將<code>6</code>和<code>9</code>混用,所以會比較多組答案。</p>
<h2>硬爆才是王道</h2>
<p>這題最直接的想法就是將所有的骰子的組合給拿出來檢驗,如果可以組出所有的平方數的話,就讓答案加一。</p>
<p>總之,實做上不難,讓我們直接來看程式碼吧!</p>
<!-- more -->
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1">#!/usr/bin/env ruby</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">update_array</span><span class="p">(</span><span class="n">ary</span><span class="p">)</span>
</span><span class='line'> <span class="n">flag6</span> <span class="o">=</span> <span class="n">ary</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
</span><span class='line'> <span class="n">flag9</span> <span class="o">=</span> <span class="n">ary</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="mi">9</span><span class="p">)</span>
</span><span class='line'> <span class="k">if</span> <span class="n">flag6</span> <span class="o">&&</span> <span class="n">flag9</span> <span class="o">||</span> <span class="o">!</span><span class="n">flag6</span> <span class="o">&&</span> <span class="o">!</span><span class="n">flag9</span>
</span><span class='line'> <span class="n">ary</span>
</span><span class='line'> <span class="k">elsif</span> <span class="n">flag6</span>
</span><span class='line'> <span class="n">ary</span> <span class="o"><<</span> <span class="mi">9</span>
</span><span class='line'> <span class="k">return</span> <span class="n">ary</span>
</span><span class='line'> <span class="k">elsif</span> <span class="n">flag9</span>
</span><span class='line'> <span class="n">ary</span> <span class="o"><<</span> <span class="mi">6</span>
</span><span class='line'> <span class="k">return</span> <span class="n">ary</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">square_dice</span> <span class="o">=</span>
</span><span class='line'> <span class="o">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="o">]</span><span class="p">,</span> <span class="o">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">4</span><span class="o">]</span><span class="p">,</span> <span class="o">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">9</span><span class="o">]</span><span class="p">,</span> <span class="o">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">6</span><span class="o">]</span><span class="p">,</span> <span class="o">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">5</span><span class="o">]</span><span class="p">,</span> <span class="o">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">6</span><span class="o">]</span><span class="p">,</span> <span class="o">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">9</span><span class="o">]</span><span class="p">,</span> <span class="o">[</span><span class="mi">6</span><span class="p">,</span> <span class="mi">4</span><span class="o">]</span><span class="p">,</span> <span class="o">[</span><span class="mi">8</span><span class="p">,</span> <span class="mi">1</span><span class="o">]]</span>
</span><span class='line'><span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'>
</span><span class='line'><span class="o">[*</span><span class="mi">0</span> <span class="o">.</span><span class="n">.</span> <span class="mi">9</span><span class="o">].</span><span class="n">combination</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="o">.</span><span class="n">to_a</span><span class="o">.</span><span class="n">combination</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">ary1</span><span class="p">,</span> <span class="n">ary2</span><span class="o">|</span>
</span><span class='line'> <span class="n">ary1</span> <span class="o">=</span> <span class="n">update_array</span><span class="p">(</span><span class="n">ary1</span><span class="p">)</span>
</span><span class='line'> <span class="n">ary2</span> <span class="o">=</span> <span class="n">update_array</span><span class="p">(</span><span class="n">ary2</span><span class="p">)</span>
</span><span class='line'> <span class="n">flag</span> <span class="o">=</span> <span class="kp">true</span>
</span><span class='line'> <span class="n">square_dice</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="o">|</span>
</span><span class='line'> <span class="k">if</span> <span class="o">!</span><span class="p">(</span><span class="n">ary1</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="n">y</span><span class="p">)</span> <span class="o">&&</span> <span class="n">ary2</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="n">x</span><span class="p">))</span> <span class="o">&&</span>
</span><span class='line'> <span class="o">!</span><span class="p">(</span><span class="n">ary1</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">&&</span> <span class="n">ary2</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="n">y</span><span class="p">))</span>
</span><span class='line'> <span class="n">flag</span> <span class="o">=</span> <span class="kp">false</span>
</span><span class='line'> <span class="k">break</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="n">ans</span> <span class="o">+=</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">flag</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="nb">puts</span> <span class="n">ans</span>
</span></code></pre></td></tr></table></div></figure>
<p>實際上跑起來也是挺快的:P 看來因為組合數沒有很大的關係:)</p>
<p>BTW,這題是我在ProjectEuler上解的第一百五十題,呼~~</p>
<p>又往前多進了一步了:D</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Project Euler 92的解答]]></title>
<link href="http://white1033.github.com/blog/2012/09/01/project-euler-solution-092/"/>
<updated>2012-09-01T19:27:00+08:00</updated>
<id>http://white1033.github.com/blog/2012/09/01/project-euler-solution-092</id>
<content type="html"><![CDATA[<p><a href="http://projecteuler.net/problem=92">題目</a>是這樣說的,妳只要將一個數字的各位數字平方後相加, 然後妳就一直重複這樣做,要嘛會停在1,不然就會掉到一個由89開頭的迴圈中(是有沒有這麼神奇!?)。</p>
<p>問題就是要去問說,在一千萬以下,有多少數字會跑到89?</p>
<h2>直觀的做法</h2>
<p>由於經過一次操作過後,數字和一定不會超過567,所以我們可以做一個表,然後去看在0到567中間的數字會停在1或者是89。之後在去檢查小於一千萬裡面的數,有多少會掉到會回到89的數裡面,就可以算出來答案了。</p>
<p>可是寫出來的程式跑的不是很快,讓人心情不是很好,所以就想了另一個演算法。</p>
<!-- more -->
<h2>改進過的做法</h2>
<p>後來想想,如果像是<code>1234567</code>的<code>7!</code>種組合,其實經過一次操作後,都會回到同一個數字,可是我之前的做法就會重複算了<code>7!</code>次,就會慢了許多,因為一直重複算一樣的東西。</p>
<p>所以新的做法,我是先去算出小於一千萬的數字經過一次操作後,所有可能的數字還有她們的組合數,然後再去看她們有多少會跑到89。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
</pre></td><td class='code'><pre><code class='perl'><span class='line'><span class="c1">#!/usr/bin/env perl</span>
</span><span class='line'><span class="k">use</span> <span class="n">v5</span><span class="mf">.12</span><span class="p">;</span>
</span><span class='line'><span class="k">use</span> <span class="n">warnings</span><span class="p">;</span>
</span><span class='line'><span class="k">use</span> <span class="nn">List::</span><span class="n">Util</span> <span class="sx">qw(sum)</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">sub </span><span class="nf">to_1or89</span> <span class="p">{</span>
</span><span class='line'> <span class="k">my</span> <span class="nv">$x</span> <span class="o">=</span> <span class="nb">shift</span><span class="p">;</span>
</span><span class='line'> <span class="k">while</span> <span class="p">(</span> <span class="nv">$x</span> <span class="o">!=</span> <span class="mi">89</span> <span class="o">&&</span> <span class="nv">$x</span> <span class="o">!=</span> <span class="mi">1</span> <span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nv">$x</span> <span class="o">=</span> <span class="n">sum</span><span class="p">(</span> <span class="nb">map</span> <span class="p">{</span> <span class="nv">$_</span> <span class="o">*</span> <span class="nv">$_</span> <span class="p">}</span> <span class="nb">split</span> <span class="sr">//</span><span class="p">,</span> <span class="nv">$x</span> <span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="k">return</span> <span class="nv">$x</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">my</span> <span class="nv">%hash</span> <span class="o">=</span> <span class="p">(</span> <span class="mi">0</span> <span class="o">=></span> <span class="mi">1</span> <span class="p">);</span>
</span><span class='line'><span class="k">my</span> <span class="nv">@squares</span> <span class="o">=</span> <span class="p">(</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">25</span><span class="p">,</span> <span class="mi">36</span><span class="p">,</span> <span class="mi">49</span><span class="p">,</span> <span class="mi">64</span><span class="p">,</span> <span class="mi">81</span> <span class="p">);</span>
</span><span class='line'><span class="k">foreach</span> <span class="p">(</span> <span class="mi">0</span> <span class="o">..</span> <span class="mi">6</span> <span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">my</span> <span class="nv">%tmp</span><span class="p">;</span>
</span><span class='line'> <span class="k">while</span> <span class="p">(</span> <span class="k">my</span> <span class="p">(</span> <span class="nv">$k</span><span class="p">,</span> <span class="nv">$v</span> <span class="p">)</span> <span class="o">=</span> <span class="nb">each</span> <span class="nv">%hash</span> <span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nv">$tmp</span><span class="p">{</span> <span class="nv">$k</span> <span class="o">+</span> <span class="nv">$_</span> <span class="p">}</span> <span class="o">+=</span> <span class="nv">$v</span> <span class="k">foreach</span> <span class="nv">@squares</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="nv">%hash</span> <span class="o">=</span> <span class="nv">%tmp</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">my</span> <span class="nv">%ans</span> <span class="o">=</span> <span class="p">(</span> <span class="mi">1</span> <span class="o">=></span> <span class="mi">0</span><span class="p">,</span> <span class="mi">89</span> <span class="o">=></span> <span class="mi">0</span> <span class="p">);</span>
</span><span class='line'><span class="nb">delete</span> <span class="nv">$hash</span><span class="p">{</span><span class="mi">0</span><span class="p">};</span>
</span><span class='line'><span class="k">while</span> <span class="p">(</span> <span class="k">my</span> <span class="p">(</span> <span class="nv">$k</span><span class="p">,</span> <span class="nv">$v</span> <span class="p">)</span> <span class="o">=</span> <span class="nb">each</span> <span class="nv">%hash</span> <span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nv">$ans</span><span class="p">{</span> <span class="n">to_1or89</span><span class="p">(</span><span class="nv">$k</span><span class="p">)</span> <span class="p">}</span> <span class="o">+=</span> <span class="nv">$v</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="n">say</span> <span class="nv">$ans</span><span class="p">{</span><span class="mi">89</span><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>這樣改寫後,速度有十分大的提升啊,不到一秒就算完了,看來之後這種題目還是多花一點心思想一下好了。</p>
<h3>其他資訊</h3>
<p>Happy Number:<a href="http://en.wikipedia.org/wiki/Happy_number">http://en.wikipedia.org/wiki/Happy_number</a></p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[玩Ruby Warrior學Ruby]]></title>
<link href="http://white1033.github.com/blog/2012/08/04/ruby-warrior/"/>
<updated>2012-08-04T17:19:00+08:00</updated>
<id>http://white1033.github.com/blog/2012/08/04/ruby-warrior</id>
<content type="html"><![CDATA[<p>最近看到了這個<a href="http://vimeo.com/groups/rubytw/videos/29884753">影片</a>,上面談到了一個遊戲叫做<a href="https://github.com/ryanb/ruby-warrior/">Ruby Warrior</a>,說是可以藉由玩這個遊戲來學習<a href="http://www.ruby-lang.org/en/">Ruby</a>。</p>
<p>玩遊戲學Ruby,聽起來挺不錯的,不過要怎麼開始?</p>
<h2>開始玩吧!</h2>
<p>首先當然要把遊戲先裝起來啊!!先把Ruby Warrior的gem給裝一下</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>gem install rubywarrior
</span></code></pre></td></tr></table></div></figure>
<p>再來執行<code>rubywarrior</code>後,就可以新增帳號開始玩了!</p>
<!-- more -->
<h2>該怎麼玩啊?</h2>
<p>遊戲主要是藉由去修改<code>player.rb</code>,然後去執行<code>rubywarrior</code>後,看看妳寫的程式是否可以過關:)</p>
<p>一開始你會發現你的<code>player.rb</code>長的像這樣</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Player</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">play_turn</span><span class="p">(</span><span class="n">warrior</span><span class="p">)</span>
</span><span class='line'> <span class="c1"># your code goes here</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>十分的乾淨… 不過幸好,第一關也是很簡單,只要一直向前走,就可以過關了:D
就像下面的這樣:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Player</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">play_turn</span><span class="p">(</span><span class="n">warrior</span><span class="p">)</span>
</span><span class='line'> <span class="n">warrior</span><span class="o">.</span><span class="n">walk!</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>這樣就可以過第一關了:D</p>
<p>不過我想這個遊戲的最大樂趣應該就是靠自己寫的程式過關吧:p
所以我也不多分享程式碼了,如果有卡關的再一同討論吧:D</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[初解八皇后問題]]></title>
<link href="http://white1033.github.com/blog/2012/03/24/first-glance-on-eight-queens-problem/"/>
<updated>2012-03-24T21:05:00+08:00</updated>
<id>http://white1033.github.com/blog/2012/03/24/first-glance-on-eight-queens-problem</id>
<content type="html"><![CDATA[<p>這個其實是個很有名的問題,可是我直到最近才好好的(?)把這個題目給好好的做了一下。看來有人問問題還是比較有壓力快快的解出問題XD</p>
<h2>從西洋棋說起</h2>
<p>這是一個由西洋棋來的問題,問說是否可以找到一種放置八個皇后的方法,使得八個皇后不會相互吃到對方,也就是說整個棋盤會被八個皇后給蓋住。</p>
<p>在軍中的時候,其實有跟資工的同梯談過這個問題,他說這種題目通常有兩種重要的取向,一個是最快解出一組解,另一個是最快解出所有的解。</p>
<p>不過因為我用的語言不是C/C++,感覺也不用跟別人爭什麼最快,就開開心心的解就好了。</p>
<!-- more -->
<h2>八城堡問題?</h2>
<p>我在公車上想這個問題的時候,第一個跑入腦中的就是「八城堡問題」。我得說,可能真的找不到這個名字的問題,不過我覺得是個很好的簡化。</p>
<p>其實我們做的事情就是將皇后換成了城堡,不過這個問題就簡單很多了,因為只要是一個一到八的排列就是解答了。例如給出[1, 2, 3, 4, 5, 6, 7, 8],就對應到(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8)這八個點,很明顯就是個八城堡的解。我們得到了一組八城堡的解,也就快得到一組八皇后的解了,因為我們只要去看檢查對角線的方向,是不是有蓋到對方,就可以知道是不是一組八皇后的解了。</p>
<h2>解法</h2>
<p>有了想法,實現就不難,下面附上我的ruby寫法。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1">#!/usr/bin/env ruby</span>
</span><span class='line'><span class="nb">require</span> <span class="s2">"set"</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">layout</span><span class="p">(</span><span class="n">perm</span><span class="p">)</span>
</span><span class='line'> <span class="p">(</span><span class="mi">0</span> <span class="o">.</span><span class="n">.</span><span class="o">.</span> <span class="mi">8</span><span class="p">)</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">i</span><span class="o">|</span>
</span><span class='line'> <span class="n">perm</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">x</span><span class="o">|</span>
</span><span class='line'> <span class="nb">print</span> <span class="n">i</span> <span class="o">==</span> <span class="n">x</span> <span class="p">?</span> <span class="s1">' Q'</span> <span class="p">:</span> <span class="s1">' .'</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="nb">puts</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="nb">Array</span><span class="p">(</span><span class="mi">0</span> <span class="o">.</span><span class="n">.</span><span class="o">.</span> <span class="mi">8</span><span class="p">)</span><span class="o">.</span><span class="n">permutation</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">perm</span><span class="o">|</span>
</span><span class='line'> <span class="k">if</span> <span class="nb">Array</span><span class="p">(</span><span class="mi">0</span> <span class="o">.</span><span class="n">.</span><span class="o">.</span> <span class="mi">8</span><span class="p">)</span><span class="o">.</span><span class="n">map</span> <span class="p">{</span> <span class="o">|</span><span class="n">i</span><span class="o">|</span> <span class="n">perm</span><span class="o">[</span><span class="n">i</span><span class="o">]</span> <span class="o">-</span> <span class="n">i</span> <span class="p">}</span><span class="o">.</span><span class="n">to_set</span><span class="o">.</span><span class="n">size</span> <span class="o">==</span> <span class="mi">8</span> <span class="o">&&</span>
</span><span class='line'> <span class="nb">Array</span><span class="p">(</span><span class="mi">0</span> <span class="o">.</span><span class="n">.</span><span class="o">.</span> <span class="mi">8</span><span class="p">)</span><span class="o">.</span><span class="n">map</span> <span class="p">{</span> <span class="o">|</span><span class="n">i</span><span class="o">|</span> <span class="n">perm</span><span class="o">[</span><span class="n">i</span><span class="o">]</span> <span class="o">+</span> <span class="n">i</span> <span class="p">}</span><span class="o">.</span><span class="n">to_set</span><span class="o">.</span><span class="n">size</span> <span class="o">==</span> <span class="mi">8</span>
</span><span class='line'> <span class="n">layout</span><span class="p">(</span><span class="n">perm</span><span class="p">)</span>
</span><span class='line'> <span class="k">break</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>果然是很輕鬆就解決了XD,希望還有寫出下一個更快速的解法的機會:P</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Project Euler 358的解答]]></title>
<link href="http://white1033.github.com/blog/2012/02/25/project-euler-solution-358/"/>
<updated>2012-02-25T14:47:00+08:00</updated>
<id>http://white1033.github.com/blog/2012/02/25/project-euler-solution-358</id>
<content type="html"><![CDATA[<p>這題是在去設定Cluster的時候看到的,發現這題蠻簡單的,所以回家的時候就順手解了它。</p>
<p><a href="http://projecteuler.net/problem=358">題目</a>是這樣說的,有一種數叫做<a href="http://goo.gl/XVOjK">cyclic number</a>,仔細的性質大家就去看一下<a href="http://en.wikipedia.org/wiki/Main_Page">Wikipedia</a>上的介紹吧,這裡就不多加說明了。那麼題目要找的是一個開頭是<code>00000000137</code>,而結尾是<code>56789</code>的一個唯一的cyclic number,然後請我們算出它的數字和。</p>
<h2>從142857說起</h2>
<p>這個數字我是在高中的時候第一次聽到的,當時聽到的時候真是十分的驚訝,原來這世界上有這麼美的數字。</p>
<p>後來在學校的BBS裡面發現有很多更深入的討論,所以也對這種類型的數字有了一點點初步的了解。</p>
<p>BTW,在我大學的時候參加迎新宿營的時候,剛剛好也出了類似的題目,所以也就順手秒殺了XD,據說當時嚇到很多學長姐,因為他們本來想用這題來拖點時間的,結果沒有想到人算不如天算啊XDDD</p>
<!-- more -->
<h2>回到正題</h2>
<p>那麼這題的解題關鍵是什麼呢?其實每一個cyclic number都會有一個跟它對應的分數(如果妳不知道,可以看看上面wiki的連結,裡面應該是會有講才是。),像是<code>142857</code>對應的就是<code>1/7</code>:p</p>
<p>所以現在問題就變成找一個質數<code>p</code>使得下面的事情成立:</p>
<ul>
<li><code>1/p</code>它前面的位數是<code>0.00000000137</code></li>
<li><code>(10^(p - 1) - 1) / p</code>的後面幾位是<code>56789</code></li>
<li>再來我們只要去算<code>9 * (p - 1) / 2</code>就會是它的數字和了(wtf!?)</li>
</ul>
<p>前兩點聽起來很合理,但是第三點是什麼?為什麼這樣就可以算出來數字和呢?
原因是這樣的,我們知道<code>1/p</code>的循環節長度是<code>p-1</code>,而又因為<code>1/p + (p - 1)/p = 1 = 0.999...</code>,所以我們知道數字和就是<code>9 * (p - 1) / 2</code>,是不是很漂亮呢:p</p>
<p>下面附上程式碼:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="c">#!/usr/bin/env python</span>
</span><span class='line'><span class="kn">from</span> <span class="nn">euler</span> <span class="kn">import</span> <span class="n">is_prime</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">is_primitive</span><span class="p">(</span><span class="n">num</span><span class="p">,</span> <span class="n">prime</span><span class="p">):</span>
</span><span class='line'> <span class="n">tmp</span> <span class="o">=</span> <span class="n">prime</span> <span class="o">-</span> <span class="mi">1</span>
</span><span class='line'> <span class="n">idx</span> <span class="o">=</span> <span class="mi">2</span>
</span><span class='line'> <span class="k">while</span> <span class="n">idx</span><span class="o">**</span><span class="mi">2</span> <span class="o"><=</span> <span class="n">tmp</span><span class="p">:</span>
</span><span class='line'> <span class="k">if</span> <span class="n">tmp</span> <span class="o">%</span> <span class="n">idx</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
</span><span class='line'> <span class="k">if</span> <span class="nb">pow</span><span class="p">(</span><span class="n">num</span><span class="p">,</span> <span class="n">idx</span><span class="p">,</span> <span class="n">prime</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">or</span> <span class="nb">pow</span><span class="p">(</span><span class="n">num</span><span class="p">,</span> <span class="n">tmp</span> <span class="o">/</span> <span class="n">idx</span><span class="p">,</span> <span class="n">prime</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
</span><span class='line'> <span class="k">return</span> <span class="bp">False</span>
</span><span class='line'> <span class="n">idx</span> <span class="o">+=</span> <span class="mi">1</span>
</span><span class='line'> <span class="k">return</span> <span class="bp">True</span>
</span><span class='line'>
</span><span class='line'><span class="n">upper_bound</span> <span class="o">=</span> <span class="mi">729927007</span>
</span><span class='line'><span class="n">lower_bound</span> <span class="o">=</span> <span class="mi">724709891</span>
</span><span class='line'>
</span><span class='line'><span class="k">for</span> <span class="n">num</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="n">lower_bound</span><span class="p">,</span> <span class="n">upper_bound</span><span class="p">,</span> <span class="mi">100000</span><span class="p">):</span>
</span><span class='line'> <span class="k">if</span> <span class="n">is_prime</span><span class="p">(</span><span class="n">num</span><span class="p">)</span> <span class="ow">and</span> <span class="n">is_primitive</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="n">num</span><span class="p">):</span>
</span><span class='line'> <span class="k">print</span><span class="p">((</span><span class="n">num</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="mi">9</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<p>真是美麗的一題,謝謝Project Euler:)</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Project Euler 206的解答]]></title>
<link href="http://white1033.github.com/blog/2012/01/29/project-euler-solution-206/"/>
<updated>2012-01-29T01:46:00+08:00</updated>
<id>http://white1033.github.com/blog/2012/01/29/project-euler-solution-206</id>
<content type="html"><![CDATA[<p>其實是單純覺得這題蠻簡單的,而且一下就寫完了,所以就順手把它寫一下。</p>
<p><a href="http://projecteuler.net/problem=206">題目</a>是要找出一個唯一的正整數,使得它的平方長得要像<code>1_2_3_4_5_6_7_8_9_0</code>這種形式。因爲數字是平方數,不難判斷最後是<code>00</code>結尾,所以要判斷的地方就更少了一點。解决的方法是直接採用暴力法,從上界往回找,直到符合這個形式。</p>
<!-- more -->
<p>程式碼:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1">#!/usr/bin/env ruby</span>
</span><span class='line'>
</span><span class='line'><span class="n">upper_bound</span> <span class="o">=</span> <span class="mi">19_293_949_596_979_899</span>
</span><span class='line'><span class="n">upper_bound_sqrt</span> <span class="o">=</span> <span class="p">(</span><span class="no">Math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">upper_bound</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">to_i</span>
</span><span class='line'>
</span><span class='line'><span class="k">while</span> <span class="kp">true</span>
</span><span class='line'> <span class="k">break</span> <span class="k">if</span> <span class="p">(</span><span class="n">upper_bound_sqrt</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">to_s</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="sr">/1.2.3.4.5.6.7.8.9/</span><span class="p">)</span>
</span><span class='line'> <span class="n">upper_bound_sqrt</span> <span class="o">-=</span> <span class="mi">2</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="nb">puts</span> <span class="n">upper_bound_sqrt</span> <span class="o">*</span> <span class="mi">10</span>
</span></code></pre></td></tr></table></div></figure>
<p>本來還想對做平方的數字做最佳化,其實奇數也只有3跟9的平方尾數會是9,但是没有最佳化的程式就跑得夠快了,所以就懶得最佳化了:p</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[有趣的猜數字謎題]]></title>
<link href="http://white1033.github.com/blog/2012/01/25/guessing-number-puzzle/"/>
<updated>2012-01-25T14:46:00+08:00</updated>
<id>http://white1033.github.com/blog/2012/01/25/guessing-number-puzzle</id>
<content type="html"><![CDATA[<p>這種題目感覺就是百出不厭,一些公司的面試題也會出現,不過解起來也是十分有趣:D</p>
<h2>題目</h2>
<p>老師心中想了一個兩位數。他把這兩位數的兩個數字和告訴了A,把這個兩位數的正因數個數告訴了B。</p>
<p>A:我不知道。</p>
<p>B:我也不知道, 但我知道是否為偶數。</p>
<p>A:我知道了。</p>
<p>B:我也知道了。</p>
<p>請問此兩位數是誰?</p>
<p>以前自己做這種題目,都是拿著筆一個一個慢慢淘汰,不過今天要選擇用把我的解題邏輯寫成程式碼,讓電腦幫我做淘汰這件事情:p</p>
<!-- more -->
<p>附上我的程式碼跟解題邏輯:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1">#!/usr/bin/env ruby</span>
</span><span class='line'>
</span><span class='line'><span class="vg">$digit_sum_hash</span> <span class="o">=</span> <span class="no">Hash</span><span class="o">.</span><span class="n">new</span> <span class="p">{</span> <span class="o">|</span><span class="nb">hash</span><span class="p">,</span> <span class="n">key</span><span class="o">|</span> <span class="nb">hash</span><span class="o">[</span><span class="n">key</span><span class="o">]</span> <span class="o">=</span> <span class="o">[]</span> <span class="p">}</span>
</span><span class='line'><span class="vg">$div_num_hash</span> <span class="o">=</span> <span class="no">Hash</span><span class="o">.</span><span class="n">new</span> <span class="p">{</span> <span class="o">|</span><span class="nb">hash</span><span class="p">,</span> <span class="n">key</span><span class="o">|</span> <span class="nb">hash</span><span class="o">[</span><span class="n">key</span><span class="o">]</span> <span class="o">=</span> <span class="o">[]</span> <span class="p">}</span>
</span><span class='line'><span class="vg">$num_list</span> <span class="o">=</span> <span class="p">(</span><span class="mi">10</span> <span class="o">.</span><span class="n">.</span> <span class="mi">99</span><span class="p">)</span><span class="o">.</span><span class="n">to_a</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">sum_of_digit</span><span class="p">(</span><span class="n">num</span><span class="p">)</span>
</span><span class='line'> <span class="k">return</span> <span class="n">num</span><span class="o">.</span><span class="n">to_s</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="sr">//</span><span class="p">)</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="o">&</span><span class="ss">:to_i</span><span class="p">)</span><span class="o">.</span><span class="n">inject</span><span class="p">(</span><span class="ss">:+</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">num_of_div</span><span class="p">(</span><span class="n">num</span><span class="p">)</span>
</span><span class='line'> <span class="n">ans</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'> <span class="p">(</span><span class="mi">1</span> <span class="o">.</span><span class="n">.</span> <span class="n">num</span><span class="p">)</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">e</span><span class="o">|</span>
</span><span class='line'> <span class="n">ans</span> <span class="o">+=</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">num</span> <span class="o">%</span> <span class="n">e</span> <span class="o">==</span> <span class="mi">0</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">return</span> <span class="n">ans</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="vg">$num_list</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">e</span><span class="o">|</span>
</span><span class='line'> <span class="vg">$digit_sum_hash</span><span class="o">[</span><span class="n">sum_of_digit</span><span class="p">(</span><span class="n">e</span><span class="p">)</span><span class="o">].</span><span class="n">push</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
</span><span class='line'> <span class="vg">$div_num_hash</span><span class="o">[</span><span class="n">num_of_div</span><span class="p">(</span><span class="n">e</span><span class="p">)</span><span class="o">].</span><span class="n">push</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">first_step</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
</span><span class='line'> <span class="k">return</span> <span class="vg">$digit_sum_hash</span><span class="o">[</span><span class="n">sum_of_digit</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="o">].</span><span class="n">length</span> <span class="o">>=</span> <span class="mi">2</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">second_step</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
</span><span class='line'> <span class="n">subset</span> <span class="o">=</span> <span class="vg">$div_num_hash</span><span class="o">[</span><span class="n">num_of_div</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="o">].</span><span class="n">select</span> <span class="p">{</span> <span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="n">first_step</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'> <span class="k">return</span> <span class="n">subset</span><span class="o">.</span><span class="n">length</span> <span class="o">>=</span> <span class="mi">2</span> <span class="o">&&</span>
</span><span class='line'> <span class="p">(</span><span class="n">subset</span><span class="o">.</span><span class="n">all?</span> <span class="p">{</span> <span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="n">e</span><span class="o">.</span><span class="n">even?</span> <span class="p">}</span> <span class="o">||</span> <span class="n">subset</span><span class="o">.</span><span class="n">all?</span> <span class="p">{</span> <span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="o">!</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">even?</span><span class="p">)</span> <span class="p">})</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">third_step</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
</span><span class='line'> <span class="n">subset</span> <span class="o">=</span> <span class="vg">$digit_sum_hash</span><span class="o">[</span><span class="n">sum_of_digit</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="o">].</span><span class="n">select</span> <span class="p">{</span> <span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="n">second_step</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'> <span class="k">return</span> <span class="p">(</span><span class="n">subset</span><span class="o">.</span><span class="n">length</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">forth_step</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
</span><span class='line'> <span class="n">subset</span> <span class="o">=</span> <span class="vg">$div_num_hash</span><span class="o">[</span><span class="n">num_of_div</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="o">].</span><span class="n">select</span> <span class="p">{</span> <span class="o">|</span><span class="n">e</span><span class="o">|</span>
</span><span class='line'> <span class="n">first_step</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="o">&&</span> <span class="n">third_step</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="k">return</span> <span class="p">(</span><span class="n">subset</span><span class="o">.</span><span class="n">length</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">final_step</span>
</span><span class='line'> <span class="n">subset</span> <span class="o">=</span> <span class="vg">$num_list</span><span class="o">.</span><span class="n">select</span> <span class="p">{</span> <span class="o">|</span><span class="n">e</span><span class="o">|</span>
</span><span class='line'> <span class="n">first_step</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="o">&&</span> <span class="n">second_step</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="o">&&</span>
</span><span class='line'> <span class="n">third_step</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="o">&&</span> <span class="n">forth_step</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="k">return</span> <span class="n">subset</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="nb">p</span> <span class="n">final_step</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure>
<p>程式碼很多地方没有最佳化,因爲數字都還蠻小的(遮臉),如果有更好的寫法,還請不吝指教一下:D</p>
<p>解題邏輯也很直觀,就照着對話依序删减可能的數字,最後一步就會跑出唯一的解答。</p>