| Bytes | Lang | Time | Link |
|---|---|---|---|
| 004 | Japt | 170829T190125Z | Shaggy |
| 050 | Ruby rprime | 230923T160108Z | Value In |
| 003 | Vyxal | 230921T001239Z | emanresu |
| 032 | Factor + math.primes.factors | 211027T020408Z | chunes |
| 007 | Braingolf | 170830T091011Z | Mayube |
| 004 | APL Dyalog Extended | 190317T052552Z | voidhawk |
| 016 | Deorst | 170830T214309Z | caird co |
| 016 | Deorst | 170830T214452Z | Mr. Xcod |
| 037 | Bash + GNU utilities | 170829T231056Z | Digital |
| 116 | Python 3 | 170830T081818Z | Mr. Xcod |
| 103 | Python 2 | 170830T080420Z | Simon |
| 005 | Actually | 170830T095151Z | user4594 |
| 004 | Pyth | 170829T184753Z | Mr. Xcod |
| 003 | Ohm v2 | 170830T074658Z | Mr. Xcod |
| 006 | Brachylog | 170830T072009Z | Mr. Xcod |
| 003 | 05AB1E | 170829T190356Z | Mr. Xcod |
| 006 | Brachylog | 170830T065734Z | Fatalize |
| 010 | J | 170830T055827Z | Gregory |
| 118 | C++ | 170830T044418Z | hvd |
| 017 | MY | 170830T013553Z | Adalynn |
| 006 | MATL | 170829T190813Z | Sanchise |
| 061 | Octave | 170829T195212Z | rahnema1 |
| 003 | Husk | 170829T192616Z | Mr. Xcod |
| 077 | Haskell | 170829T194535Z | Cristian |
| 004 | Jelly | 170829T185809Z | Leaky Nu |
| 004 | Gaia | 170829T193934Z | Mr. Xcod |
| 011 | CJam | 170829T191230Z | geokavel |
| 064 | Mathematica | 170829T190022Z | ZaMoC |
| 004 | Pyke | 170829T191313Z | Blue |
| 102 | PowerShell | 170829T191134Z | AdmBorkB |
| 004 | Pyke | 170829T185928Z | Mr. Xcod |
Factor + math.primes.factors, 32 bytes
[ [ factors ] map-flat members ]
factorsGet the prime factors of a number.map-flatApply a quotation to each member of a sequence, collecting the (non-scalar) results in a flat sequence of the same length.membersRemove duplicates.
Braingolf, 7 bytes
&(p)u=;
Oh look, it's basically a chain of 4 built-ins
Explanation
&(p)u=; Implicit input from commandline args
(.) Sandbox loop, sandboxes each item in a separate stack and runs the
code within the loop.
& Append the entire sandboxed stack when loop ends, rather than only the
top of stack after each iteration
p Prime factors
u Unique
= Print stack
; Suppress implicit output
Braingolf v2, 5 bytes
&pu=;
Braingolf's v2 interpreter doesn't have the "sandbox loop" (.) implemented yet, however due to more consistent modifier behavior, it's not needed for this challenge, as the greedy & modifier works directly on the prime factors p instruction, causing it to factorize every item on the stack.
Deorst, 16 bytes
EDkE]EQFPkl1FeE_
Done with help from @Mr.Xcoder. This is way too long for a pseudogolfing language.
How it works
EDkE]EQFPkl1FeE_ - Full program, implicit input: [1,2,3,4,5]
ED - Get divisors. Vectorizes. STACK = [[1], [1,2], [1,3], [1,2,4], [1,5]]
k - Turn off sorting for the next command
E] - Flatten the stack. STACK = [1, 1, 2, 1, 3, 1, 2, 4, 1, 5]
EQ - Deduplicate stack in place. STACK = [1, 2, 3, 4, 5]
FP - Filter by primality 1 is considered prime. STACK = [1, 2, 3, 5]
k - Turn off sorting for the next command
l1 - Push 1. STACK = [1, 2, 3, 5, 1]
Fe - Filter elements that are equal to the last element. STACK = [2, 3, 5]
E_ - Output the whole stack
Deorst, 16 bytes
EDkE]l1FeFPkEQE_
Done with help from @cairdcoinheringaahing in the Deorst chatroom (note that the solutions are different).
Explanation
EDkE]l1FeFPkEQE_ Full program.
ED Push the list of divisors of each element.
k Prevent the stack from reordering.
E] Flatten the stack.
l1Fe Remove 1s from the stack (because caird rushed and made 1 prime!) - Should be removed in future language releases.
FP Keep the primes.
k Prevent the stack from reordering.
EQ Deduplicate.
E_ Output the result.
Bash + GNU utilities, 37
- 21 bytes saved thanks to @muru (wow!)
factor|tr \ \\n|awk '!/:/&&!a[$0]++'
Python 3, 128 125 116 bytes
This is a pure Python solution. No packages. Thanks to Halvard for saving 9 bytes.
def f(l):y=[k for i in l for k in range(2,i+1)if i%k<1*all(k%x for x in range(2,k))];print(sorted({*y},key=y.index))
Python 2, 133 127 126 bytes
def f(l):y=sum([[k for k in range(2,i+1)if i%k<1*all(k%x for x in range(2,k))]for i in l],[]);print sorted(set(y),key=y.index)
Python 2, 142 138 134 bytes
l=input();r=[]
for i in sum([[k for k in range(2,i+1)if i%k<1*all(k%x for x in range(2,k))]for i in l],[]):r+=[i]*(i not in r)
print r
Very surprised there was no Python answer yet. Working on golfing.
Python 2, 88 119 103 bytes
Here we go. With the correct sorting.
def f(l,s=[]):[s.append(x) for x in sum([list(primefac(i)) for i in l],[]) if x not in s];print s
from primefac import*
Apperently I can't get it to work on TIO, because the package is not supported. It does run on my machine tho. Here are my Test outputs:
f([1,2,3,4,5,6,7,8,9,10],[]) #[2, 3, 5, 7]
f([10,9,8,7,6,5,4,3,2,1],[]) #[2, 5, 3, 7]
f([100,99,98,1,2,3,4,5],[]) #[2, 5, 3, 11, 7]
f([541,60,19,17,22],[]) #[541, 2, 3, 5, 19, 17, 11]
f([1,1,2,3,5,8,13,21,34,45],[]) #[2, 3, 5, 13, 7, 17]
f([6,7,6,7,6,7,6,5],[]) #[2, 3, 7, 5]
f([1],[]) #[]
f([8],[]) #[2]
f([],[]) #[]
Somehow I was not able to make the function as a lambda-function. Whenever i try to return the list comprehention, it returns [None, None, ...]. If i am just overlooking something, could someone point that mistake out? Thanks for the feedback!
Edit:
Using Mr. Xcoders sorting algorithm I could cut down the code by 16 bytes. Thank you for that part.
from primefac import*
def f(l):a=sum([list(primefac(i))for i in l],[]);print sorted(set(a),key=a.index)
Actually, 5 bytes
♂y♂i╔
Explanation:
♂y♂i╔
♂y prime factors of each element
♂i flatten
╔ deduplicate (stable)
Pyth, 5 4 bytes
{smP
Try it here! or Verify all test cases.
Alternative: {sPM
Explanation
{smP Full program with implicit input (Q).
m Map over the input.
P Prime factors.
s Flatten.
{ Deduplicate.
Ohm v2, 3 bytes
Yet another 3-byter (thanks to languages with auto-vectorization).
m{U
Explanation
m Prime factors. Auto-vectorizes over the input.
{ Flatten.
U Uniquify.
Brachylog, 6 bytes
ḋᵐ↔ᵐcd
Brachylog, 6 bytes
ḋᵐoᵐcd
Explanation
ḋᵐ Map with prime decomposition (which returns the factors in reverse order).
↔ᵐ Reverse each (or oᵐ - order each).
c Concatenate (flatten).
d Deduplicate.
05AB1E, 3 bytes
Outputs as a list of Strings.
f˜Ù
2sable, 3 bytes
Yes, this also works in 2sable. Also returns a list of Strings.
f˜Ù
Brachylog, 6 bytes
ḋᵐoᵐcd
Explanation
ḋᵐ Map prime decomposition
oᵐ Map order
c Concatenate
d Remove duplicates
J, 10 bytes
~.(#~*),q:
I'm sure some clever J-er could make this shorter.
C++, 118 bytes
[](auto n){decltype(n)r;for(int m:n)for(int i=1,j;i++<m;){j=m%i;for(int x:r)j|=!(i%x);if(!j)r.push_back(i);}return r;}
Needs to be passed the input in a std::vector<int>, returns another std::vector<int> for output.
MY, 17 bytes
⎕Ḋḟ’⊢f(‘53ǵ'ƒf(ū←
How?
⎕evaluated inputḊdivisors (vectorizes/vecifies)ḟflatten’⊢f(‘decrement, filter, increment (removes1)53ǵ'the string'P'in MY's codepage, which is primality testing. Sadly0x35=53is the 16th prime number, and there's not a command for pushing16to the stack >_< .ƒas a functionf(filter by thatūuniquify←output
MATL, 6 bytes
"@Yfvu
Explanation:
" % Loop over input
@ % Push the array element
Yf % Prime factors
v % Concatenate entire stack vertically (does nothing the first iteration)
u % Stably get distinct (unique, in MATLAB terminology) elements. Does so every loop but this is code golf, not fastest code.
Interesting MATL tidbits: generally, all functions apply to vectors (arrays) just as easily. But in this case, the number of factors is variable for each input, and Matlab and by extension MATL generally only deal in square matrices, so I had to use a for loop ".
Furthermore, MATL has two main concatenation operators: h and v, horizontal and vertical concatenation. Their behaviour differs significantly: v concatenates the entire stack, even if it has only one element like in our first iteration. h takes exactly two elements and will fail if only one is present, making it unsuitable for this application.
Husk, 3 bytes
1 byte saved thanks to @Zgarb.
uṁp
Explanation
uṁp Full program. p Prime factors of each. ṁ Map function over list and concatentate the result. u Unique.
Haskell, 77 bytes
import Data.List
x!y|y>x=[]|x`mod`y<1=y:(x`div`y)!y|1<2=x!(y+1)
nub.((!2)=<<)
Explanation:
- the
x!yoperator returns a list of all prime factors ofxthat are greater than or equal toy - the
(!2)function returns a list of all prime factors of its argument - the function on the last line implements the required functionality
Gaia, 4 bytes
ḍ¦_u
Explanation
ḍ¦ Prime factors of each. _ Flatten the list. u Remove duplicate elements.
CJam, 11 bytes
{:mfe__&1-}
Function that takes array of ints and outputs array of ints.
Mathematica, 64 bytes
Select[DeleteDuplicates[First/@FactorInteger@#~Flatten~1],#>1&]&
input
[{100, 99, 98, 1, 2, 3, 4, 5}]
PowerShell, 102 bytes
param($x)$a=@();$x|%{$a+=(2..($z=$_)|?{!($z%$_)-and'1'*$_-match'^(?!(..+)\1+$)..'}|sort)};$a|select -u
(Borrows factorization idea from TessellatingHeckler's answer over on "Get thee behind me Satan-Prime!")
Takes input as a literal array $x. Creates a new empty array $a. Loops over $x. Each iteration we loop from 2 up to the current number, checking whether that is a factor -and is prime, then |sort the output of that, and append it to $a. When we're done going through $x, we then output $a but |select only the -unique numbers thereof. This exploits the fact that the uniqueify goes left-to-right, keeping the first occurrence, which matches the problem description. Those numbers are left on the pipeline and output is implicit.