Forum: Ruby [FATAL] failed to allocate memory

1a4213d34804626d852f6e053ff42e98?d=identicon&s=25 Octavian Chiliment (okt2000)
on 2014-07-27 22:32
I am constantly getting the "[FATAL] failed to allocate memory" error
message when I run the following code:
"$world=[]
$world+=((0..256).to_a+(0..256).to_a+(0..256).to_a).combination(3).to_a"
I know that there is not enough memory for the array. But is there any
other way to store this array?
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2014-07-28 01:29
Octavian Chiliment wrote in post #1153661:

> "$world=[]
> $world+=((0..256).to_a+(0..256).to_a+(0..256).to_a).combination(3).to_a"
> But is there any other way to store this array?

Store in file :

open("e.txt","w") do |f|
  l=(0..256).to_a
  (l*3).combination(3).each {|a|  f.puts a.inspect }
end
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2014-07-28 01:51
Regis d'Aubarede wrote in post #1153671:
> Octavian Chiliment wrote in post #1153661:
>
>> "$world=[]
>> $world+=((0..256).to_a+(0..256).to_a+(0..256).to_a).combination(3).to_a"
>> But is there any other way to store this array?


Or, store in a string

============================
l=(0..255).to_a
res,sep="",""

(l*3).combination(3).each {|a|
  res << sep
  sep=";"
  res << a.map {|a| a.chr }.join("")
}
============================

res.size 300MB...
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2014-07-28 02:26
Regis d'Aubarede wrote in post #1153674:
> Regis d'Aubarede wrote in post #1153671:
>> Octavian Chiliment wrote in post #1153661:
>>
>>> "$world=[]
>>> $world+=((0..256).to_a+(0..256).to_a+(0..256).to_a).combination(3).to_a"
>>> But is there any other way to store this array?

Or, store in a list of bignum :

============================
MAX=255
MAX1=MAX+1
l=(0..MAX).to_a ; l=(l*3)

res=l.combination(3).map { |a|
 a.inject(0) {|cpt,a| (cpt*MAX1+a) }
}

def decode(l,i)
  v = l[i]
  (1..3).map { r=(v%MAX1);v=v/MAX1;r }.reverse
end
============================


============================
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.