How Can I Improve This Ruby Recursive Factorization Code?

Click For Summary
SUMMARY

The discussion focuses on improving a Ruby recursive factorization code that factors numbers and translates them into binary. The provided code utilizes the 'prime' library to generate prime factors and their indices. The user is attempting to create a binary representation from the indices of these prime factors but encounters confusion regarding the intended output. Clarification on the desired outcome and additional code comments are recommended for better understanding.

PREREQUISITES
  • Ruby programming language proficiency
  • Understanding of recursion in programming
  • Familiarity with the 'prime' library in Ruby
  • Basic knowledge of binary number representation
NEXT STEPS
  • Review Ruby recursion techniques for better implementation
  • Explore the 'prime' library documentation for advanced usage
  • Learn about binary conversion methods in Ruby
  • Investigate debugging strategies for Ruby code to clarify logic flow
USEFUL FOR

Ruby developers, programmers interested in number theory, and anyone looking to enhance their skills in recursive algorithms and prime factorization.

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

Similar threads

  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 34 ·
2
Replies
34
Views
6K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
3K
  • · Replies 97 ·
4
Replies
97
Views
10K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
9
Views
3K
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K