2014年9月4日 星期四

[FWD] HTTP 302

Thread Name:
Sample Start: 2014-09-05 12:01:16 CST
Load time: 64
Latency: 64
Size in bytes: 525
Headers size in bytes: 263
Body size in bytes: 262
Sample Count: 1
Error Count: 0
Response code: 302
Response message: Found

Response headers:
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.com.tw/?gfe_rd=cr
Content-Length: 262
Date: Fri, 05 Sep 2014 04:01:17 GMT
Server: GFE/2.0
Alternate-Protocol: 80:quic


HTTPSampleResult fields:
ContentType: text/html; charset=UTF-8
DataEncoding: UTF-8


傳說中的 URL redirection,今天玩 jmeter 發現的。

2014年8月1日 星期五

[ProjectEuler] 423

Problem: http://projecteuler.net/problem=423

Super slow code.

Time complexity: O(n^2 / ln(n))

I need to think another way to solve it.



import bisect
import sys

class ConsecutivePrimeCountingFunction():
    def __init__(self):
        self.primes = []
        self.pos = 0
        self._init_primes()
    
    def _init_primes(self):
        #SIEVE_RANGE = 50100
        SIEVE_RANGE = 50000100
        sieve_visited = [False] * SIEVE_RANGE
        sieve_visited[0] = sieve_visited[1] = True
        for i in range(2, SIEVE_RANGE):
            if sieve_visited[i] is False:
                self.primes.append(i)
                for j in range(i + i, SIEVE_RANGE, i):
                    sieve_visited[j] = True
        print('Total prime count:', len(self.primes))
    
    def get(self, n):
        if n >= self.primes[self.pos]:
            self.pos += 1
        return self.pos

    def reset(self):
        self.pos = 0

    def get_directly(self, n):
        return bisect.bisect(self.primes, n)

class Problem():
    def __init__(self):
        self.mod = 1000000007
        self.consecutive_prime_counting_function = ConsecutivePrimeCountingFunction()
    
    def solve(self):
        for n in [50000000]:
            #print(self.S(n))
            print(self.S_lite(n))
    
    def S_lite(self, L):
        max_count = self.consecutive_prime_counting_function.get_directly(L) + 1
        consecutive_map = [0 for count in range(max_count)]
        consecutive_map[0] = 1
        print('Big data structure |consecutive_map| is ready')
        result = 6 # Trivial case for C(1) = 6
        self.consecutive_prime_counting_function.reset()
        
        for i in range(2, L+1):
            if i % 1000 == 0:
                print('Curr:', i, '=>', result)
            pos = self.consecutive_prime_counting_function.get(i)
            prev = consecutive_map[0]
            consecutive_map[0] = 5 * prev
            for count in range(1, max_count):
                curr = consecutive_map[count]
                if curr == 0 and prev == 0:
                    break
                consecutive_map[count] = curr * 5 + prev
                prev = curr
            result = (result + sum(consecutive_map[:pos + 1]) * 6) % self.mod
        return result

    def C(self, n):
        max_count = self.consecutive_prime_counting_function.get_directly(n) + 1
        consecutive_map = [0 for count in range(max_count)]
        consecutive_map[0] = 1

        for i in range(n - 1):
            next_consecutive_map = [0 for count in range(max_count)]
            next_consecutive_map[0] = consecutive_map[0] * 5
            for count in range(1, max_count):
                next_consecutive_map[count] = consecutive_map[count] * 5 + consecutive_map[count - 1]
            consecutive_map = next_consecutive_map
        result = sum(consecutive_map) * 6
        return result

    def S(self, L):
        return sum([self.C(n) for n in range(1, L + 1)]) % self.mod

def main():
    problem = Problem()
    problem.solve()
    
if __name__ == '__main__':
    sys.exit(main())

2014年6月11日 星期三

[Redis] Redis Event Library

Document: http://redis.io/topics/internals-rediseventlib

Source: https://github.com/antirez/redis/

[ProjectEuler] Problem 473

大原則:碰到 Fibonacci 的問題都不會太困難,一定會有什麼遞迴。

如果問的數字很大,可以用 Q-matrix 把 complexity time O(N) 降為 O(logN)



不過還是有幾題卡住就是了,喵的。

