Collatz Chain Algorithm RUBY - ruby

I am trying to populate an array according to the Collatz sequence. The constraints for the sequence are as follows:
positive integers:
n → n/2 (n is even)
n → 3n + 1 (n is odd)
Example Output
3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
Ideally, I wanted to construct a recursive call that would populate the array according to the constraints of the sequence. However, I believe my logic for the recursive call is extremely flawed. The intended behavior is to iterate over the nested array, manipulating only the last element of each sub array until the element reaches 1. I am trying to build my understanding of recursion and would appreciate any suggestions on how to fix this problem.
def collatzSum(maxNumber)
sequenceHash = Hash.new(0)
i = maxNumber
until i == 0 do
if i.even?
sequenceHash[i] = [(i), (i / 2)]
elsif i.odd? && i != 1
sequenceHash[i] = [(i), (3 * i + 1)]
elsif i == 1
sequenceHash[i] = [i]
end
i -= 1
end
p sequenceHash
helper_method recursion. Method should take in hash values and iterate according to if statements.
=begin
desired output
hash = {5=>[5,16, 8, 4, 2,1],
4=>[4,2,1],
3=>[3,10,5,16,8,4,2,1],
2=>[2,1],
1=>[1]}
=end
Code:
collatzChain = lambda do |k|
j = 0
k = j[-1]
until k == 1 do
if k.even?
sequenceHash[k] << (k / 2)
elsif k.odd?
sequenceHash[k] << (3 * k + 1)
end
end
j += 1
end
collatzChain.call(sequenceHash.values)
sequenceHash
end
collatzSum(5)

So you mention that you wanted a recursive algorithm, your current approach looks iterative to me. To be recursive, you need to call the method you're in with values closer and closer to a base condition and then, once you hit the base condition, you return back out, up the call chain building up your return values. So, for the Collatz sequence a recursive approach would look like:
def build_collatz_chain(max_number)
return_value = [max_number]
# our base condition is when the number passed in is equal to 1, so
# when we get 1 as the max_number, we'll return an array looking like
# [1]
return return_value if max_number == 1
if max_number.even?
# here, with an even max_number, we'll recurse and call our method
# again, passing in the new max_number, which is the current
# max_number / 2.
return_value + build_collatz_chain(max_number / 2)
else
# same as above, but we're odd, so we'll recurse with 3 * max_number + 1
return_value + build_collatz_chain(3 * max_number + 1)
end
end
and now when we call this with a value of 5, what will end up happening is something like:
call build_collatz_chain(5)
call build_collatz_chain(16)
call build_collatz_chain(8)
call build_collatz_chain(4)
call build_collatz_chain(2)
call build_collatz_chain(1)
We have hit the base condition! return with [1]
return from 2 with [2, 1]
return from 4 with [4, 2, 1]
return from 8 with [8, 4, 2, 1]
return from 16 with [16, 8, 4, 2, 1]
return from 5 with [5, 16, 8, 4, 2, 1]
So, now if you want a hash of all numbers up to the passed in max_number with their Collatz chains as values you can use a helper to call this for each value, up to max (this helper is iterative, but could be made recursive...exercise for the viewer if you want it recursive):
def collatz_sum(max_number)
{ }.tap do |sequence_hash|
max_number.downto(1) do |i|
sequence_hash[i] = build_collatz_chain(i)
end
end
end
and then when you call collatz_sum(5) you get back:
{5=>[5, 16, 8, 4, 2, 1], 4=>[4, 2, 1], 3=>[3, 10, 5, 16, 8, 4, 2, 1], 2=>[2, 1], 1=>[1]}
The reason your approach is iterative is in the collatzChain lambda, you are setting a value (j) and then incrementing it and just looping through until k is equal to 1. It's also an infinite loop because you initially set k as:
j = 0
k = j[-1]
and so k == 0, and then you iterate until k == 1 and then you never update what the value of k is again.

It's not clear that a recursive operation is necessary here since this seems to be a straightforward mapping between a value x and f(x). By switching to a simple array output you can achieve what you want with:
def collatz_sum(max)
(2..max).map do |i|
[
i,
if (i.even?)
i / 2
else
3 * i + 1
end
]
end.reverse + [ [ 1 ] ]
end

Related

Find the x amount of factorials starting from 0 using recursion. 5 == [1,1,2,6,24]

