Skip to content

Instantly share code, notes, and snippets.

@Neonik47
Created September 3, 2018 22:40
Show Gist options
  • Save Neonik47/76d5741f84624fb762851cfb6ef17b7f to your computer and use it in GitHub Desktop.
Save Neonik47/76d5741f84624fb762851cfb6ef17b7f to your computer and use it in GitHub Desktop.
def testo(&block)
min_time = nil
total_time = 0
1000.times do
break if total_time > 10
ti1 = Time.now
res = block.call
ti2 = Time.now
if res == -1
min_time = -1
break
end
dt = ti2-ti1
min_time=dt if (min_time||=dt) < dt
total_time += dt
end
printf("%.5f ms\n", min_time*1000)
end
def t1
(2..@n).reduce([0,1]){ |f| f << f.last(2).reduce(:+) }
end
def t2
res = [] and Array.new(@n,0).each_with_index{|x,i| res << (i<2 ? i : res[i-1] + res[i-2])} and res
end
def t3
res = [] and @n.times{|i| res << (i<2 ? i : res[i-1] + res[i-2])} and res
end
def t4
return -1 if @n > 30
f = -> (x){x<2 ? x : f[x-1] + f[x-2]} and (0..@n).map { |e| f[e] }
end
def t5
i = Math.sqrt(5) and f = -> (x){(((((1 + i)/2)**x) - (((1-i)/2)**x)) / i).to_i} and (0..@n).map { |e| f[e] }
rescue FloatDomainError
puts "FloatDomainError: Infinity"
return -1
end
def dd(n)
@n=n
testo {t1}
testo {t2}
testo {t3}
testo {t4}
testo {t5}
end
def y1
(2..@n).reduce([0,1]){ |f| f << f.last(2).reduce(:+) }[0]
end
def y2
res = [] and Array.new(@n,0).each_with_index{|_x,i| res << (i<2 ? i : res[i-1] + res[i-2])} and res[-1]
end
def y3
res = [] and @n.times{|i| res << (i<2 ? i : res[i-1] + res[i-2])} and res[-1]
end
def y4
return -1 if @n > 30
(f = -> (x){x<2 ? x : f[x-1] + f[x-2]})[@n]
end
def y5
i = Math.sqrt(5) and (((((1 + i)/2)**@n) - (((1-i)/2)**@n)) / i).to_i
rescue FloatDomainError
puts "FloatDomainError: Infinity"
return -1
end
def yy(n)
@n=n
testo {y1}
testo {y2}
testo {y3}
testo {y4}
testo {y5}
end
def ttt4(x)
x<2 ? x : ttt4(x-1) + ttt4(x-2)
end
def tty4
(0..@n).map { |e| ttt4(e) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment