Factors to string
Any integer can be written as a product of prime numbers. This is called the number's prime factorization. For instance, 24's prime factorization is 24 = 2 x 2 x 2 x 3
or even better 24 = 2 ^ 3 x 3
. For this exercise, we are given the prime factorization. We have to construct the string representation.
Examples:
(factors->string [2 2 2 3]) ;=> "24 = 2^3 x 3"
(factors->string [7]) ;=> "7 = 7"
(factors->string [2 2 7]) ;=> "28 = 2^2 x 7"
Just for clarity, here are the rules:
- If the prime factor appears only once, just use the number by itself (i.e., "2")
- If the prime factor appears more than once, use the exponent notation (i.e., "2^3")
- If there is more than one prime factor, separate them with an "x" for multiplication (i.e., "3 x 7")
- Start the string with "n =" where n is the number that has been factorized. You can calculate this by multiplying all the factors together.
Thanks to this site for the challenge idea where it is considered Hard level in Ruby.
Email submissions to eric@purelyfunctional.tv before August 29, 2020. You can discuss the submissions in the comments below.