I am trying to get the first num factorials.
For example if num is 6 then we would want the first 6 factorials: 1,1,2,6,24,120
Below is my current solution. It always returns an extra factorial. So if num is 6 it returns 7 factorials.
def factorials_rec(num)
return [1] if num == 1
arr = factorials_rec(num-1)
arr << num * arr.last
end
Result [1, 1, 2, 6, 24, 120, 720]
Desired result [1, 1, 2, 6, 24, 120]
I only want 6 factorials but 7 are being returned.
Using recursion, how can I adjust this function to return the desired number of factorials.
I have tried
base case = return [1,1] if num == 2, etc..
arr << num * arr.last unless arr.length >= num, etc...
Try fixing the last line to:
arr << (num-1) * arr.last

Min, Max, Average, and Median of All Possible Sums (Ruby)

I've been using a piece of Ruby code that I found here.
Here's the code:
a = [1, 4, 7, 13]
def add(ary, idx, sum)
(idx...ary.length).each do |i|
add(ary, i+1, sum + ary[i])
end
puts sum
end
add(a, 0, 0)
Thing is, I don't need it to spit out the results of adding all the sums. I need the min, max, median, and average of the sums.
How do I modify this code in order to get them? I'm a total beginner at Ruby. I've been using this code, and then transferring the results to Excel to get the values I want. But it feels like my methods could be more efficient.
Thank you for your help.
EDIT: Expected results - Currently the code spits this out on my screen:
25
12
18
5
21
8
14
1
24
11
17
4
20
7
13
0
I want it to spit out the min, average, median, and max instead:
0
12.5
12.5
25
a = [1, 4, 7, 13]
def all_sums(array)
combination_lengths = (0..array.length)
all_combinations = combination_lengths.flat_map do |c|
array.combination(c).to_a
end
all_combinations.map(&:sum)
end
def print_min_max_avg_med(array)
puts array.min
puts array.max
puts array.sum.to_f / array.length
sorted_arr = array.sort
puts sorted_arr[(array.length - 1) / 2] + sorted_arr[array.length / 2] / 2.0
end
print_min_max_avg_med(all_sums(a))
Ok, instead of outputting the values we can store them in an arrary and use that array for the values you need.
(edited after chewing out by Stefan Pochmann)
a = [1, 4, 7, 13]
def add(ary, idx, sum, results = nil)
unless results
results = []
first_run = true
end
(idx...ary.length).each do |i|
add(ary, i+1, sum + ary[i], results)
end
results << sum
if first_run
puts results.min
puts results.inject(&:+).to_f / results.size
puts (results.sort[((results.size - 1) / 2)] + results.sort[(results.size / 2)]) / 2.0
puts results.max
end
end
add(a, 0, 0)
Alright, after seeing the examples from Pochmann and Bronca, I put this together after googling for a better way to get the median.
a = [1, 4, 7, 13]
def all_sums(array)
combination_lengths = (0..array.length)
all_combinations = combination_lengths.flat_map do |c|
array.combination(c).to_a
end
all_combinations.map(&:sum)
end
def median(array)
sorted = array.sort
len = sorted.length
(sorted[(len - 1) / 2] + sorted[len / 2]) / 2.0
end
def print_min_max_avg_med(array)
puts array.min
puts array.empty? ? 0 : array.sum.to_f / array.length
puts median(array)
puts array.max
end
print_min_max_avg_med(all_sums(a))
I've run a few tests, and it seems to work for both odd and even arrays. Hope this is useful to the future somebody else stuck in my present position.
Thank you everyone who helped.
Min and Max
The min and max are easy.
def min_and_max_of_sums a
return [nil, nil] if a.empty?
negs, nonnegs = a.partition { |n| n < 0 }
[negs.any? ? negs.sum : nonnegs.min, nonnegs.any? ? nonnegs.sum : negs.max]
end
min_and_max_of_sums [1, 4, -5, 7, -8, 13]
#=> [-13, 25]
min_and_max_of_sums [1, 2, 3]
#=> [1, 6]
min_and_max_of_sums [-1, -2, -3]
#=> [-6, -1]
min_and_max_of_sums []
#=> [nil, nil]
Mean
Now consider the calculation of the mean.
If n is the size of the array a, there are 2n combinations of elements of a that contain between 0 and n elements.1 Moreover, there is a 1-1 mapping between each of those combinations and an n-vector of zeros and ones, where the ith element of the n-vector equals 1 if and only if the element ai is included in the combination. Note that there are 2n such n-vectors, one-half containing a 1 in the ith position. This means that one-half of the combinations contain the element ai. As i is arbitrary, it follows that each element of a appears in one-half of the combinations.
The mean of the sums of all elements of all combinations equals T/2n, where T is the sum of the sums of the elements of each combination. Each element ai appears in 2n/2 combinations, so its contribution to T equals (in Ruby terms)
a[i] * 2**(n)/2
As this hold for every element of a, the mean equals
a.sum * (2**(n)/2)/2**(n)
=> a.sum/2
Here's an example. For the array
a = [1, 4, 8]
the mean of the sums would be
a.sum/2
#=> 13/2 => 6.5
If we were to calculate the mean by its definition we would perform the following calculation (and of course get the same return value).
(0 + (1) + (4) + (8) + (1+4) + (1+8) + (4+8) + (1=4+8))/2**3
#=> (4*1 + 4*4 + 4*8)/8
#=> (1 + 4 + 8)/2
#=> 6.5
I will leave the calculating of the median to others.
1 Search for "Sums of the binomial coefficients" here.

