From: David Springer Date: 2012-10-07T10:29:31+09:00 Subject: Re: Kata @$$-kick! Prime Factors --bcaec554d7a21a3a2a04cb6e09f8 Content-Type: text/plain; charset=ISO-8859-1 This will get you the answers you want BUT it is not doing the correct thing. Your trial divisors only need to be primes. # ----------- # 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 divisor = 2 while divisor*divisor <= factor # 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." while 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 divisor += 1 end #/do puts puts "Exiting loop; #{factor} is the last quotient to've passed if statement." if factor > 1 prime_factors << factor end p prime_factors end On Sat, Oct 6, 2012 at 4:40 PM, incag neato wrote: > 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] > > -- > Posted via http://www.ruby-forum.com/. > > -- David N. Springer Eau Claire, WI --bcaec554d7a21a3a2a04cb6e09f8 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable This will get you the answers you want BUT it is not doing the correct thin= g.
Your trial divisors only need to be primes.

# -----------
# Create a method which takes integers and re= turns their prime factors in an array.

=A0 # primes are integers greater than 1,
=A0= # divisible only by 1 and themselves.
=A0 # primes > 2 are od= d numbers.
=A0 # prime factors of integer <=3D square root of = integer parameter (n)

def primes(n)
puts
puts
= =A0 # initialize the array to add the prime factors
=A0 prime_fac= tors =3D []
=A0 factor =3D n
=A0 divisor =3D 2
=A0 while divisor*divisor <=3D factor
=A0 # iterate over range, up to sqrt integer
=A0 #(2).upto(M= ath.sqrt(factor).to_i). each do |divisor|
=A0 # for divisor in (2= ..factor) do
=A0 # Math.sqrt(factor).to_i.downto(2).each do |divi= sor|

=A0 =A0 puts "Finding primes from #{divisor} to #{= factor}."
=A0 =A0 puts
=A0 =A0 puts "Checking= now if #{divisor} is a factor."

=A0 =A0 =A0 = while factor % divisor =3D=3D 0

=A0 =A0 =A0 =A0 puts "--> #{factor} is divisibl= e by #{divisor}."

=A0 =A0 =A0 =A0 prime_facto= rs << divisor

=A0 =A0 =A0 =A0 puts "Fac= tors array now set: #{prime_factors.inspect}"

=A0 =A0 =A0 =A0 quotient =3D factor / divisor

=A0 =A0 =A0 =A0 factor =3D quotient

=A0 =A0 =A0 end #/if loop
=A0 =A0 =A0 divisor +=3D 1
=
=A0 end #/do
=A0 puts
=A0 puts "Exiting loop; #{factor} is the last = quotient to've passed if statement."
=A0 if factor > = 1
=A0 =A0 =A0 prime_factors << factor
=A0 =A0end<= /div>
=A0 p prime_factors
end


On Sat, Oct 6, = 2012 at 4:40 PM, incag neato <lists@ruby-forum.com> wrote= :
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 p= lace
to "see" a solution, but have just been looking at it too long no= w.

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)
=A0 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.

=A0 # primes are integers greater than 1,
=A0 # divisible only by 1 and themselves.
=A0 # primes > 2 are odd numbers.
=A0 # prime factors of integer <=3D square root of integer parameter (n)=

def primes(n)
puts
puts
=A0 # initialize the array to add the prime factors
=A0 prime_factors =3D []
=A0 factor =3D n

=A0 # iterate over range, up to sqrt integer
=A0 (2).upto(Math.sqrt(factor).to_i). each do |divisor|
=A0 # for divisor in (2..factor) do
=A0 # Math.sqrt(factor).to_i.downto(2).each do |divisor|

=A0 =A0 puts "Finding primes from #{divisor} to #{factor}."
=A0 =A0 puts
=A0 =A0 puts "Checking now if #{divisor} is a factor."

=A0 =A0 =A0 if factor % divisor =3D=3D 0

=A0 =A0 =A0 =A0 puts "--> #{factor} is divisible by #{divisor}.&quo= t;

=A0 =A0 =A0 =A0 prime_factors << divisor

=A0 =A0 =A0 =A0 puts "Factors array now set: #{prime_factors.inspect}&= quot;

=A0 =A0 =A0 =A0 quotient =3D factor / divisor

=A0 =A0 =A0 =A0 factor =3D quotient

=A0 =A0 =A0 end #/if loop

=A0 end #/do
=A0 puts
=A0 puts "Exiting loop; #{factor} is the last quotient to've passe= d if
statement."
=A0 prime_factors << factor
=A0 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]

--
Posted via http://= www.ruby-forum.com/.




-- David N. Springer
Eau Claire, WI
--bcaec554d7a21a3a2a04cb6e09f8--