2014年6月8日 星期日

2014年5月12日 星期一

[GIT] Changing git remote origin

git remote rm origin 
git remote add origin https://username@bitbucket.org/your.new.repo 

2014年5月5日 星期一

2014年5月4日 星期日

[ProjectEuler] Notes on Problem #435


Lemma: F_n(x) = (f_{n+1} x^{n+1} + f_{n} x^{n+2} - x) / (x^2 + x - 1)

F_n(x) 
= sum_{0 <= i <= n} f_i x^i 
= sum_{1 <= i <= n} f_i x^i ...... (A)

x F_n(x) 
= sum_{0 <= i <= n} f_{i} x^{i+1} 
= sum_{1 <= i+1 <= n+1} f_{i+1 - 1} x^{i+1}
= sum_{1 <= j <= n+1} f_{j-1} x^{j}
= sum_{1 <= i <= n} f_{i-1} x^{i} + f_n x^{n+1} ...... (B)
                     
(A) + (B) implies that (x+1) F_n(x) = sum_{1 <= i <= n} f_{i+1} x^i + f_n x^{n+1}

Multiply x on both sides:

x(x+1) F_n(x) = sum_{1 <= i <= n} f_{i+1} x^{i+1} + f_n x^{n+2}
= sum_{2 <= i+1 <= n+1} f_{i+1} x^{i+1} + f_n x^{n+2}
= sum_{2 <= i <= n+1} f_i x^i + f_n x^{n+2}
= sum_{1 <= i <= n} f_i x^i - f_1 x + f_{n+1} x^{n+1} + f_n x^{n+2}
= F_n(x) + f_{n+1} x^{n+1} + f_n x^{n+2} - x
(x^2 + x - 1) F_n(x) = f_{n+1} x^{n+1} + f_n x^{n+2} - x

So, F_n(x) = (f_{n+1} x^{n+1} + f_n x^{n+2} - x) / (x^2 + x - 1)

不過這離解題還有一大段的距離。

2014年4月28日 星期一

[ProjectEuler] Problem #320


結果我還是用筆電硬幹 ((當然有一些技巧))

跑了五小時 Macbook Pro 完全性的在發燙呀!

還好答案是對的,有時候很懷疑電腦跑太久答案會算錯。

2014年4月22日 星期二

[ProjectEuler] TODO: Dynamic programming

Problem #217

Apply dynamic programming on n, not on 10^n ((Solved))



Problem #427

Apply dynamic programming on n-sequence (?)



Problem #413

Apply dynamic programming (?)



應該吧,總之就是再多想想好了。

2014年4月21日 星期一

[ProjectEuler] Problem #379

不知道怎麼解。

參考資料:

http://2000clicks.com/mathhelp/NumberTh05DivisorsTau.aspx

但我要計算的是 Σ Tau(k^2) where k = 1 to N.  N = 10^6 or 10^12. 

2014年3月25日 星期二

[FWD] google-guice


看起來會比 Spring framework 輕量化。

https://code.google.com/p/google-guice/wiki/Motivation?tm=6



Direct constructor calls 第一個解法就是使用 factory pattern,但違反了 Dependency Injection 原則,這個也與 single responsibility 有關,以上面連結為例,BillingService 究竟不該負責產生CreditCardProcessor 以及 TransactionLog。

    bind(TransactionLog.class).to(DatabaseTransactionLog.class);
    bind(CreditCardProcessor.class).to(PaypalCreditCardProcessor.class);
    bind(BillingService.class).to(RealBillingService.class);

所以自然而然就會有這樣子的設計: binding,頗舒服的設計。



2014年3月11日 星期二

[PE] Problem 202


這題可以找到公式。

不過找到公式還差一段路,可以靠 Problem 1 (!!) 的想法來做。

2014年2月20日 星期四

[PE] Problem 407 & 451 & 457


Problem #451 is harder than #407.

However, if we know how to solve #457, #451 and #457 are the same.


Level 9

ChopinPlover

Level 9


Solved 243 out of 459 problems


Levels Completed

Solve
250
problems
Solve
275
problems
Solve
300
problems
Solve
325
problems
Solve
350
problems
Solve
375
problems
Solve
400
problems
Solve
425
problems


Problems Solved

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