Ruby: Recursion and order of execution

The output of the below method is [6,4,2,0,0,2,4,6].
I understand everything up to the array=[6,4,2,0,0] at n-1 with array[4]=0 added at line no. 3. But I'm completely stumped on why the method continues to execute even after line no 3 is executed which should then return [6,4,2,0,0] to the original method call. Even more vexing is that n resets to n=1 and increments to n=2 and n=3...n=3 being the starting argument value passed in the method call.
Also, I'm having major problems understanding recursion in the various possibilities. An answer to this question and a suggestion on a 'recursion for dummies' would be greatly appreciated!
def append(array, n)
return array if n < 0 #base case, how we end this thing
array << n*2 #Line No. 1
append(array, n - 1) #Line No. 2
array << n*2 #Line No. 3
end
append( [], 3)
#output [6,4,2,0,0,2,4,6]
There is nothing mysterious about the order of execution here, and your counter doesn't get incremented. To understand what happens, let's walk through the code line by line. I'll take append([], 2) to make it quicker.
# you call append([], 2)
return array if n < 0
# n >= 0 so we continue
array << n*2
# array is now [4]
append(array, n - 1)
# you call append(array, 1) which will mutate array,
# lets call x what will be appended to it
# array is now [4, x]
array << n*2
# array is now [4, x, 4]
# you get [4, x, 4] as a returned value from the append method
# because after the first line there is no return statement,
# so the return value of the last line is returned
# let's now see what x, that is append(array, 1) is
return array if n < 0
# n >= 0 so we continue
array << n*2
# array is now [4, 2] because at that time, array is [4]
append(array, n - 1)
# you call append(array, 0) which will mutate array,
# lets call y what will be appended to it
# array is now [4, 2, y]
array << n*2
# array is now [4, 2, y, 2]
# this is what you return to the first method invocation
# so we can replace [4, x, 4] with [4, 2, y, 2, 4]
# let's now see what y, that is append(array, 0) is
return array if n < 0
# n >= 0 so we continue
array << n*2
# array is now [4, 2, 0] because at that time, array is [4, 2]
append(array, n - 1)
# you call append(array, -1) which will mutate array,
# lets call z what will be appended to it
# array is now [4, 2, 0, z]
array << n*2
# array is now [4, 2, 0, z, 0]
# this is what you return to the second method invocation
# so we can replace [4, 2, y, 2, 4] with [4, 2, 0, z, 0, 2, 4]
# now in the last invocation, z is nothing because -1 < 0,
# so nothing is appended to the array
# the first method invocation returns [4, 2, 0, 0, 2, 4]
The return statement only returns from its immediate method invocation. The fact that a method is recursive doesn't change that. It will not somehow find the top level invocation of itself and return from it.
If I can give you an advice when working with recursion, it would be to not mutate your arguments. Working with pure functions is much easier and intuitive, especially in this context. This is how your append method would look like without mutations :
def append n
return n < 0 ? [] : [n * 2, append(n - 1), n * 2].flatten
end
and you would call it like this :
array = append(3)
# [6, 4, 2, 0, 0, 2, 4, 6]
This way your array doesn't get mutated and you get a much clearer image of what the method returns.
In case you don't find it clearer, visualize it this way
# append(3)
[6,
# append(2)
[4,
# append(1)
[2,
# append(0)
[0,
# append(-1)
[]
, 0].flatten
, 2].flatten
, 4].flatten
, 6].flatten
The method is invoked many times. Talking about how "the method continues to execute" already demonstrates that you're not thinking about this incorrectly.
Each invocation of the method is completely independent of every other invocation, and each invocation has a unique copy of n with its own value. The method is invoked four times, and each of those four invocation pushes two items into the array, yielding eight total items.
The key to undertanding what is happening is that each method invocation pushes a number onto the array, and then invokes itself, and then pushes another number onto the array. Both "6" entires are pushed by the same method invocation, and they wrap all of the other entries because the recursion happened between the two array << n*2 calls in the method where n was 6.
Consider the following:
def method_a
puts "A start"
method_b
puts "A end"
end
def method_b
puts " B start"
method_c
puts " B end"
end
def method_c
puts " C start"
puts " !!!"
puts " C end"
end
method_a
This is not recursive, but it behaves in a similar manner. The output of this code is:
A start
B start
C start
!!!
C end
B end
A end
This is as opposed to what you seem to expect to see:
A start
B start
C start
!!!
Each function outputs a "start" line, invokes its next function, and then when the nested invocation returns, it outputs its "end" line. This is exactly how your recursive function behaves. Each invocation continues to execute after the nested invocation returns. The local value of n is unchanged, and it gets pushed onto the array a second time.
I think you have the idea that return ends everything, but that is not the case.
Here is what is happening, step by step:
append(array = [], n = 3) # initial call
array << 6 #Line No. 1
append(array = [6], n = 2) #Line No. 2
array << 4 #Line No. 1
append(array = [6,4], n = 1) #Line No. 2
array << 2 #Line No. 1
append(array = [6,4,2], n = 0) #Line No. 2
array << 0 #Line No. 1
append(array = [6,4,2,0], n = -1) #Line No. 2
return array #base case
# but `return` doesn't leave the recursion.
# it only goes up one step in the call stack, like so:
array << 0 #Line No. 3 -> array = [6,4,2,0,0]
array << 2 #Line No. 3 -> array = [6,4,2,0,0,2]
array << 4 #Line No. 3 -> array = [6,4,2,0,0,2,4]
array << 6 #Line No. 3 -> array = [6,4,2,0,0,2,4,6]
I think that Line No. 3 introduced some confusion. If it was simply n*2, you would have seen that in the end it didn't return the array, but instead a Fixnum. Here is a shortened version of the step by step for this case:
append([], 3) # initial call
array << 6; append([6], 2)
array << 4; append([6,4], 1)
array << 2; append([6,4,2], 0)
array << 0; append([6,4,2,0], -1)
return array
0 # result of n*2 (Line No. 3)
2 # result of n*2 (Line No. 3)
4 # result of n*2 (Line No. 3)
6 # result of n*2 (Line No. 3)
#output = 6
On the other hand, if you remove Line No. 3, the last line will be the result from a call to append, which in effect will coincide with the base case.
append([], 3) # initial call
array << 6; append([6], 2)
array << 4; append([6,4], 1)
array << 2; append([6,4,2], 0)
array << 0; append([6,4,2,0], -1)
return array
array # result from the call append([6,4,2], 0) (Line No. 2)
array # result from the call append([6,4], 1) (Line No. 2)
array # result from the call append([6], 2) (Line No. 2)
array # result from the call append([], 3) (Line No. 2)
#output = [6,4,2,0]

