-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
864 lines (534 loc) · 27.1 KB
/
index.html
File metadata and controls
864 lines (534 loc) · 27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
<meta charset="utf-8">
<title>a bystander's contribution</title>
<meta name="author" content="Ray Wu">
<meta name="description" content="Today I tried to do bundle install: $ bundle install
Fetching gem metadata from http://rubygems.org/.
Error Bundler::HTTPError during request to …">
<!-- http://t.co/dKP3o1e -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="canonical" href="http://raywu.github.com/">
<link href="/favicon.png" rel="icon">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
<script src="/javascripts/modernizr-2.0.js"></script>
<script src="/javascripts/ender.js"></script>
<script src="/javascripts/octopress.js" type="text/javascript"></script>
<link href="/atom.xml" rel="alternate" title="a bystander's contribution" type="application/atom+xml">
<!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
<link href="http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
</head>
<body >
<header role="banner"><hgroup>
<h1><a href="/">a bystander's contribution</a></h1>
<h2>Ruby, Rails & Life as a Student</h2>
</hgroup>
</header>
<nav role="navigation"><ul class="subscription" data-subscription="rss">
<li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
</ul>
<form action="http://google.com/search" method="get">
<fieldset role="search">
<input type="hidden" name="q" value="site:raywu.github.com" />
<input class="search" type="text" name="q" results="0" placeholder="Search"/>
</fieldset>
</form>
<ul class="main-navigation">
<li><a href="/">Ruby Blog</a></li>
<li><a href="/blog/archives">Archives</a></li>
<li><a href="/about">About</a></li>
<li><a href="http://blog.raywu.co/">Tumblr Blog</a></li>
</ul>
</nav>
<div id="main">
<div id="content">
<div class="blog-index">
<article>
<header>
<h1 class="entry-title"><a href="/blog/2012/10/20/til-rubygems-dot-org-is-partially-blocked-in-china/">TIL RubyGems.org Is Partially Blocked in China</a></h1>
<p class="meta">
<time datetime="2012-10-20T14:02:00+08:00" pubdate data-updated="true">Oct 20<span>th</span>, 2012</time>
</p>
</header>
<div class="entry-content"><p>Today I tried to do <em>bundle install</em>:</p>
<div><script src='https://gist.github.com/3922248.js?file=ConsoleRubyGems.rb'></script>
<noscript><pre><code>$ bundle install
Fetching gem metadata from http://rubygems.org/.
Error Bundler::HTTPError during request to dependency API
Fetching full source index from http://rubygems.org/
^C
Quitting...</code></pre></noscript></div>
<p>According to the mirror site below, it is because RubyGems.org is being hosted on Amazon S3 AFAIK, and Amazon S3 is partially blocked (I have no way of knowing for sure).</p>
<p>A friend showed me a neat trick, which every Rails engineer first learns being behind the <em>Great Firewall of China</em>:</p>
<p>Replace the <strong>source</strong> with this line in GemFile: <strong>source “http://ruby.taobao.org”</strong></p>
<p>Go visit the <a href="http://ruby.taobao.org/">mirror site</a></p>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/2012/10/02/project-euler-problem-11-ruby/">Project Euler Problem 11: Ruby</a></h1>
<p class="meta">
<time datetime="2012-10-02T02:28:00+08:00" pubdate data-updated="true">Oct 2<span>nd</span>, 2012</time>
</p>
</header>
<div class="entry-content"><h3><a href="http://projecteuler.net/problem=11">Problem 11:</a></h3>
<p><em>In the 2020 grid below, four numbers along a diagonal line have been marked in red.</em></p>
<p>08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08<br/>
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00<br/>
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65<br/>
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91<br/>
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80<br/>
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50<br/>
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70<br/>
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21<br/>
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72<br/>
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95<br/>
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92<br/>
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57<br/>
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58<br/>
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40<br/>
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66<br/>
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69<br/>
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36<br/>
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16<br/>
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54<br/>
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48</p>
<p><em>The product of these numbers is 26 63 78 14 = 1788696.</em></p>
<p><em>What is the greatest product of four adjacent numbers in any direction
(up, down, left, right, or diagonally) in the 2020 grid?</em></p>
<h3> My attempt </h3>
<div><script src='https://gist.github.com/3812900.js?file=problem11.rb'></script>
<noscript><pre><code>grid_string = <<EOS
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
EOS
grid = []
grid_string.each_line do |line|
grid << line.split #=> array of rows
end
grid.map! do |row|
row.map! do |x|
x.to_i
end
end
$max = 0
def compute(array)
product = []
i = 0
for line in array
while line[i+3]
product << line[i] * line[i+1] * line[i+2] * line[i+3]
i += 1
end
i = 0
end
product.max #=> outputs max
end
def max?(x)
if $max < x
$max = x
end
end
# ==HORIZONTAL==
max?(compute(grid))
# ==VERTICAL==
num = []
arr_col = []
grid[i].each_index do |x|
while grid[i]
num << grid[i][x]
i += 1
end
arr_col << num
i = 0
num = []
end
max?(compute(arr_col))
# ==DIAGNAL Upper Left to Bottom Right==
num = []
arr_utb = []
grid.each_index do |i|
grid[i].each_index do |x|
while grid[i]
num << grid[i][x]
i += 1
x += 1
end
arr_utb << num.compact
num = []
end
end
max?(compute(arr_utb))
# ==DIAGNAL Bottom Left to Upper Right==
num = []
arr_btu = []
grid.each_index do |i|
grid[i].each_index do |x|
while grid[20-i]
num << grid[20-i][x]
i += 1
x += 1
break if (20 - i) < 0 # this prevents the collection to continue recursively
end
arr_btu << num.compact
num = []
end
end
max?(compute(arr_btu))
puts "The highest product is #{$max}"</code></pre></noscript></div>
<p>My solution heavily relies on proper scoping.</p>
<p>I used the <strong>$max</strong> global variable so the method <strong>max?(x)</strong> can access and edit the same <strong>$max</strong>.</p>
<p><strong>There are also a few neat tricks that I learned:</strong></p>
<ul>
<li>I didn’t know about <a href="http://en.wikipedia.org/wiki/Here_document#Ruby">ruby here doc</a>; this will come in handy on Project Euler for inputs</li>
<li>Here Doc really helped visualizing the grid in nested arrays</li>
<li>Array method <a href="http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-each_index">each_index</a> is super useful to loop through each item in the array</li>
<li>Around <strong>Line #103</strong>, it was an epiphany to use the inverse count <strong>( 20 - i )</strong></li>
<li><strong>Line #107</strong> is also crucial; otherwise, the output array will not stop at edge of the grid (will continue on and pick up the thread on the opposite side of the grid)</li>
</ul>
<h3> Conclusion </h3>
<p>The diagnal parts have much room for improvement. The current codes are not optimal. They also loop through a ton codes (<strong>arr_utb.uniq</strong> shed much light). Otherwise, I am pretty happy with the refactoring jobs I did up top for <strong>compute(array)</strong> and <strong>max?(x)</strong>.</p>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/2012/03/20/break-if-found/">Break if Found</a></h1>
<p class="meta">
<time datetime="2012-03-20T22:32:00+08:00" pubdate data-updated="true">Mar 20<span>th</span>, 2012</time>
</p>
</header>
<div class="entry-content"><p>Ruby blocks are awesome. I find myself using a lot of enumerables & enumerators.</p>
<p>Not knowing how to break out of a block is a problem I ran into a lot.</p>
<p>In Problem 9 of <a href="http://www.projecteuler.net">Project Euler</a>, the answer has to satisfy the condition of x ^ 2 + y ^ 2 = c ^ 2.</p>
<div><script src='https://gist.github.com/2093533.js?file=problem9false.rb'></script>
<noscript><pre><code># Problem 9
# A Pythagorean triplet is a set of three natural numbers, a b c, for which,
# a^2 + b^2 = c^2
# For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
# There exists exactly one Pythagorean triplet for which a + b + c = 1000.
# Find the product abc.
found = []
(1..1E3).each { |x|
(1..1E3).each { |y|
puts "a = #{x}, b = #{y}, c = #{1000 - x - y}, product = #{x * y * (1000 - x - y)}"
found << [x, y, (1000 - x - y)] if x ** 2 + y ** 2 == (1000 - x - y) ** 2
}
}
p found.inspect</code></pre></noscript></div>
<p>This nested block describes the logic needed for the answer.
Printing out ‘found’ displays the answer in an array. This took a while.</p>
<h3> Using Break </h3>
<div><script src='https://gist.github.com/2093533.js?file=problem9.rb'></script>
<noscript><pre><code> found = false
(1..1E3).each { |x|
(1..1E3).each { |y|
puts "a = #{x}, b = #{y}, c = #{1000 - x - y}, product = #{x * y * (1000 - x - y)}"
found = true if x ** 2 + y ** 2 == (1000 - x - y) ** 2
break if found
}
break if found
}</code></pre></noscript></div>
<p>In this scenario, by setting ‘found = false’ (line 1), it gives the block a heads up to break out of the loop, when ‘found’ returns true.</p>
<p>This dramatically reduces the time to run the script.</p>
<h3> Conclusion </h3>
<p>Blocks are awesome; but knowing when to break out of a loop is double awesome.</p>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/2012/03/18/omniauth-environment-handling/">OmniAuth Environment Handling</a></h1>
<p class="meta">
<time datetime="2012-03-18T12:01:00+08:00" pubdate data-updated="true">Mar 18<span>th</span>, 2012</time>
</p>
</header>
<div class="entry-content"><p>OmniAuth is very powerful, and it’s dramatically improved after 2.0 came out. I only started to feel more comfortable with its magic after the third time using it.</p>
<p>One thing I didn’t do before, was to set separate authentication keys
for development and production environments.</p>
<p>Doing this on the <a href="http://tourious.co/">Tourious</a> app made life so much easier!</p>
<h3>Here are the files</h3>
<p>This is the standard OmniAuth set up on initializer. Instead of passing authentication keys directly, set them to constants.</p>
<div><script src='https://gist.github.com/2076187.js?file=omniauth.rb'></script>
<noscript><pre><code># resides at config/initializer/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, TWITTER_KEY, TWITTER_SECRET
provider :facebook, FACEBOOK_KEY, FACEBOOK_SECRET
end
</code></pre></noscript></div>
<p>Pass the constants with a set of keys from Twitter and Facebook for
development environment:</p>
<div><script src='https://gist.github.com/2076187.js?file=development.rb'></script>
<noscript><pre><code>Tourious::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
TWITTER_KEY = 'Development Keys'
TWITTER_SECRET = 'Development Secret'
FACEBOOK_KEY = 'Development Keys'
FACEBOOK_SECRET = 'Development Secret'
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger
config.active_support.deprecation = :log
# Only use best-standards-support built into browsers
config.action_dispatch.best_standards_support = :builtin
# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
config.active_record.auto_explain_threshold_in_seconds = 0.5
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
end
</code></pre></noscript></div>
<p>Create another set of Twitter and Facebook authentication keys strictly for production:</p>
<div><script src='https://gist.github.com/2076187.js?file=production.rb'></script>
<noscript><pre><code>Tourious::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
TWITTER_KEY = 'Production Keys'
TWITTER_SECRET = 'Production Secret'
FACEBOOK_KEY = 'Production Keys'
FACEBOOK_SECRET = 'Production Secret'
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
# Generate digests for assets URLs
config.assets.digest = true
# Defaults to Rails.root.join("public/assets")
# config.assets.manifest = YOUR_PATH
# Specifies the header that your server uses for sending files
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# See everything in the log (default is :info)
# config.log_level = :debug
# Prepend all log lines with the following tags
# config.log_tags = [ :subdomain, :uuid ]
# Use a different logger for distributed setups
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Use a different cache store in production
# config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
# Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false
# Enable threaded mode
# config.threadsafe!
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
# config.active_record.auto_explain_threshold_in_seconds = 0.5
end
</code></pre></noscript></div>
<h3> Conclusion </h3>
<p>This way, I can also set different call-back URL on Twitter and Facebook, and never have to touch the settings again.</p>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/2012/03/17/hello-world/">Hello World!</a></h1>
<p class="meta">
<time datetime="2012-03-17T20:38:00+08:00" pubdate data-updated="true">Mar 17<span>th</span>, 2012</time>
</p>
</header>
<div class="entry-content"><p>A few months earlier, I started tackling <a
href="http://www.projecteuler.net">Project Euler</a>. Here is one of the
lessons I learned:</p>
<h3>Avoid Infinite Loops</h3>
<p>Yes, Infinite Loop. I thought I had you pinned to the wall, but I was wrong; I didn’t even realize what I got myself into. No, not until my buddy <a href="http://www.github.com/igrabes">Ian Grabill</a> pointed it out.</p>
<h3>Project Euler’s #3 problem:</h3>
<p> What is the largest prime factor of the number 600851475143 ?
The way I started out with this problem, was to get all the factors for the number in question. My approach was to wrap the number in question into a method as an argument:</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">factor</span><span class="p">(</span><span class="n">num</span><span class="p">)</span>
</span><span class='line'> <span class="p">(</span><span class="mi">2</span><span class="o">.</span><span class="n">.num</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">n</span><span class="o">|</span> <span class="k">yield</span> <span class="n">n</span> <span class="k">if</span> <span class="n">num</span><span class="o">/</span><span class="n">n</span> <span class="o">*</span> <span class="n">n</span> <span class="o">==</span> <span class="n">num</span> <span class="p">}</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>This seemed straight forward enough. Boom, donzo.
I ran the script to test it out with 40, and factor(40) worked beautifully. Then I tried it withfactor(600,851,475,143); 10 seconds’ went by, nothing yet. 120 seconds, nothing still. Next thing I knew, my computer was screeching noises I hadn’t ever heard before. Ctrl-C, Ctrl-C, Ctrl-C, abort, abort!!!</p>
<p>What happened?
While 40 spat out 8 numbers that are all divisble to 40, who knows how many of these factors are out there for 600851475143.
So, now what?</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">factor</span><span class="p">(</span><span class="n">num</span><span class="p">,</span> <span class="n">limit</span><span class="p">)</span>
</span><span class='line'> <span class="p">(</span><span class="mi">1</span><span class="o">.</span><span class="n">.limit</span><span class="p">)</span><span class="o">.</span><span class="n">map</span> <span class="k">do</span> <span class="o">|</span><span class="n">f</span><span class="o">|</span>
</span><span class='line'> <span class="n">num</span> <span class="o">/</span> <span class="n">f</span>
</span><span class='line'> <span class="k">yield</span> <span class="n">f</span> <span class="k">if</span> <span class="n">num</span> <span class="o">/</span> <span class="n">f</span> <span class="o">*</span> <span class="n">f</span> <span class="o">==</span> <span class="n">num</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>After talking to Ian at 2am ET about this ungodly problem of mine, he pointed me to this neat little trick. Ian had discovered that by dividing each number within a range of (1..limit) into the num (in this case 600851475143), would help you find all the factors within a contained range.</p>
<p>For instance, factor(600851475143, 10000) would only look for all the factors of 600851475143 that were within the range of (1..10000). This made my computer happy.</p>
<h3>Conclusion</h3>
<p>Granted, had my MacBook Pro had more computing power, and my memories a gizzillion GBs, I could have just left the script run until it got all the factors for 600851475143.</p>
<p>Okay, okay, IT’S NOT AN INFINITE LOOP. But my point is, if you have an inkling that you will run into this kind of technical problems (Hello Houston!), put a cap on it.</p>
</div>
</article>
<div class="pagination">
<a href="/blog/archives">Blog Archives</a>
</div>
</div>
<aside class="sidebar">
<section>
<h1>Recent Posts</h1>
<ul id="recent_posts">
<li class="post">
<a href="/blog/2012/10/20/til-rubygems-dot-org-is-partially-blocked-in-china/">TIL RubyGems.org is partially blocked in China</a>
</li>
<li class="post">
<a href="/blog/2012/10/02/project-euler-problem-11-ruby/">Project Euler Problem 11: Ruby</a>
</li>
<li class="post">
<a href="/blog/2012/03/20/break-if-found/">Break if found</a>
</li>
<li class="post">
<a href="/blog/2012/03/18/omniauth-environment-handling/">OmniAuth Environment Handling</a>
</li>
<li class="post">
<a href="/blog/2012/03/17/hello-world/">Hello World!</a>
</li>
</ul>
</section>
<section>
<h1>GitHub Repos</h1>
<ul id="gh_repos">
<li class="loading">Status updating…</li>
</ul>
<a href="https://github.com/raywu">@raywu</a> on GitHub
<script type="text/javascript">
$.domReady(function(){
if (!window.jXHR){
var jxhr = document.createElement('script');
jxhr.type = 'text/javascript';
jxhr.src = '/javascripts/libs/jXHR.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(jxhr, s);
}
github.showRepos({
user: 'raywu',
count: 0,
skip_forks: true,
target: '#gh_repos'
});
});
</script>
<script src="/javascripts/github.js" type="text/javascript"> </script>
</section>
<section>
<h1>Latest Tweets</h1>
<ul id="tweets">
<li class="loading">Status updating…</li>
</ul>
<script type="text/javascript">
$.domReady(function(){
getTwitterFeed("raywu", 4, false);
});
</script>
<script src="/javascripts/twitter.js" type="text/javascript"> </script>
<a href="http://twitter.com/raywu" class="twitter-follow-button" data-show-count="false">Follow @raywu</a>
</section>
<section class="googleplus googleplus-hidden">
<h1>
<a href="https://plus.google.com/102573216451517008752?rel=author">
<img src="http://www.google.com/images/icons/ui/gprofile_button-32.png" width="32" height="32">
Google+
</a>
</h1>
</section>
</aside>
</div>
</div>
<footer role="contentinfo"><p>
Copyright © 2013 - Ray Wu -
<span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>
</p>
</footer>
<script type="text/javascript">
var disqus_shortname = 'raywu';
var disqus_script = 'count.js';
(function () {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/' + disqus_script;
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
}());
</script>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#appId=212934732101925&xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<script type="text/javascript">
(function() {
var script = document.createElement('script'); script.type = 'text/javascript'; script.async = true;
script.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(script, s);
})();
</script>
<script type="text/javascript">
(function(){
var twitterWidgets = document.createElement('script');
twitterWidgets.type = 'text/javascript';
twitterWidgets.async = true;
twitterWidgets.src = 'http://platform.twitter.com/widgets.js';
document.getElementsByTagName('head')[0].appendChild(twitterWidgets);
})();
</script>
</body>
</html>