I’m trying to re-write the prime factors kata without using the Prime

library. About to go berserk with frustration, too.

I’ve tried a mess of different techniques and puts’d all over the place

to “see” a solution, but have just been looking at it too long now.

Right now the code below will iterate over a range, find the factors,

and add

to array.

But I’m missing something to reduce the factors to actual prime factors.

: \

The following produces the answer I’m looking for, with the prime

library (for

guidance).

require ‘prime’

def prime(n)

n.prime_division.map { |base, exp| [base]*exp }.flatten

end

Any input on the below?? Thanks in advance for your time & help

# -----------

# Create a method which takes integers and returns their prime factors

in an array.

# primes are integers greater than 1,

# divisible only by 1 and themselves.

# primes > 2 are odd numbers.

# prime factors of integer <= square root of integer parameter (n)

def primes(n)

puts

puts

# initialize the array to add the prime factors

prime_factors = []

factor = n

# iterate over range, up to sqrt integer

(2).upto(Math.sqrt(factor).to_i). each do |divisor|

# for divisor in (2…factor) do

# Math.sqrt(factor).to_i.downto(2).each do |divisor|

```
puts "Finding primes from #{divisor} to #{factor}."
puts
puts "Checking now if #{divisor} is a factor."
if factor % divisor == 0
puts "--> #{factor} is divisible by #{divisor}."
prime_factors << divisor
puts "Factors array now set: #{prime_factors.inspect}"
quotient = factor / divisor
factor = quotient
end #/if loop
```

end #/do

puts

puts “Exiting loop; #{factor} is the last quotient to’ve passed if

statement.”

prime_factors << factor

p prime_factors

end

# primes(256) # should print [2,2,2,2,2,2,2,2]

# primes(100) # should print [2,2,5,5]

# primes(81) # should print [3,3,3,3]

# primes(10) # should print [2,5]

primes(8) # should print [2,2,2] --> currently prints [2,4]