# Is recusion the best?

**URL:** <https://www.ruby-forum.com/t/is-recusion-the-best/172615>\
**Category:** Ruby\
**Created:** [July 24, 2009, 4:48am UTC](https://www.ruby-forum.com/t/is-recusion-the-best/172615 "2009-07-24T04:48:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 24, 2009, 4:48am UTC](https://www.ruby-forum.com/t/is-recusion-the-best/172615/1 "2009-07-24T04:48:27Z")

</div>

Hi all,

I have some codes implemented with recursion. When the loop/recursion  
number is small everything looks fine to me. But the program starts  
slow when the loop number is \>7.I wonder if recursion is the best when  
the recursion number is \>7?

Thanks,

Li

#########################def table(array,word\_size=1,word=’’)

hash\_table=Hash.new(0)

# create the keys first

array.each do |e|  
if word\_size==1  
hash\_table["#{word+e}"]=0

else  
hash\_temp=table(array,word\_size-1,word+e )  
hash\_table.merge!(hash\_temp)

end  
end

# create the values based on keys

hash\_table.each do |k,v|  
hash\_table[k]=k.tr(‘ACGT’,‘0123’).to\_i(4)  
end

return hash\_table  
end

######################  
array=[‘A’,‘C’,‘G’,‘T’]  
#array=[‘A’]  
p table(array,10)

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 24, 2009, 6:19am UTC](https://www.ruby-forum.com/t/is-recusion-the-best/172615/2 "2009-07-24T06:19:36Z")

</div>

On Fri, Jul 24, 2009 at 11:47 AM, Li Chen[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Li

Does this do the same thing?

hash\_table=Hash.new(0)  
wordsize = 10

(0…4\*\*wordsize).each do |f|  
hash\_table[f.to\_s(4).rjust(wordsize,“0”).tr(‘0123’,‘ACGT’)] = f  
end

p hash\_table

Harry

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 24, 2009, 9:02am UTC](https://www.ruby-forum.com/t/is-recusion-the-best/172615/3 "2009-07-24T09:02:38Z")

</div>

Harry K. wrote:

> Harry

Yes, they are the same. GOOD JOB!!!

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 24, 2009, 9:41am UTC](https://www.ruby-forum.com/t/is-recusion-the-best/172615/4 "2009-07-24T09:41:48Z")

</div>

> Yes, they are the same. GOOD JOB!!!
> 
> Li

Well, it is much faster, but it will start to feel very slow, too if  
your word size gets much bigger.

Harry
