kenneth.kin.lum
BAN USERIn Ruby (2.0 or above):
def f(n)
(1..n).inject {|a,b| a^b }
end
or shorter version:
def f(n)
(1..n).inject(:^)
end
In Ruby:
# recursion requirement, this returns an array of array, which is
# array of all the possible steps to take, for n step staircase, and
# k step possible each time
def all_ways(n, k)
# this is tricky because it is 1 way if n = 0, which is "doing nothing"
# it is not 0 way, it is 1 way
return [[]] if n == 0
result = []
1.upto(k) do |n_steps|
if n >= n_steps
all_ways(n - n_steps, k).each do |possible_way|
result.push([n_steps] + possible_way)
end
end
end
return result
end
all_ways(4, 2).each {|i| puts i.join(", ")}
In Ruby:
def f200
result = 0
factor = 0.5
16.times do
result += factor * f1()
factor /= 2
end
return (result * 200).to_i + 1
end
In Ruby 2.0:
- kenneth.kin.lum January 04, 2016