[Question]
  [
*Inspired (with the explanation stolen from) [this](https://stackoverflow.com/q/45906747/7742131)*
## Background
Say you have two lists `A = [a_1, a_2, ..., a_n]` and `B = [b_1, b_2, ..., b_n]` of integers. We say `A` is **potentially-divisible** by `B` if there is a permutation of `B` that makes `a_i` divisible by `b_i` for all `i`. The problem is then: is it possible to reorder (i.e. permute) `B` so that `a_i` is divisible by `b_i` for all `i`?
For example, if you have
```
A = [6, 12, 8]
B = [3, 4, 6]
```
Then the answer would be `True`, as `B` can be reordered to be `B = [3, 6, 4]` and then we would have that `a_1 / b_1 = 2`, `a_2 / b_2 = 2`, and `a_3 / b_3 = 2`, all of which are integers, so `A` is potentially-divisible by `B`.
As an example which should output `False`, we could have:
```
A = [10, 12, 6, 5, 21, 25]
B = [2, 7, 5, 3, 12, 3]
```
The reason this is `False` is that we can't reorder `B` as 25 and 5 are in `A`, but the only divisor in `B` would be 5, so one would be left out.
## Your task
Your task is, obviously, to determine whether two lists (given as input) are potentially divisible. You may take input in any accepted manner, as with output.
Duplicates in the lists are a possibility, and the only size restrictions on the integers are your language. All integers in both lists will be larger than 0, and both lists will be of equal size.
As with all [decision-problem](/questions/tagged/decision-problem "show questions tagged 'decision-problem'")s the output values must be 2 distinct values that represent true and false.
This is a [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so shortest code wins!
## Test cases
```
Input, input => output
[6, 12, 8], [3, 4, 6] => True
[10, 5, 7], [1, 5, 100] => False
[14, 10053, 6, 9] [1,1,1,1] => True
[12] [7] => False
[0, 6, 19, 1, 3] [2, 3, 4, 5, 6] => undefined
```
  
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 5 bytes
```
Œ!%ḄẠ
```
Returns **0** for *True*, **1** for *False*.
[Try it online!](https://tio.run/##y0rNyan8///oJEXVhztaHu5a8P/wciP9yP//o7kUos10FAyNdBQsYnUUoo11FEx0FMxidYDihgY6CqY6CuYgcUMw09DAACJjAmabAlUDNVtCFUARRIURSNAczDYAqzK0BEsbg8SBtkEsMgXZxRULAA "Jelly – Try It Online")
### How it works
```
Œ!%ḄẠ  Main link. Arguments: A, B (arrays)
Œ!     Generate all permutations of A.
  %    Take each permutation modulo B (element-wise).
   Ḅ   Convert all resulting arrays from binary to integer.
       This yields 0 iff the permutation is divisible by B.
    Ạ  All; yield 0 if the result contains a 0, 1 otherwise.
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), ~~7~~ ~~6~~ 5 bytes
Saved 2 bytes thanks to @Zgarb
```
▼▲‡¦P
```
Takes argents in reverse order and returns `1` for `True` and `0` for `False`.
[Try it online!](https://tio.run/##ASUA2v9odXNr///ilrzilrLigKHCplD///9bMyw0LDZd/1s2LDEyLDhd)
### Explanation
```
    P     -- Permutations of the first argument
  ‡       -- Deep zip (vectorises function) with second argument
   ¦      --   Does x divide y
 ▲        -- Get the maximum of that list, returns [1,1...1] if present
▼         -- Get the minimum of that list, will return 0 unless the list is all 1s
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 7 bytes
Input: takes lists B and A (reversed order)  
Output: 1 when true, 0 otherwise
```
œvIyÖPM
```
[Try it online!](https://tio.run/##MzBNTDJM/f//6OQyz8rD0wJ8//@PNtZRMNFRMIvlijbTUTA00lGwiAUA "05AB1E – Try It Online")
Explanations:
```
œvIyÖPM    Complete program
œ          Pushes all permutations of B as a list
 v         For each permutation
  I        Pushes last input on top of the stack
   yÖ      Computes a % b == 0 for each element of A and B
     P     Pushes the total product of the list
      M    Pushes the largest number on top of the stack
```
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~8~~ ~~7~~ 6 bytes
*1 byte off using an idea from Dennis' Jelly answer*
```
Y@\!aA
```
Inputs are `B`, then `A`. Output is `0` if divisible or `1` if not.
[**Try it online!**](https://tio.run/##y00syfmf8D/SIUYx0fG/S8j/aGMdBRMdBbNYrmgzHQVDIx0FCyDTUEfBFMgzMACxDcAcc7AwGIJYJmBpU6BuoDZLoAhY3igWAA)
### Explanation
```
Y@     % Implicit input: row vector B. Matrix of all permutations, each on a row
\      % Implicit input: row vector A. Modulo, element-wise with broadcast. Gives
       % a matrix in which each row contains the moduli of each permutation of B
       % with respect to A
!a     % True for rows that contain at least a nonzero value
A      % True if all values are true. Implicit display
```
[Answer]
# Mathematica, 52 bytes
```
Cases[Permutations@#2,p_/;And@@IntegerQ/@(#/p)]!={}& 
```
*thanks @ngenisis for -5 bytes*
[Answer]
# JavaScript (ES6), ~~67~~ 63 bytes
Returns a boolean.
```
f=([x,...a],b)=>!x||b.some((y,i)=>x%y?0:f(a,c=[...b],c[i]=1/0))
```
### Test cases
```
f=([x,...a],b)=>!x||b.some((y,i)=>x%y?0:f(a,c=[...b],c[i]=1/0))
console.log(f([6, 12, 8],[3, 4, 6]))        // true
console.log(f([10, 5, 7],[1, 5, 100]))      // false
console.log(f([14, 10053, 6, 9],[1,1,1,1])) // true
console.log(f([12],[7]))                    // false
```
[Answer]
# [Haskell](https://www.haskell.org/), ~~79~~ ~~74~~ ~~68~~ ~~62~~ 61 bytes
```
import Data.List
f a=any((<1).sum.zipWith rem a).permutations
```
[Try it online!](https://tio.run/##VY4/C8MgEMXn5lPc0CEBEc3fBpqtY/cO4iA0IdJoRM3Qfnlr7NJwHBzv/e7uzcK9xmUJQSqzWg834QW@S@ezCcQg9DvPr7TAblP4I81D@hnsqEAU2IxWbV54uWoXlJAaBniu2clYqT2cYWItAloiuHBWIagRtPzfpQRBg6DjjKaBEnL066Q1cTce6ncs1REqOesOCkk47WMjqDiLAX7fmz0AhC8 "Haskell – Try It Online")
*Saved 1 byte thanks to @nimi*
[Answer]
# [R](https://www.r-project.org/) + [combinat](https://cran.r-project.org/web/packages/combinat/combinat.pdf), ~~69~~ ~~66~~ 58 bytes
*-3 bytes thanks to Jarko Dubbeldam*
*another -8 bytes thanks to Jarko*
```
function(a,b)any(combinat::permn(b,function(x)all(!a%%x)))
```
oddly, R doesn't have a builtin for generating all permutations. Returns a boolean.
Additionally, with Jarko's second improvement, `any` coerces the list to a vector of `logical` with a warning.
[Try it online! (r-fiddle)](http://www.r-fiddle.org/#/fiddle?id=Os98W9XW&version=3 "R-fiddle")
[Answer]
# Mathematica, 42 bytes
```
MemberQ[Permutations@#2,a_/;And@@(a∣#)]&
```
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 191 bytes
```
#define F(v)for(i=0;i<v;++i){
#define X if(f(s,n,a,b))return 1
j;f(s,n,a,b,i)int*a,*b;{if(--n){F(n)X;j=i*(n%2);b[j]^=b[n];b[n]^=b[j];b[j]^=b[n];}X;}else{F(s)if(a[i]%b[i])return 0;}return 1;}}
```
[Try it online!](https://tio.run/##hZLbasMwDIbv8xQio2CnLsuxXeeF3fUZCiWDuHOGQ@eOJO3FQp4903JoXWhoCMLol/V/Et4vvvb7tn36lJnSEjbkTLNjQVTscvV25vO5orU1qltQGclIyTRLmaC0kNWp0OBZOb9kmaJKV07KHMFrLF8sNK03RNMtz2PlED3zKRe7PPmIxU4n/D/8H/PEzDZb3shDKfFmSbFLulPJTGAYPV3ejO68adrvVGlCrdoC/NAfUm@XQAz1koHnM3hp@EUSgxQwCBksDeUgNQql@pXHDDvA83h2Uq@v@jlVJYGMYCWDLqQew44U3sGuipO04RXsLEV0Gyi3rjx@b@q5DCIGK5NnlDrFc91Bu6XxTRp/kgZnFf5jmmCwDDvDCFeBi1qbUMEFavjvUgUmVTBJhf1F8JgqHDx9E2RIru76h6Z/OOmPY4rwsX/UW7ndNrx1N3dgsgwFuOT@8UTX93PLFZlc0SQX3hfRFFfT/gE "C (gcc) – Try It Online")
**Usage:** `f(int size, int size, int *a, int *b)`
returns `1` if divisable, `0` otherwise. See example usage on TIO.
(Gotta do permutations the hard way in C, so this is hardly competitive)
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 6 bytes
```
pᵐz%ᵛ0
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/pf/ahtw6Ompkcdy5VKikpTlfRqQMy0xJziVKXa04vK/xc83DqhSvXh1tkG//9HR0eb6Rga6VjE6kQb65jomMUCGdGGBjqmOuZAliGQNjQwgAiagJimxjpmOpZgKTCESBkBSXMw0wAobWgJlDEG8ox0QGaagkyNBQA "Brachylog – Try It Online")
The predicate succeeds if the two lists are potentially divisible and fails if they are not.
```
pᵐ        For some pair of permutations of the two input lists,
  z       for each pair of corresponding elements
   %ᵛ0    the first mod the second is always zero.
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 bytes
```
Œ!ọẠ€1e
```
[Try it online!](https://tio.run/##ASwA0/9qZWxsef//xZIh4buN4bqg4oKsMWX///9bNiwgMTIsIDhd/1szLCA0LCA2XQ "Jelly – Try It Online")
Factorial complexity in the length of the list.
[Answer]
# Pyth - 11 bytes
```
sm!s%Vdvz.p
```
[Test Suite](http://pyth.herokuapp.com/?code=sm%21s%25Vdvz.p&test_suite=1&test_suite_input=%5B6%2C+12%2C+8%5D%0A%5B3%2C+4%2C+6%5D%0A%5B10%2C+12%2C+6%2C+5%2C+21%2C+25%5D%0A%5B2%2C+7%2C+5%2C+3%2C+12%2C+3%5D&debug=0&input_size=2).
[Answer]
# J, 27 bytes
```
0=[:*/(A.~i.@!@#)@]+/@:|"1[
```
[Try it online!](https://tio.run/##y/r/P03BVk/BwDbaSktfw1GvLlPPQdFBWdMhVlvfwapGyTCay89JTyGkqDSVKzU5I19Bw0zB0EjBQlMhTUHDWMFEwUwTrMAtMacYpsLQQMFUwRyswhDIMjQw0Pz/HwA)
Takes the first list as the left argument and the second list as the right.
[Answer]
# CJam, ~~20~~ 17 bytes
```
:A;e!{A\.%:+!}#W>
```
[Test Version](http://cjam.aditsu.net/#code=l~%3Ail~%3Ai%5C%7B%3AA%3Be!%7BA%5C.%25%3A%2B!%7D%23W%3E%7D~&input=%5B14%2010053%206%209%5D%0A%5B1%201%201%201%5D%0A)
Function that takes array B as the first argument and array A as the second argument. Note that in the test version I switch the order to A then B.
[Answer]
## JavaScript (ES6), 100 bytes
```
f=(a,b)=>!a[0]||a.some((c,i)=>b.some((d,j)=>c%d<1&f(e=[...a],d=[...b],e.splice(i,1),d.splice(j,1))))
```
Somewhat inefficient; an extra `&` would speed it up.
[Answer]
# [Python 2](https://docs.python.org/2/), 90 bytes
```
lambda a,b:any(sum(map(int.__mod__,a,p))<1for p in permutations(b))
from itertools import*
```
[Try it online!](https://tio.run/##LU5dC4JAEHzvV8xbZ2ziWWlJ9UdM5KSkA@@D83oI8bfbXQjL7Aw7s4z9@rfR@dLfHssgVPcUENRVQn/Z@FFMCcuk9mnbKvNsWxJkk@TKe@NgITXsy6mPF14aPbIuSTa9MwrSv5w3ZhghlTXO75YYCH9jpK4LAs8J54ZQHwhHQtFEXvOMcCKUUfA/5Vm2no5/cQr@EL@sjnVWSx5X2TTVBrAu1MZ2mgnTjP094DYNLZTwLBShPmLou/wA "Python 2 – Try It Online")
[Answer]
# PHP, ~~112 180~~ 178 bytes
I was thinking too short.
```
function($a,$b){for($p=array_keys($b);++$i<count($b);){foreach($b as$k=>$x)$f|=$a[$k]%$x;if($f=!$f)return 1;$p[$i]?[$b[$j],$b[$i],$i]=[$b[$i],$b[$j=$i%2*--$p[$i]],0]:$p[$i]=$i;}}
```
anonymous function takes two arrays, returns `NULL` for falsy and `1` for truthy.  
Throws an error if second array contains `0`.
[Try it online](http://sandbox.onlinephpfunctions.com/code/00157e5748c09e70b7f1128bf8238538f7f84ac2).
[Answer]
# [Perl 6](https://perl6.org), 38 bytes
Actually @nwellnhof's answer seems to be too much readable, so I set out to follow the fine Perl tradition of write-only code :—).
1 byte saved thanks to @nwellnhof.
```
{min max (@^a,) XZ%% @^b.permutations}
```
[Try it online!](https://tio.run/##VY7RCoJAEEXf@4oLJRgsoWVWROA/9BJFwlZrDrm7su5iEn27peFDzMtwz5nhlsIUcSsbTDLs2pckBcmf8JOUsykOR89Dkl5mpTDSWW5Jq@rdVrzz/VPMEM4Z1meG04IhYojP0@1owGHAsGRYdTjs1zAI/oSoj5bf2@@rzc/r58@ad2A1RErXW4yx12i0w5V3hR8C2lnUuVCwuUBp9N1wiYwUVbm4gTIYpxSpO8hCusJSWQhYkqKatR8 "Perl 6 – Try It Online")
What does it do: It's an anonymous function that takes two list arguments. When we say `@^a`, we mean the first one, when `@^b`, it's the second one.
`(@^a,)` is a list containing the list `@^a`. `@^b.permutations` is the list of all the permutations of `@^b`. The "XZ%%" operator makes all possible pairs of that one list on the left and all the permutations on the right, and uses the operator "Z%%" on them, which is the standard "zip" operation using the divisibility operator %%.
The `max` operator gives the largest element of the list (in this case, it's the list that has the most `True`'s in it). We then reduce it using the logical AND operator to see if all elements of that "most true" list are true, and that's the result. It's nearly exact copy of what @nwellnhof wrote, just using obscure operators to shave off bytes.
[Answer]
# [Factor](https://factorcode.org/) + `math.combinatorics math.unicode`, 49 bytes
```
[ <permutations> [ v/ [ ratio? ] ∄ ] with ∃ ]
```
[Try it online!](https://tio.run/##bVC7jsIwEOzzFXM/AEl4Ho/QgWiu4a5CFL5ghAWxI9sBnVAqKPhOfiRsYjiBYEda7axnRtauWGyVLn5m069JDwmz61qskl8hGa1FbNwqkyJWS@7IjpcW94IN15JvkWpu7V@qhbRgxigy9j3v4IHqQGgjCNFFTmMDTaI54V4fGEb41hn/lwc@WuhU8oCmwPcfDJV8zLbmQd8sNa0GBX/eXA6l6018WIk6T7/Aa3zuFXMMUq6TzDIrlDQR5tjVqemSj7DA5Xyivhd0i8v5iEWxcgeIEpaiVlwB "Factor – Try It Online")
* `<permutations> [ ... ] with ∃` Is there any permutation of the second input where...
* `v/` ...the quotient of the first input and the permutation...
* `[ ratio? ] ∄` ...contains no ratios? (In other words, are they all integers?)
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 7 bytes
```
Ṗ$vḊvAa
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyJBIiwiIiwi4bmWJHbhuIp2QWEiLCIiLCJbNiwgMTIsIDhdLCBbMywgNCwgNl1cblsxMCwgNSwgN10sIFsxLCA1LCAxMDBdXG5bMTQsIDEwMDUzLCA2LCA5XSwgWzEsMSwxLDFdXG5bMTJdLCBbN10iXQ==)
[Answer]
# [Python 2](https://docs.python.org/2/), 92 bytes
```
lambda a,b:any(all(x%y<1for x,y in zip(a,p))for p in permutations(b))
from itertools import*
```
[Try it online!](https://tio.run/##LY3LDoIwEEXX8hWzMbRmFhR5KBGX/ASyKIHGJtA2pSbgzyNFMpuZc@7NmMW9tYpXUb7WgY9tx4FjW3C1ED4MZD4vDya0hRkXkAq@0hCOhlLPjCemt@PHcSe1mkhLaSCsHkG63jqthwnkaLR1l7XrBVRbt6VFcDJWKucfYVg@QxQ7D4KK1BkCixFuDUJ9RUgQsoZ6wSKEFCH3gu0ri6JDJfuRbvmtfv8n9jl87FHe0PUH "Python 2 – Try It Online")
Yer basic implementation.
[Answer]
# [Ruby](https://www.ruby-lang.org/), 56 bytes
```
->x,y{x.permutation.any?{|p|p.zip(y).all?{|a,b|a%b==0}}}
```
[Try it online!](https://tio.run/##bY5BC4JAEIXv/YqBCAqmZbdS87B26xd0Mw9rrSDotqgLbupvt02jLjEMDPO@93iVSe2YAb@O26hF27VEy6o0jWjyhyJC2VPX616TZ67XdkNEUbiHwLQXq5RzOgzDqCEj69hHYDuEY4IQ7xEOCH6ygSWP4FIZuZghRhE8hOANselklH6wsyjqL3eYFM8Fudxwxqf5k7l7y8GfFDq5WegWYZ9g7PrN1bxfO6PuMsuVvOPshVxBaeEmajm@AA "Ruby – Try It Online")
Fairly straightforward, exploits the fact that `permutation` exists.
[Answer]
# Scala, 60 Bytes
**Golfed:**
```
a=>b=>b.permutations exists(a zip _ forall(p=>p._1%p._2==0))
```
**Ungolfed:**
```
a=>b=>         // Function literal taking 2 lists of integers, a and b.
b.permutations // All permutations of b.
exists(        // Whether the given function is true for any element.
a zip _        // Zips a and the current permutation of b into a list of pairs.
forall(        // Whether the given function is true for all elements.
p=>            // Function literal taking a pair of integers.
p._1%p._2==0)) // If the remainder of integer division between the members of the pair is 0.
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt/), ~~12~~ 11 bytes
Outputs `true` or `false`.
```
Vá de@gY vX
```
[Test it](https://ethproductions.github.io/japt/?v=1.4.5&code=VuEgZGVAZ1kgdlg=&input=WzYsMTIsOF0KWzMsNCw2XQ==)
---
## Explanation
Implicit input of arrays `U` & `V` (`A` & `B`, respectively)
```
Vá
```
Generate an array of all permutations of `V`.
```
d
```
Check if any of the elements (sub-arrays) return true.
```
e@
```
Check if every element in the current sub-array returns true when passed through the following function, with `X` being the current element and `Y` the current index.
```
gY
```
Get the element in `U` at index `Y`.
```
vX
```
Check if it's divisible by `X`.
]