How to 'reverse sum' in Ruby?

I have no clue how to call this in correct math-terms. Consider a method which takes two digits:
def num_of_sum(total, group_count)
end
where total is an integer and group_count is an integer.
How would I get a 'nicely' grouped Array of integers of group_count-length which sum up till total.
My spec would look like:
describe "number to sum of" do
it "grabs all numbers" do
expect(num_of_sum(10, 2)).to eq([5,5])
expect(num_of_sum(10, 3)).to eq([3,3,4])
expect(num_of_sum(20, 3)).to eq([6,7,7])
expect(num_of_sum(100, 3)).to eq([33,33,34])
expect(num_of_sum(100, 2)).to eq([50,50])
end
end
I tried this, which works:
def num_of_sum(total, in_groups_of)
result = []
section_count ||= (total.to_f / in_groups_of.to_f).round
while(total > 0)
total -= section_count
if (total - section_count) < 0 && (total + section_count).even?
section_count += total
total -= total
end
result << section_count
end
result
end
But, for instance, this spec doesn't work:
expect(num_of_sum(67,5)).to eq([13,13,13,14,14])
I need the array to contain numbers that are as close to each other as possible. But the array is limited to the length of the group_count.
Does someone know what the mathemetical name for this is, so I can search a bit more accurately?
The mathematical term for this is an integer partition
A more direct approach to this is to observe that if you do integer division (round down) of the total by the number of groups, then your sum would be short by total mod number_of_groups, so you just need to distribute that amount across the array:
def even_partition(total, number_of_groups)
quotient, remainder = total.divmod(number_of_groups)
(number_of_groups-remainder).times.collect {quotient} +
remainder.times.collect { quotient + 1}
end
def n_parts(num, groupcount)
div, mod = num.divmod(groupcount)
Array.new(groupcount-mod, div) + Array.new(mod, div+1)
end
n_parts(100,3) => [33, 33, 34]
Docs to Array.new and Fixnum.divmod
A naive implementation is like this:
Let's take example of (20, 3). You want three numbers as a result.
20 / 3 # => 6
This is your "base" value. Create an array of three sixes, [6, 6, 6]. That'll get you 18. Now you have to distribute remaining 2 as equally as possible. For example, enumerate array elements and increment each one by 1, until you have no value to distribute. Result is [7, 7, 6]. Good enough, I think.
Possible (working) implementation:
def breakdown(total, group_count)
avg_value, extra = total.divmod(group_count)
result = Array.new(group_count, avg_value)
extra.times do |i|
result[i] += 1
end
result
end
breakdown(10, 2) == [5, 5] # => true
breakdown(10, 3) == [4, 3, 3] # => true
breakdown(20, 3) # => [7, 7, 6]
I have no clue how it’s called, but here is a solution:
def num_of_sum sum, count
result = [i = sum / count] * count # prepare an array e.g. [3,3,3] for 10,3
result[sum - i * count..-1] + # these should be left intact
result[0...sum - i * count].map { |i| i + 1 } # these are ++’ed
end
Hope it helps.
Another way:
def floors_then_ceils(n, groups)
floor, ceils = n.divmod(groups)
groups.times.map { |i| (i < groups-ceils) ? floor : floor + 1 }
end
floors_then_ceils(10, 3)
#=> [3, 3, 4]
floors_then_ceils(9, 3)
#=> [3, 3, 3]
Alternatively, groups.times.map... could be replaced with:
Array.new(groups-ceils, floor).concat(Array.new(ceils, floor+1))

