# Prime Factors

### From codewars.com by Edxael (that’s me)

### Description:

*Inspired by one of Uncle Bob’s TDD Kata*

Write a function that generates factors for a given number.

- The
`function`

takes an`integer`

on the standard input and returns a list of integers. That list contains the prime factors in numerical sequence.

#### Example

`1`

-> []`3`

-> [3]`8`

-> [2, 2, 2]`9`

-> [3, 3]`12`

-> [2, 2, 3]

: – – My Code – – :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#test def prime_factors(n) left = n factors = [] 2.upto(n) do |x| if left % x == 0 then factors << x left /= x redo end end factors end n = 222 p prime_factors(n) # --------------------------------- # Test Run # # [2, 3, 37] # --------------------------------- |

Note: The following code is not written by me, it was created by another user of codewars.com it belongs to the user: njohnson7

1 2 3 4 |
require 'prime' def prime_factors(n) n.prime_division.flat_map { |prime, amt| [prime] * amt } end |

x