How Can I Improve This Ruby Recursive Factorization Code?

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 2K views
farolero
Messages
166
Reaction score
10
mabe someone can help me with this code:

i have this code, basically first i factorize for example the number 28 to: [2,2,7] and then i make a list of prime numbers and find the index of each factor in that list, so 2 is prime number with index 0 and 7 prime number with index 2 so it ends up like this: [[0],[0],[2]] with which another recursion would be: [[0],[0],[[0]]] which tranlated to binary would be: 1101101110111

but I am stuck on this:
Ruby:
require 'prime'

def f(n)

   Prime.prime_division(n).flat_map { |factor, power| [factor] *   power }

end

n=rand(10000)

puts n

f=f (n)

require 'prime'

@list=Prime.take(10000)

g=[]

j=0

f.each do |j|if j>10

    i=f(@list.index(j))

    g.push i

    i=[]

else

    g.push j

end

end

print g
 
Physics news on Phys.org
farolero said:
mabe someone can help me with this code:

i have this code, basically first i factorize for example the number 28 to: [2,2,7] and then i make a list of prime numbers and find the index of each factor in that list, so 2 is prime number with index 0 and 7 prime number with index 2 so it ends up like this: [[0],[0],[2]] with which another recursion would be: [[0],[0],[[0]]] which tranlated to binary would be: 1101101110111

It is not clear at least to me, what exactly you're trying to accomplish. Giving more explanation about that and putting some comments in your code would be helpful.
 
  • Like
Likes   Reactions: jim mcnamara