ginasuma
ginasuma

Reputation: 21

List the factors of a non-prime number

My goal is to display if a number is prime, and if it is not, list all of its factors. If I input 6, the program should return:

6 is not a prime number => 2, 3

Here is what I have so far:

puts "Enter a number: "
  num = gets
  num = num.to_i

def prime(num)
  is_prime = true
  for i in 2..num-1
    if num % i == 0
      is_prime = false
    end
  end
  if is_prime
    puts "#{num} is prime!"
  else
    puts "#{num} is not prime!"
  end
end
prime(num)  

I tried making a while loop for this but I can't seem to make it work. I'm not sure if I am using the num%i==0 formula correctly. Any help would be appreciated.

Upvotes: 0

Views: 123

Answers (3)

Yunnosch
Yunnosch

Reputation: 26753

Homework hint 1)
You need to change the following things in order to get all the factors:

  • do not stop as soon as you find one factor
    (which your prime-finder code inefficiently does not do anyway...)
  • print each factor you find

If you also need the correct exponent you further need to

  • check for each found factor whether it is still a factor after dividing by it,
    this needs to be done in a loop

Homework hint 2)
In order to get a specific desired output, print the decoration (e.g. "," and newlines) explicitly when it is needed.
Printing the statement first and only once can be done by using a flag for "already printed" and checking it before printing the statement in the loop.

(I assume that this is a homework question and am therefor intentionally not giving a complete solution, according to the compromise desccribed here How do I ask and answer homework questions?
Thank you for not minding my assumption, or so I gather from your accepting my answer. In case it is not about homework I assume that you appreciated the help and the way of helping, in spite of my mistake.)

Upvotes: 0

Cary Swoveland
Cary Swoveland

Reputation: 110725

require 'prime'

def factors(n)
  first, *rest =
    Prime.prime_division(n).map { |n,power| [n].product((0..power).to_a) }
   first.product(*rest).map { |a| a.reduce(1) { |t,(n,power)| t * n**power } }       
end

Let's use this method to compute the factors of 1500.

factors 1500
  #=> [ 1,  5,  25,  125,
  #     3, 15,  75,  375,
  #     2, 10,  50,  250,
  #     6, 30, 150,  750,
  #     4, 20, 100,  500,
  #    12, 60, 300, 1500]

Note that

Prime.prime_division(1500)
  #=> [[2, 2], [3, 1], [5, 3]]

and

Prime.prime_division(500).map { |n,power| [n].product((0..power).to_a) }
  #=> [[[2, 0], [2, 1], [2, 2]], [[5, 0], [5, 1], [5, 2], [5, 3]]]

Moreover, factors(n).size equals 2 if and only if n is prime. For example,

factors(37)
  #=> [1, 37]
Prime.prime?(37)
  #=> true

See Prime::prime_division.

Upvotes: 0

Sagar Pandya
Sagar Pandya

Reputation: 9498

If permitted to do so, use the Standard Library class Prime.

require 'prime'

def is_prime? n
  n.prime? ? n : Prime.prime_division(n)
end

p is_prime? 6   #=> [[2, 1], [3, 1]]
p is_prime? 11  #=> 11
p is_prime? 100 #=> [[2, 2], [5, 2]]

Upvotes: 1

Related Questions