| Bytes | Lang | Time | Link |
|---|---|---|---|
| 002 | Nekomata + e | 241101T152445Z | alephalp |
| 008 | Japt x¡ | 170828T092110Z | Shaggy |
| 007 | Vyxal | 220711T000508Z | naffetS |
| 049 | Factor + math.combinatorics math.unicode | 220615T044513Z | chunes |
| 006 | Brachylog | 190302T234751Z | Unrelate |
| 060 | Scala | 170830T074346Z | Llew Val |
| 038 | Perl 6 | 170828T180535Z | Ramillie |
| 058 | R + combinat | 170828T141934Z | Giuseppe |
| 191 | C gcc | 170828T151124Z | Felix Pa |
| 061 | Haskell | 170827T202446Z | jferard |
| 178 | PHP | 170827T212737Z | Titus |
| 056 | Ruby | 170828T120442Z | ymbirtt |
| 063 | JavaScript ES6 | 170828T103039Z | Arnauld |
| 006 | MATL | 170827T192820Z | Luis Men |
| 090 | Python 2 | 170828T071757Z | ovs |
| 005 | Husk | 170827T191924Z | H.PWiz |
| 007 | 05AB1E | 170827T214553Z | scottine |
| 100 | JavaScript ES6 | 170827T235257Z | Neil |
| 052 | Mathematica | 170827T192613Z | ZaMoC |
| 042 | Mathematica | 170827T233335Z | JungHwan |
| 017 | CJam | 170827T194256Z | geokavel |
| 005 | Jelly | 170827T220020Z | Dennis |
| 027 | J | 170827T213306Z | cole |
| 092 | Python 2 | 170827T211742Z | Chas Bro |
| 011 | Pyth | 170827T190008Z | Maltysen |
| 007 | Jelly | 170827T185427Z | Leaky Nu |
Nekomata + -e, 2 bytes
↕¦
Take input in reversed order (B,A).
↕¦
↕ Find a permutation of B
¦ that divides A (vectorized)
-e checks if there is a solution.
Japt -x¡, 12 11 8 bytes
á ËívV e
á ËívV e :Implicit input of arrays U=A & V=B
á :Permutations of U
Ë :Map
í V : Interleave with V
v : And reduce each pair by testing divisibility
e : All truthy?
:Implicit output of sum of resulting array as a Boolean
Factor + math.combinatorics math.unicode, 49 bytes
[ <permutations> [ v/ [ ratio? ] ∄ ] with ∃ ]
<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?)
Brachylog, 6 bytes
pᵐz%ᵛ0
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.
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.
Perl 6, 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}
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.
R + combinat, 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.
C (gcc), 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;}}
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)
Haskell, 79 74 68 62 61 bytes
import Data.List
f a=any((<1).sum.zipWith rem a).permutations
Saved 1 byte thanks to @nimi
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.
Ruby, 56 bytes
->x,y{x.permutation.any?{|p|p.zip(y).all?{|a,b|a%b==0}}}
Fairly straightforward, exploits the fact that permutation exists.
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
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.
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
Python 2, 90 bytes
lambda a,b:any(sum(map(int.__mod__,a,p))<1for p in permutations(b))
from itertools import*
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.
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
05AB1E, 7 bytes
Input: takes lists B and A (reversed order)
Output: 1 when true, 0 otherwise
œvIyÖPM
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
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.
Mathematica, 52 bytes
Cases[Permutations@#2,p_/;And@@IntegerQ/@(#/p)]!={}&
thanks @ngenisis for -5 bytes
Mathematica, 42 bytes
MemberQ[Permutations@#2,a_/;And@@(a∣#)]&
CJam, 20 17 bytes
:A;e!{A\.%:+!}#W>
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.
Jelly, 5 bytes
Œ!%ḄẠ
Returns 0 for True, 1 for False.
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.
J, 27 bytes
0=[:*/(A.~i.@!@#)@]+/@:|"1[
Takes the first list as the left argument and the second list as the right.
Python 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*
Yer basic implementation.