By derrick at 06/09/2018 - 04:19

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

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.

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

## Comments

## Re: ruby beginner help

By Paul Martensen at 06/09/2018 - 04:45Hey 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:

## Re: ruby beginner help

By derrick at 06/09/2018 - 22:57Thank 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

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

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>

## Re: ruby beginner help

By derrick at 06/09/2018 - 05:23Hi, 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.

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

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>

## Re: ruby beginner help

By =?UTF-8?Q?J=C3=... at 06/10/2018 - 09:50derrick < ... at thecopes dot me> wrote:

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.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>

<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

## Re: ruby beginner help

By derrick at 06/10/2018 - 20:23Thank 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

derrick < ... at thecopes dot me> wrote:

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.

<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

## Re: ruby beginner help

By Saumya jeet at 06/09/2018 - 11:49This 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

## Re: ruby beginner help

By Sarkar Chinmoy at 06/09/2018 - 11:55Please remove me from your mailing list. Thanks

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 theminto 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

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. ... at gmx dot de>Time:2018 Jun 9 (Sat) 16:46To: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>

## Re: ruby beginner help

By derrick at 06/09/2018 - 23:09Unsubscribe yourself from this mailing list. Why did you subscribe in the first place?

Please remove me from your mailing list. Thanks

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

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.

------------------------------ ------------------------------ ------

Time:2018 Jun 9 (Sat) 16:46

Hey derrick!

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

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

In short:And map(&:flatten) takes each of these [[a,b], c] arrays and turns them into [a, b, c] arrays without flattening the whole

#product Gives you the cartesian product of two arrays so [[2, 4]].product([51, 12]) #=> [[[2,4], 51], [[2,4], 12]]

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

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>

<http://lists.ruby-lang.org/ cgi-bin/mailman/options/ruby- talk>