should destructive functions extend their classes?

I'm implementing some stuff from CLRS, and the partition() algorithm is causing me a little concern. It's implied (from the consistently vague pseudocode therein, but not stated explicitly) that partition should modify the array it's passed, and return a related scalar value:
PARTITION(A, p, r)
1 x = A[r]
2 i = p -1
3 for j = p to r - 1
4 if A[j] <= x
5 i = i + 1
6 swap A[i], A[j]
7 swap A[i+1], A[r]
8 return i + 1
This is easy to throw together in Ruby, but my question is, since it changes the array it's passed without returning it, should it be a class method like array.do_stuff!()?
irb(main):007:0> ary
=> [0, 3, 0, 2, 1, 2, 2, 2, 4, 4, 2]
irb(main):008:0> partition(ary,2,4)
=> 3
irb(main):009:0> ary
=> [0, 3, 0, 1, 2, 2, 2, 2, 4, 4, 2]
For reference, here is my code:
def partition(my_list, part_start, part_end, pivot = my_list[part_end])
# From CLRS p. 171
# In-place rearrangement of subarrays.
sort_separator = part_start - 1
for loop_ind in (part_start..part_end-1)
if my_list[loop_ind] <= my_list[part_end]
sort_separator += 1
my_list[sort_separator],my_list[loop_ind] =
my_list[loop_ind],my_list[sort_separator]
end
end
my_list[sort_separator+1],my_list[part_end] =
my_list[part_end],my_list[sort_separator+1]
return(sort_separator+1)
end

Resources