.-[17:11] asonas:~/dev/tmp/fib (master)
`-% node -v
v0.10.28
.-[17:11] asonas:~/dev/tmp/fib (master)
`-% ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
.-[17:11] asonas:~/dev/tmp/fib (master)
`-% time ruby fib.rb
12586269025
ruby fib.rb 0.04s user 0.03s system 79% cpu 0.083 total
.-[17:11] asonas:~/dev/tmp/fib (master)
`-% time node fib.js
12586269025
node fib.js 0.02s user 0.01s system 96% cpu 0.034 total
-
-
Save asonas/2a06e5858b79d42ccd6e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function fib(n) { | |
| var a = 1, b = 0, p = 0, q = 1, c = n, tmp_1, tmp_2; | |
| while (c > 0) { | |
| if (c % 2 === 0) { | |
| tmp_1 = p * p + q * q; | |
| tmp_2 = (q + 2 * p) * q; | |
| p = tmp_1; | |
| q = tmp_2; | |
| c = c / 2; | |
| } else { | |
| tmp_1 = (a + b) * q + a * p; | |
| tmp_2 = a * q + b * p; | |
| a = tmp_1; | |
| b = tmp_2; | |
| c--; | |
| } | |
| } | |
| return b; | |
| } | |
| var sys = require('sys'); | |
| sys.print(fib(50)+"\n"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Fibonacci | |
| attr_accessor :memo | |
| def initialize | |
| @memo = [0 , 1] | |
| end | |
| def cal(n) | |
| @memo.size.upto(n){|i| @memo[i] = @memo[i - 1] + @memo[i - 2]} | |
| puts @memo[n] | |
| end | |
| end | |
| Fibonacci.new.cal(50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment