ruby beginner help

I am having a lot of trouble wrapping my head around this
I have an array of arrays of prime numbers.
I want to add a each prime number to this array.
example
I have

prime_ary = [[3, 5], [3, 7], [3, 11], [5, 11]]
add_ary = [57, 91, 109]

I would like to get an array of arrays like this

[[3,5, 57], [3, 7, 57], [3, 11, 57], [5, 11, 57], [3, 5, 91], [3, 7, 91], [3, 11, 91] ... ]
There should be 12 sub arrays in the array at the end. I have this function in an array class but it doesn't give me the result I would like. I have been working on this for a few days and feel like I have loosing the ability to think clearly. Any help is appreciated.

def add_ary_prime(numb)
    solution_ary = []
    numb.each do |rannumb|
      solution_ary.push(self.map {|arr| arr << rannumb})
    end
    solution_ary
  end

p prime_ary.add_ary_prime(add_ary)

sincerely,

Derrick

Hey derrick!

You could use the Array#product and flatten methods like so:

prime_array.product(add_array).map(&:flatten)

In short:

#product Gives you the cartesian product of two arrays so [[2, 4]].product([51, 12]) #=> [[[2,4], 51], [[2,4], 12]]
And map(&:flatten) takes each of these [[a,b], c] arrays and turns them into [a, b, c] arrays without flattening the whole

thing.

Hope this helps,
Paul

···

On 09.06.2018 10:19, derrick wrote:

I am having a lot of trouble wrapping my head around this
I have an array of arrays of prime numbers.
I want to add a each prime number to this array.
example
I have

prime_ary = [[3, 5], [3, 7], [3, 11], [5, 11]]
add_ary = [57, 91, 109]

I would like to get an array of arrays like this

[[3,5, 57], [3, 7, 57], [3, 11, 57], [5, 11, 57], [3, 5, 91], [3, 7, 91], [3, 11, 91] ... ]
There should be 12 sub arrays in the array at the end. I have this function in an array class but it doesn't give me the result I would like. I have been working on this for a few days and feel like I have loosing the ability to think clearly. Any help is appreciated.

def add_ary_prime(numb)
solution_ary =
numb.each do |rannumb|
solution_ary.push(self.map {|arr| arr << rannumb})
end
solution_ary
end

p prime_ary.add_ary_prime(add_ary)

sincerely,

Derrick

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Hi, I have been trying to use flatten but I keep getting one big array instead of an array of arrays. I know that flatten can take an argument though. Also, product doesnt seem to scale all that well either. I was using product to combine 5 arrays of 800 prime numbers and it ate up all my memory and killed the script.
I haven't tried your exact method though let me try that first.

Thank you for the suggestion.

···

------------------------------------------------------------------
From:Paul Martensen <paul.martensen@gmx.de>
Time:2018 Jun 9 (Sat) 16:46
To:ruby-talk <ruby-talk@ruby-lang.org>
Subject:Re: ruby beginner help

Hey derrick!

You could use the Array#product and flatten methods like so:

prime_array.product(add_array).map(&:flatten)

In short:
#product Gives you the cartesian product of two arrays so [[2, 4]].product([51, 12]) #=> [[[2,4], 51], [[2,4], 12]]
And map(&:flatten) takes each of these [[a,b], c] arrays and turns them into [a, b, c] arrays without flattening the whole
thing.

Hope this helps,
Paul

On 09.06.2018 10:19, derrick wrote:

I am having a lot of trouble wrapping my head around this
I have an array of arrays of prime numbers.
I want to add a each prime number to this array.
example
I have

prime_ary = [[3, 5], [3, 7], [3, 11], [5, 11]]
add_ary = [57, 91, 109]

I would like to get an array of arrays like this

[[3,5, 57], [3, 7, 57], [3, 11, 57], [5, 11, 57], [3, 5, 91], [3, 7, 91], [3, 11, 91] ... ]
There should be 12 sub arrays in the array at the end. I have this function in an array class but it doesn't give me the result I would like. I have been working on this for a few days and feel like I have loosing the ability to think clearly. Any help is appreciated.

def add_ary_prime(numb)
     solution_ary = []
     numb.each do |rannumb|
       solution_ary.push(self.map {|arr| arr << rannumb})
     end
     solution_ary
   end

p prime_ary.add_ary_prime(add_ary)

sincerely,

Derrick

This code works as expected, the trick is to get a new array like [3, 5,
57] without changing prime_ary.
Using' push' or '<<' changes the array even if you use 'each' iterator.
Instead you can use '+' to join them
into a temp array append it to the solution array.

prime_ary = [[3, 5], [3, 7], [3, 11], [5, 11]]
add_ary = [57, 91, 109]
n = add_ary.size
solution_ary =
n.times do |i|
   prime_ary.each do |j|
     temp = j + [add_ary[i-1]]
     solution_ary << temp
   end
end
p solution_ary

···

On 9 June 2018 at 14:53, derrick <derrick@thecopes.me> wrote:

Hi, I have been trying to use flatten but I keep getting one big array
instead of an array of arrays. I know that flatten can take an argument
though. Also, product doesnt seem to scale all that well either. I was
using product to combine 5 arrays of 800 prime numbers and it ate up all my
memory and killed the script.
I haven't tried your exact method though let me try that first.

Thank you for the suggestion.

------------------------------------------------------------------
From:Paul Martensen <paul.martensen@gmx.de>
Time:2018 Jun 9 (Sat) 16:46
To:ruby-talk <ruby-talk@ruby-lang.org>
Subject:Re: ruby beginner help

Hey derrick!

You could use the Array#product and flatten methods like so:

prime_array.product(add_array).map(&:flatten)

In short:

#product Gives you the cartesian product of two arrays so [[2,
4]].product([51, 12]) #=> [[[2,4], 51], [[2,4], 12]]
And map(&:flatten) takes each of these [[a,b], c] arrays and turns them
into [a, b, c] arrays without flattening the whole

thing.

Hope this helps,
Paul

On 09.06.2018 10:19, derrick wrote:
I am having a lot of trouble wrapping my head around this
I have an array of arrays of prime numbers.
I want to add a each prime number to this array.
example
I have

prime_ary = [[3, 5], [3, 7], [3, 11], [5, 11]]
add_ary = [57, 91, 109]

I would like to get an array of arrays like this

[[3,5, 57], [3, 7, 57], [3, 11, 57], [5, 11, 57], [3, 5, 91], [3, 7, 91],
[3, 11, 91] ... ]
There should be 12 sub arrays in the array at the end. I have this
function in an array class but it doesn't give me the result I would like.
I have been working on this for a few days and feel like I have loosing the
ability to think clearly. Any help is appreciated.

def add_ary_prime(numb)
    solution_ary =
    numb.each do |rannumb|
      solution_ary.push(self.map {|arr| arr << rannumb})
    end
    solution_ary
  end

p prime_ary.add_ary_prime(add_ary)

sincerely,

Derrick

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe> <ruby-talk-request@ruby-lang.org?subject=unsubscribe><http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt; <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
2nd Year UG
Electrical Engineering
IIEST Shibpur

Thank you for your answer Paul it worked great. I had to do some optimization to get rid of duplicates to keep from running out of ram but your solution was the answer.

Derrick

···

------------------------------------------------------------------
发件人:Paul Martensen<paul.martensen@gmx.de>
日 期:2018年06月09日 16:45:41
收件人:<ruby-talk@ruby-lang.org>
主 题:Re: ruby beginner help

Hey derrick!

You could use the Array#product and flatten methods like so:

prime_array.product(add_array).map(&:flatten)

In short:
#product Gives you the cartesian product of two arrays so [[2, 4]].product([51, 12]) #=> [[[2,4], 51], [[2,4], 12]]
And map(&:flatten) takes each of these [[a,b], c] arrays and turns them into [a, b, c] arrays without flattening the whole
thing.

Hope this helps,
Paul

On 09.06.2018 10:19, derrick wrote:

I am having a lot of trouble wrapping my head around this
I have an array of arrays of prime numbers.
I want to add a each prime number to this array.
example
I have

prime_ary = [[3, 5], [3, 7], [3, 11], [5, 11]]
add_ary = [57, 91, 109]

I would like to get an array of arrays like this

[[3,5, 57], [3, 7, 57], [3, 11, 57], [5, 11, 57], [3, 5, 91], [3, 7, 91], [3, 11, 91] ... ]
There should be 12 sub arrays in the array at the end. I have this function in an array class but it doesn't give me the result I would like. I have been working on this for a few days and feel like I have loosing the ability to think clearly. Any help is appreciated.

def add_ary_prime(numb)
     solution_ary = []
     numb.each do |rannumb|
       solution_ary.push(self.map {|arr| arr << rannumb})
     end
     solution_ary
   end

p prime_ary.add_ary_prime(add_ary)

sincerely,

Derrick

Unsubscribe yourself from this mailing list. Why did you subscribe in the first place?

···

------------------------------------------------------------------
发件人:Sarkar Chinmoy<chinmoy_sarkar@yahoo.com>
日 期:2018年06月09日 23:55:04
收件人:Ruby users<ruby-talk@ruby-lang.org>
主 题:Re: ruby beginner help

Please remove me from your mailing list. Thanks

On Saturday, June 9, 2018, 10:49:52 AM CDT, Saumya jeet <promptc3.0@gmail.com> wrote:

This code works as expected, the trick is to get a new array like [3, 5, 57] without changing prime_ary.
Using' push' or '<<' changes the array even if you use 'each' iterator. Instead you can use '+' to join them
into a temp array append it to the solution array.

prime_ary = [[3, 5], [3, 7], [3, 11], [5, 11]]
add_ary = [57, 91, 109]n = add_ary.size
solution_ary = []
n.times do |i|
   prime_ary.each do |j|
     temp = j + [add_ary[i-1]]
     solution_ary << temp
   end
end
p solution_ary

On 9 June 2018 at 14:53, derrick <derrick@thecopes.me> wrote:

Hi, I have been trying to use flatten but I keep getting one big array instead of an array of arrays. I know that flatten can take an argument though. Also, product doesnt seem to scale all that well either. I was using product to combine 5 arrays of 800 prime numbers and it ate up all my memory and killed the script.
I haven't tried your exact method though let me try that first.

Thank you for the suggestion.

------------------------------ ------------------------------ ------
From:Paul Martensen <paul.martensen@gmx.de>
Time:2018 Jun 9 (Sat) 16:46
To:ruby-talk <ruby-talk@ruby-lang.org>
Subject:Re: ruby beginner help

Hey derrick!

You could use the Array#product and flatten methods like so:

prime_array.product(add_array) .map(&:flatten)

In short:
#product Gives you the cartesian product of two arrays so [[2, 4]].product([51, 12]) #=> [[[2,4], 51], [[2,4], 12]]
And map(&:flatten) takes each of these [[a,b], c] arrays and turns them into [a, b, c] arrays without flattening the whole
thing.

Hope this helps,
Paul

On 09.06.2018 10:19, derrick wrote:
I am having a lot of trouble wrapping my head around this
I have an array of arrays of prime numbers.
I want to add a each prime number to this array.
example
I have

prime_ary = [[3, 5], [3, 7], [3, 11], [5, 11]]
add_ary = [57, 91, 109]

I would like to get an array of arrays like this

[[3,5, 57], [3, 7, 57], [3, 11, 57], [5, 11, 57], [3, 5, 91], [3, 7, 91], [3, 11, 91] ... ]
There should be 12 sub arrays in the array at the end. I have this function in an array class but it doesn't give me the result I would like. I have been working on this for a few days and feel like I have loosing the ability to think clearly. Any help is appreciated.

def add_ary_prime(numb)
     solution_ary = []
     numb.each do |rannumb|
       solution_ary.push(self.map {|arr| arr << rannumb})
     end
     solution_ary
   end

p prime_ary.add_ary_prime(add_ ary)

sincerely,

Derrick

Unsubscribe: <mailto:ruby-talk-request@ ruby-lang.org?subject= unsubscribe>
<http://lists.ruby-lang.org/ cgi-bin/mailman/options/ruby- talk>

Unsubscribe: <mailto:ruby-talk-request@ ruby-lang.org?subject= unsubscribe>
<http://lists.ruby-lang.org/ cgi-bin/mailman/options/ruby- talk>

--
2nd Year UG
Electrical Engineering
IIEST Shibpur

This has nothing to do with the scalability of `product` or any
other possible solution. It is inherent in the problem:

There are 800^5 = 327680000000000 such combinations, each
containing 5 numbers, so you need to store 1638400000000000
numbers, assuming 64 Bit integers, you need almost 12 PiByte
(12200000 GiByte) of RAM to store them.

Even if you don’t store but only process them lazily one-by-one,
assuming you have 10 GHz CPU and can process one number at every
clock cycle, you need almost two days. And since you probably
don’t have a 10 GHz CPU, more like 3 GHz, and you will probably
need more like 100 clock cycles to process each number, a more
realistic estimate is 1 year and 9 months.

In other words: it doesn’t matter whether you use `product` or
something else. It doesn’t even matter whether you use Ruby, C,
or re-write the entire thing in hand-optimized machine code.

You need to change the algorithm.

Greetings,
    Jörg.

···

derrick <derrick@thecopes.me> wrote:

Also, product doesnt seem to scale all that well either. I was
using product to combine 5 arrays of 800 prime numbers and it
ate up all my memory and killed the script.

Thank you, yes you are correct. I actually meant the algorithm using product in that way doesn't scale well. That is why I was writing for help, to find another algorithm. In the end 800 primes wasn't enough. I had to go up to 1500 primes but I have now found a solution for projecteuler#60. Thank you so much for helping.

Derrick

···

------------------------------------------------------------------
发件人:Jörg W Mittag<ruby-talk@joergwmittag.de>
日 期:2018年06月10日 21:50:17
收件人:Ruby users<ruby-talk@ruby-lang.org>
主 题:Re: ruby beginner help

derrick <derrick@thecopes.me> wrote:

Also, product doesnt seem to scale all that well either. I was
using product to combine 5 arrays of 800 prime numbers and it
ate up all my memory and killed the script.

This has nothing to do with the scalability of `product` or any
other possible solution. It is inherent in the problem:

There are 800^5 = 327680000000000 such combinations, each
containing 5 numbers, so you need to store 1638400000000000
numbers, assuming 64 Bit integers, you need almost 12 PiByte
(12200000 GiByte) of RAM to store them.

Even if you don’t store but only process them lazily one-by-one,
assuming you have 10 GHz CPU and can process one number at every
clock cycle, you need almost two days. And since you probably
don’t have a 10 GHz CPU, more like 3 GHz, and you will probably
need more like 100 clock cycles to process each number, a more
realistic estimate is 1 year and 9 months.

In other words: it doesn’t matter whether you use `product` or
something else. It doesn’t even matter whether you use Ruby, C,
or re-write the entire thing in hand-optimized machine code.

You need to change the algorithm.

Greetings,
    Jörg.