diff options
author | Santo Cariotti <sancn@live.com> | 2017-06-11 16:48:12 +0200 |
---|---|---|
committer | Santo Cariotti <sancn@live.com> | 2017-06-11 16:48:12 +0200 |
commit | 7707f7cf59e76186f764264d0c8f4e4f618904f8 (patch) | |
tree | ad7428bb520b33116ea77a46f975c4a0bcd259d9 /ruby | |
parent | 263704d75777429c9cb84416b9b2aa7d6f2f610f (diff) |
first ruby file
Diffstat (limited to 'ruby')
-rw-r--r-- | ruby/lswf.rb | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/ruby/lswf.rb b/ruby/lswf.rb new file mode 100644 index 0000000..57228ba --- /dev/null +++ b/ruby/lswf.rb @@ -0,0 +1,44 @@ +fib = [] +fib.push 1 +fib.push 1 + +def fibonacci(n, fib) + for i in 2...n do + fib.push fib[i-1] + fib[i-2] + + break if fib[i] > n + end + + return i +end + +File.open('input.txt', 'r') do |fin| + N = fin.gets.to_i +end + +lst = fibonacci(N, fib) + +fib = fib.reverse + +somma = 0 +seq = [] + +for i in 0...fib.length-1 do + potSomma = somma + fib[i] + + if potSomma < N + somma = potSomma + seq << 1 + else + seq << 0 + end +end + +seq << 1 +seq = seq.reverse + +seq.pop if N == 1 || N > 4 + +File.open('output.txt', 'w') do |fout| + seq.each {|c| fout.print c} +end |