13
1
Given an array of positive integers, output a stable array of the distinct prime factors of these integers. In other words, for each integer in the input in order, get its prime factors, sort them, and append any primes not already in the output to the output.
Test Cases
[1,2,3,4,5,6,7,8,9,10] -> [2,3,5,7]
[10,9,8,7,6,5,4,3,2,1] -> [2,5,3,7]
[100,99,98,1,2,3,4,5] -> [2,5,3,11,7]
[541,60,19,17,22] -> [541,2,3,5,19,17,11]
[1,1,2,3,5,8,13,21,34,45] -> [2,3,5,13,7,17]
[6,7,6,7,6,7,6,5] -> [2,3,7,5]
[1] -> []
[8] -> [2]
[] -> []
Output can be as an array or list of integers or strings, delimited output, or any other standard means of outputting an ordered list of numbers.
This is code-golf, so shortest answer in bytes wins.
Sandbox – Stephen – 2017-08-29T18:45:26.423
5This is one of those challenges that I think is “too simple”. Almost every answer is gonna look like one of these: (a) a loop over the input, and Ye Olde Prime Factorization Code with a conditional append; (b) a chain of four built-ins. There just isn’t much room for creativity. Maybe the answers will prove me wrong, but I doubt it. There’s very little more to golf than prime factorization here, and that’s been done to death. – Lynn – 2017-08-29T19:02:22.020
1@Lynn it's trivial for golfing langs, but non-trivial for nearly everything else. Not sure if that's grounds for triviality here :/ – Stephen – 2017-08-29T19:03:41.000
Can you tell me which are "the distinct prime factors" of 1? – J42161217 – 2017-08-29T19:05:49.883
@Jenny_mathy there are none, therefore
[1] -> []
-1
is not prime. – Stephen – 2017-08-29T19:06:22.467Can we output as a list of strings (i.e.:
['2', '3', '5', '13', '7', '17']
)? – Mr. Xcoder – 2017-08-29T19:08:49.353@Mr.Xcoder
Output can be as an array or list of integers or strings
– Stephen – 2017-08-29T19:09:01.997Does the output list order matter? – Digital Trauma – 2017-08-29T23:00:43.830
1@DigitalTrauma Yes. Otherwise it would just be "output the set of all prime factors of the input" – Stephen – 2017-08-30T00:32:08.213