Brain-Flak, 96 bytes
((({})<>){<(([()]{})){<>(({})(<()>))<>{(({})){({}[()])<>}{}}{}<>([{}()]({})){((<{}{}>))}}{}>{}})
##Explanation:
Explanation:
Now outdated by improvements.
The heart of the algorithm is this:
({}(<>))<>{(({})){({}[()])<>}{}}{}<>([{}()]({})) turns |N, M...| into |N mod M, M...|
{((<{}{}>))} if the top of stack is not zero, replace it and the second with zero
That is a modification on mod that will give us M
if it is a factor of N
and 0
otherwise. Full code is below.
((({})<>) place input, N on both stacks
{ Loop to find factors
<
(([()]{})) Decrement and Duplicate; get next factor to check
{ if not zero
(<>({})<>) Copy N from other stack
({}(<>))<>{(({})){({}[()])<>}{}}{}<>([{}()]({})){((<{}{}>))} Code explained above
}
{} drop the zero
>
{} add the factor
}) push the sum