Most Common Multiple
Clash Royale CLAN TAG#URR8PPP
up vote
22
down vote
favorite
Not to be confused with Least Common Multiple.
Given a list of positive integers with more than one element, return the most common product of two elements in the array.
For example, the MCM of the list [2,3,4,5,6]
is 12
, as a table of products is:
2 3 4 5 6
---------------
2 | # 6 8 10 12
3 | # # 12 15 18
4 | # # # 20 24
5 | # # # # 30
6 | # # # # #
Thanks DJMcMayhem for the table
As 12
appears the most times (two times as 2*6
and 3*4
). Note that we aren't including the product of an element and itself, so 2*2
or 4*4
do not not appear in this list. However, identical elements will still be multiplied, so the table for [2,3,3]
looks like:
2 3 3
----------
2 | # 6 6
3 | # # 9
3 | # # #
With the MCM being 6
.
In the event of a tie, you may return any of the tied elements, or a list of all of them.
- This is code-golf, so the shortest byte count for each language wins!
Test-cases:
[2,3,4,5,6] -> 12
[7,2] -> 14
[2,3,3] -> 6
[3,3,3] -> 9
[1,1,1,1,2,2] -> 2
[6,200,10,120] -> 1200
[2,3,4,5,6,7,8,8] -> 24
[5,2,9,10,3,4,4,4,7] -> 20
[9,7,10,9,7,8,5,10,1] -> 63, 70, 90 or [63,70,90]
code-golf array-manipulation
add a comment |Â
up vote
22
down vote
favorite
Not to be confused with Least Common Multiple.
Given a list of positive integers with more than one element, return the most common product of two elements in the array.
For example, the MCM of the list [2,3,4,5,6]
is 12
, as a table of products is:
2 3 4 5 6
---------------
2 | # 6 8 10 12
3 | # # 12 15 18
4 | # # # 20 24
5 | # # # # 30
6 | # # # # #
Thanks DJMcMayhem for the table
As 12
appears the most times (two times as 2*6
and 3*4
). Note that we aren't including the product of an element and itself, so 2*2
or 4*4
do not not appear in this list. However, identical elements will still be multiplied, so the table for [2,3,3]
looks like:
2 3 3
----------
2 | # 6 6
3 | # # 9
3 | # # #
With the MCM being 6
.
In the event of a tie, you may return any of the tied elements, or a list of all of them.
- This is code-golf, so the shortest byte count for each language wins!
Test-cases:
[2,3,4,5,6] -> 12
[7,2] -> 14
[2,3,3] -> 6
[3,3,3] -> 9
[1,1,1,1,2,2] -> 2
[6,200,10,120] -> 1200
[2,3,4,5,6,7,8,8] -> 24
[5,2,9,10,3,4,4,4,7] -> 20
[9,7,10,9,7,8,5,10,1] -> 63, 70, 90 or [63,70,90]
code-golf array-manipulation
Sandbox (deleted)
â Jo King
Aug 6 at 10:39
4
Suggested test case: one where all elements are the same (i.e.[3,3,3] -> 9
). With all your current test cases filtering out any pairs where elements are the same (even for test cases like[2,3,3]
containing the same values) will still hold the correct test-results, but will fail for this test case because none will remain after filtering.
â Kevin Cruijssen
Aug 6 at 12:07
@Kevin Good suggestion, added
â Jo King
Aug 6 at 12:38
add a comment |Â
up vote
22
down vote
favorite
up vote
22
down vote
favorite
Not to be confused with Least Common Multiple.
Given a list of positive integers with more than one element, return the most common product of two elements in the array.
For example, the MCM of the list [2,3,4,5,6]
is 12
, as a table of products is:
2 3 4 5 6
---------------
2 | # 6 8 10 12
3 | # # 12 15 18
4 | # # # 20 24
5 | # # # # 30
6 | # # # # #
Thanks DJMcMayhem for the table
As 12
appears the most times (two times as 2*6
and 3*4
). Note that we aren't including the product of an element and itself, so 2*2
or 4*4
do not not appear in this list. However, identical elements will still be multiplied, so the table for [2,3,3]
looks like:
2 3 3
----------
2 | # 6 6
3 | # # 9
3 | # # #
With the MCM being 6
.
In the event of a tie, you may return any of the tied elements, or a list of all of them.
- This is code-golf, so the shortest byte count for each language wins!
Test-cases:
[2,3,4,5,6] -> 12
[7,2] -> 14
[2,3,3] -> 6
[3,3,3] -> 9
[1,1,1,1,2,2] -> 2
[6,200,10,120] -> 1200
[2,3,4,5,6,7,8,8] -> 24
[5,2,9,10,3,4,4,4,7] -> 20
[9,7,10,9,7,8,5,10,1] -> 63, 70, 90 or [63,70,90]
code-golf array-manipulation
Not to be confused with Least Common Multiple.
Given a list of positive integers with more than one element, return the most common product of two elements in the array.
For example, the MCM of the list [2,3,4,5,6]
is 12
, as a table of products is:
2 3 4 5 6
---------------
2 | # 6 8 10 12
3 | # # 12 15 18
4 | # # # 20 24
5 | # # # # 30
6 | # # # # #
Thanks DJMcMayhem for the table
As 12
appears the most times (two times as 2*6
and 3*4
). Note that we aren't including the product of an element and itself, so 2*2
or 4*4
do not not appear in this list. However, identical elements will still be multiplied, so the table for [2,3,3]
looks like:
2 3 3
----------
2 | # 6 6
3 | # # 9
3 | # # #
With the MCM being 6
.
In the event of a tie, you may return any of the tied elements, or a list of all of them.
- This is code-golf, so the shortest byte count for each language wins!
Test-cases:
[2,3,4,5,6] -> 12
[7,2] -> 14
[2,3,3] -> 6
[3,3,3] -> 9
[1,1,1,1,2,2] -> 2
[6,200,10,120] -> 1200
[2,3,4,5,6,7,8,8] -> 24
[5,2,9,10,3,4,4,4,7] -> 20
[9,7,10,9,7,8,5,10,1] -> 63, 70, 90 or [63,70,90]
code-golf array-manipulation
edited Aug 6 at 12:38
asked Aug 6 at 10:38
Jo King
14k13881
14k13881
Sandbox (deleted)
â Jo King
Aug 6 at 10:39
4
Suggested test case: one where all elements are the same (i.e.[3,3,3] -> 9
). With all your current test cases filtering out any pairs where elements are the same (even for test cases like[2,3,3]
containing the same values) will still hold the correct test-results, but will fail for this test case because none will remain after filtering.
â Kevin Cruijssen
Aug 6 at 12:07
@Kevin Good suggestion, added
â Jo King
Aug 6 at 12:38
add a comment |Â
Sandbox (deleted)
â Jo King
Aug 6 at 10:39
4
Suggested test case: one where all elements are the same (i.e.[3,3,3] -> 9
). With all your current test cases filtering out any pairs where elements are the same (even for test cases like[2,3,3]
containing the same values) will still hold the correct test-results, but will fail for this test case because none will remain after filtering.
â Kevin Cruijssen
Aug 6 at 12:07
@Kevin Good suggestion, added
â Jo King
Aug 6 at 12:38
Sandbox (deleted)
â Jo King
Aug 6 at 10:39
Sandbox (deleted)
â Jo King
Aug 6 at 10:39
4
4
Suggested test case: one where all elements are the same (i.e.
[3,3,3] -> 9
). With all your current test cases filtering out any pairs where elements are the same (even for test cases like [2,3,3]
containing the same values) will still hold the correct test-results, but will fail for this test case because none will remain after filtering.â Kevin Cruijssen
Aug 6 at 12:07
Suggested test case: one where all elements are the same (i.e.
[3,3,3] -> 9
). With all your current test cases filtering out any pairs where elements are the same (even for test cases like [2,3,3]
containing the same values) will still hold the correct test-results, but will fail for this test case because none will remain after filtering.â Kevin Cruijssen
Aug 6 at 12:07
@Kevin Good suggestion, added
â Jo King
Aug 6 at 12:38
@Kevin Good suggestion, added
â Jo King
Aug 6 at 12:38
add a comment |Â
22 Answers
22
active
oldest
votes
up vote
10
down vote
R, 54 50 41 bytes
order(-tabulate(combn(scan(),2,prod)))[1]
Try it online!
Alternatively, for 54 53 44 bytes:
names(sort(-table(combn(scan(),2,prod))))[1]
Try it online!
In principle, the latter version outputs the relevant result even without the names
function, but followed by the count of such most frequent products, which isn't asked for...
Thanks to CriminallyVulgar for -4 and -1, and Giuseppe for -9 on both.
1
On the second on you can use -table() instead of descending=TRUE for -1. I really like the cleverness of the first one though. EDIT: Just realised you can also apply that to the first one for -4, so there's that. Try it online!
â CriminallyVulgar
Aug 6 at 12:59
1
combn(scan(),2,prod)
works instead of usingapply
â Giuseppe
Aug 6 at 13:24
add a comment |Â
up vote
8
down vote
Brachylog, 11 bytes
âÂÂÃÂÃÂᶠá»ÂtáµÂth
Try it online!
Explanation
ᶠFind all:
âÂÂÃÂàProduct of a subset of 2 elements
á»Âtáµ Order by occurrences
th Take the last element and discard the number of occurrences
I don't know how code golf usually works but aren't some of these characters outside the standard 256 code points and therefore multiple bytes each?
â Holloway
Aug 7 at 12:02
2
@Holloway Brachylog uses a custom code page
â Fatalize
Aug 7 at 12:07
add a comment |Â
up vote
6
down vote
Pyth, 12 bytes
eo/QN=*M.cQ2
Test suite
First, we take all 2 element combinations of the input without replacement (.cQ2
). Then, we map each of these pairs to their product (*M
). Next, we overwrite the input variable with the list of products (=
). Next, we sort the list of products by the number of occurrences in the list of products (o/QN
). Finally, the take the final element of the sorted list (e
).
add a comment |Â
up vote
6
down vote
MATL, 8 7 bytes
2XN!pXM
Try it online!
(-1 byte by using the method from @Mr. Xcoder's Jelly answer.)
2XN % nchoosek - get all combinations of 2 elements from input
!p % get the product of each combination
XM % 'mode': get the most common value from that
Older answer:
8 bytes
&*XRXzXM
Try it online!
&* % multiply input by its transpose,
% getting all elementwise products
XR % take the upper-triangular portion of that,
% zeroing out repetitions and mainly self-multiplications
Xz % remove the zeroed out parts
XM % 'mode' calculation - get the most common value from that
add a comment |Â
up vote
6
down vote
Mathematica, 32 bytes
-17 bytes (and a fix) thanks to JungHwan Min.
Commonest[1##&@@@#~Subsets~2]&
Pure function. Takes a list of numbers as input and returns the list of MCMs as output.
Actually it looks like both of us misread the question. This fails for input3, 3, 3
. Fixed:Commonest[1##&@@@#~Subsets~2]&
â JungHwan Min
Aug 6 at 20:21
@JungHwanMin Huh. I thought thatSubsets
didn't count repeats as separate elements. It seems like it does, though, so thanks!
â LegionMammal978
Aug 7 at 1:28
add a comment |Â
up vote
5
down vote
Jelly, 6 bytes
Ã
ÂcPâ¬ÃÂá¹Â
Try it online! or Check out the test suite.
How it works
à ÂcPâ¬ÃÂá¹ â Full program / Monadic link.
à Âc â Unordered pairs without replacement.
P⬠â Product of each.
ÃÂá¹ â Mode (most common element).
Alternative: à ÂcZPÃÂá¹Â
add a comment |Â
up vote
5
down vote
05AB1E, 8 6 bytes
æ2ùP.M
-2 bytes thanks to @Kaldo.
Try it online or verify all test cases.
Explanation:
æ # Take the powerset of the input-list
# i.e. [2,3,3] â [,[2],[3],[3],[2,3],[2,3],[3,3],[2,3,3]]
2ù # Leave only the inner lists of size 2:
# i.e. [,[2],[3],[3],[2,3],[2,3],[3,3],[2,3,3]] â [[2,3],[2,3],[3,3]]
P # Take the product of each remaining pair
# i.e. [[2,3],[2,3],[3,3]] â [6,6,9]
.M # Only leave the most frequent value(s) in the list
# i.e. [6,6,9] â [6]
1
æ2ùP.M for 6 bytes
â Kaldo
Aug 6 at 12:22
@Kaldo Thanks! Completely forgot aboutù
.
â Kevin Cruijssen
Aug 6 at 12:30
add a comment |Â
up vote
5
down vote
Perl 6, 41 bytes
key max bag(@_ X*@_)âÂÂ@_ûò: *.value:
Try it online!
Could you please explain to me (or point me to the docs) what are the colons doing there? I can't quite make it out... I can see that it has something to do with argument passing but nothing more.
â Ramillies
Aug 6 at 22:08
1
@Ramillies That's the infix:
operator.
â nwellnhof
Aug 6 at 23:18
Ah, I see. Thank you.
â Ramillies
Aug 6 at 23:22
add a comment |Â
up vote
5
down vote
MATLAB, 43 bytes
I=input('');i=I'*I*1-eye(nnz(I));mode(i(:))
It's also kind of a tongue twister!
Explanation
I=input(''); % Takes an input like "[2,3,4,5,6]"
i=I'*I % Multiplies the input by its own transverse
*1-eye(nnz(I)); % Multiplies by 1-identity matrix to remove diagonal
mode(i(:)) % Calculates most common value and prints it
i am not sure you need to doI'*I*1-eye
Why not justI'*I-eye
?
â aaaaaa
Aug 8 at 0:45
add a comment |Â
up vote
4
down vote
Stax, 12 10 bytes
â¢êv^âÂÂöâºâ£ÃÂû
Run and debug it
add a comment |Â
up vote
4
down vote
Python 3, 77 72 bytes
f=lambda k,*l,m=:l and f(*l,m=m+[k*x for x in l])or max(m,key=m.count)
Try it online!
add a comment |Â
up vote
4
down vote
JavaScript (ES6), 72 70 bytes
a=>a.map(m=o=(y,Y)=>a.map(x=>Y--<0?m=(o[x*=y]=-~o[x])<m?m:o[r=x]:0))|r
Try it online!
add a comment |Â
up vote
3
down vote
Charcoal, 24 bytes
WøFÃÂâÂÂøøâÂÂÃÂ
úIâÂÂæÃÂ
â¼âÂÂÃÂ
ùâÂÂï¼¥ÃÂ
âÂÂÃÂ
û
Try it online! Link is to verbose version of code. Explanation:
Wø
While the input array is non-empty...
ÃÂâÂÂøø
... pop the last element and multiply the rest of the array by that element...
F...âÂÂÃÂ
ú
... and push the results to the predefined empty list.
âÂÂï¼¥ÃÂ
âÂÂÃÂ
û
Count the number of times each product appears in the list and take the maximum...
æÃÂ
â¼âÂÂÃÂ
ù...
... then filter for the products whose count equals that maximum...
IâÂÂ
...then pop the last element and cast to string for implicit print.
add a comment |Â
up vote
3
down vote
Attache, 59 bytes
Last##~SortBy#`&&:`~##Flat[UpperTriangle&1!Table&_!`*]^^0
Try it online!
Still working on golfing this down a bit, but I think this is near optimal for the approach I've chosen.
Explanation
This is a composition of three functions:
Flat[UpperTriangle&1!Table&_!`*]^^0
SortBy#`&&:`~
Last
The first function does the bulk of the computation:
Flat[UpperTriangle&1!Table&_!`*]^^0
anonymous lambda; input: _ (e.g.: [2,3,4,5,6])
Table&_!`* shorthand for Table[`*, _]
this creates a multiplication table using the input
e.g.:
4 6 8 10 12
6 9 12 15 18
8 12 16 20 24
10 15 20 25 30
12 18 24 30 36
UpperTriangle&1! takes the strict upper triangle of this matrix
e.g.:
0 6 8 10 12
0 0 12 15 18
0 0 0 20 24
0 0 0 0 30
0 0 0 0 0
Flat[ ]^^0 flattens this list and removes all 0s
e.g.: [6, 8, 10, 12, 12, 15, 18, 20, 24, 30]
The second is a bit complicated but does something rather simple. First, it is useful to know that f&n
is a function which, when called with arguments ...x
, returns f[...x, n]
. f&:n
is similar, returning f[n, ...x]
. Now, let's decompose this:
( ~SortBy ) # (`& &: `~)
First, f#g
creates a fork. With input n
, it returns f[n, g[n]]
. However, in this case, f
is the function ~SortBy
. ~f
reverses the arguments of the function. This means that ~f#g
is equivalent to f[g[n], n]
, or here, SortBy[(`& &: `~)[n], n]
.
`& &: `~
follows the form f&:n
. But what are `&
and `~
? They are "operator quotes", and return a function equivalent to the quoted operator. So, in this case, `&
is the same thing as $ x & y
. With that in mind, this expression is equivalent to the following for binary operators:
f&:n <=> $ f[n, x]
<=> $ (`&)[`~, x]
<=> $ `~ & x
This yields the function `~&x
, where x
is the result from the first function. n ~ a
counts the occurrences of n
in a
. So, this returns a function which counts the occurrences of the argument in the computed array from function 1.
Going back to SortBy
, this each element in the array by the number of times it appears in it.
Finally, Last
takes the element which occurs the most. Ties are broken by the sorting algorithm.
Is the UpperTriangle part required? Can you just flatten the table and sort?
â svavil
Aug 7 at 12:14
@svavil Yes, it is required;[5, 2, 9, 10, 3, 4, 4, 4, 7] -> 16
instead of20
without it.
â Conor O'Brien
Aug 7 at 18:41
add a comment |Â
up vote
3
down vote
APL (Dyalog Unicode), 29 27 19 bytes
âÂÂ/â¢/â£èâ¸âÂÂâµâ³âÂÂâ¢âÂÂèâ¢ÃÂâÂÂ
Try it online!
Tacit fn.
Thanks to Adám for the tacit version and 2 bytes.
Thanks to ngn for 8 bytes!
How:
âÂÂ/â¢/â£èâ¸âÂÂâµâ³âÂÂâ¢âÂÂèâ¢ÃÂâÂÂ
â¢ÃÂâ â Multiply each element with the entire argument, then
â³âÂÂâ¢âÂÂè â Remove 1 from the first, two from the next etc. (removes repeated multiplications);
â The result is then fed into the function:
âÂÂâµ â Flatten the result;
â£è⸠â Key; creates a matrix in which each row corresponds to a unique product;
â¢/ â Get the rightmost column of the matrix;
âÂÂ/ â Get the highest value.
1
This is only 27.
â Adám
Aug 7 at 15:18
add a comment |Â
up vote
3
down vote
CJam, 70 68 bytes
q',/S*~_,(:LLX-,0a+[X1++*](;_X=_Y=@*fYfX]~;]__@e=$;_,(=
Try it online!
Explanation
q',/S*~ Turn input string into a valid CJam array
_,( Find the length of the array and subtract 1
:L Assign the result to L
fX Outer for loop
LX-,0a+[X1++*](; Create an array with all the array indexes bigger than X
fY Inner for loop
_X=_Y=@* Create a multiple of array[X] and array[Y] (Guaranteed to be from a unique combination of factors)
~;] Casts away all stack items except for an array of the multiples
__@e=$; Sorts array by number of occurrences (largest number of occurences at the end)
_,(= Gets the last element of the array
You will need to scroll right to see the explanations as the code is quite long, as well as the explanations.
This was an absolute nightmare to write. CJam doesn't have a powerset function (unlike a ton of other golfing languages - great choice on my part), which means that I had to manually find the powerset. However, this gave me the opportunity to ignore any invalid number of factors, unlike other answers with a powerset function.
This should be golfable, considering I'm terrible at CJam.
Changes:
Helen cut off 2 bytes!
Old: q',/S*~_,1-:LLX-,0a+[X1++*](;_X=_Y=@*fYfX]~;]__@e=$;_,1-=
New: q',/S*~_,(:LLX-,0a+[X1++*](;_X=_Y=@*fYfX]~;]__@e=$;_,(=
By changing the 1-
s to simply (
we get the same effect but with a lower byte count.
add a comment |Â
up vote
2
down vote
Java (JDK 10), 132 bytes
a->int l=a.length,i=l,j=0,r=99999,m=new int[r];for(;i-->0;)for(j=l;--j>i;)m[a[i]*a[j]]++;for(;r-->0;)i=m[r]<i?i:m[j=r];return j;
Try it online!
add a comment |Â
up vote
2
down vote
Japt -h
, 20 bytes
Not the best. I dont know how to make this code shorter
ãUsYàî*X
c n óÃÂ¥ ñÃÂo
Try it online!
add a comment |Â
up vote
2
down vote
Husk, 7 bytes
á¹ âº#màá¹Â2
Try it online!
Explanation
á¹ âº#màá¹Â2 -- example input [3,3,3]
á¹Â2 -- subsequences of length 2: [[3,3],[3,3],[3,3]]
mÃÂ -- map product: [9,9,9]
á¹ -- apply
# -- | count occurences in list
⺠-- to maxOn that list: [9]
add a comment |Â
up vote
2
down vote
Haskell, 96 94 bytes
-2 bytes thanks to nimi (using sortOn(0<$)
instead of length
)!
import Data.List
f a|b<-zip[0..]a=last.last.sortOn(0<$).group$sort[x*y|(i,x)<-b,(j,y)<-b,i/=j]
Try it online!
1
sortOn(0<$)
.
â nimi
Aug 7 at 19:26
add a comment |Â
up vote
2
down vote
MATLAB 39 bytes
a=input('');
b=triu(a'*a,1);
mode(b(b~=0))
Also check out answer by Jacob Watson
1
Having the second line beb=triu(a'*a,1);
saves 4 bytes.
â sundar
Aug 8 at 12:32
@sundar Oh snap, you are right :) I hadtriu
intially but drifted away somehow
â aaaaaa
Aug 8 at 14:47
Good solution, I didn't realise the upper triangle function was so short!
â Jacob Watson
Aug 10 at 8:19
add a comment |Â
up vote
2
down vote
SQL Server, 93 bytes
SELECT TOP 1a.a*b.a
FROM @ a
JOIN @ b ON a.i<b.i
GROUP BY a.a*b.a
ORDER BY COUNT(a.a*b.a)DESC
Input is assumed to come from a table of the form
DECLARE @ TABLE (A int, i int identity);
Example table population:
INSERT INTO @ VALUES (9), (7), (10), (9), (7), (8), (5), (10), (1);
Explanation:
I assume a "list of integers" will have an index associated with them, which in my case is the column i
. The column a
contains the values of the list.
I create products of every pair, where the left pair comes in the list earlier than the right pair. I then group on the product, and sort by the most populous number.
I'm a little sad I didn't get to use any cte or partitioning clauses, but they were just too long. SELECT
is a very expensive keyword.
Alternative, 183 bytes
WITH c
AS(SELECT a,ROW_NUMBER()OVER(ORDER BY a)r
FROM @),d AS(SELECT a.a*b.a p,COUNT(a.a*b.a)m
FROM c a
JOIN c b ON a.r<b.r GROUP BY a.a*b.a)SELECT TOP 1p
FROM d
ORDER BY m DESC
If SQL doesn't get to have a separate index column, here is a solution where I create an index using the ROW_NUMBER
function. I personally don't care about the order, but an order is required and using the a
column is the shortest.
add a comment |Â
22 Answers
22
active
oldest
votes
22 Answers
22
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
R, 54 50 41 bytes
order(-tabulate(combn(scan(),2,prod)))[1]
Try it online!
Alternatively, for 54 53 44 bytes:
names(sort(-table(combn(scan(),2,prod))))[1]
Try it online!
In principle, the latter version outputs the relevant result even without the names
function, but followed by the count of such most frequent products, which isn't asked for...
Thanks to CriminallyVulgar for -4 and -1, and Giuseppe for -9 on both.
1
On the second on you can use -table() instead of descending=TRUE for -1. I really like the cleverness of the first one though. EDIT: Just realised you can also apply that to the first one for -4, so there's that. Try it online!
â CriminallyVulgar
Aug 6 at 12:59
1
combn(scan(),2,prod)
works instead of usingapply
â Giuseppe
Aug 6 at 13:24
add a comment |Â
up vote
10
down vote
R, 54 50 41 bytes
order(-tabulate(combn(scan(),2,prod)))[1]
Try it online!
Alternatively, for 54 53 44 bytes:
names(sort(-table(combn(scan(),2,prod))))[1]
Try it online!
In principle, the latter version outputs the relevant result even without the names
function, but followed by the count of such most frequent products, which isn't asked for...
Thanks to CriminallyVulgar for -4 and -1, and Giuseppe for -9 on both.
1
On the second on you can use -table() instead of descending=TRUE for -1. I really like the cleverness of the first one though. EDIT: Just realised you can also apply that to the first one for -4, so there's that. Try it online!
â CriminallyVulgar
Aug 6 at 12:59
1
combn(scan(),2,prod)
works instead of usingapply
â Giuseppe
Aug 6 at 13:24
add a comment |Â
up vote
10
down vote
up vote
10
down vote
R, 54 50 41 bytes
order(-tabulate(combn(scan(),2,prod)))[1]
Try it online!
Alternatively, for 54 53 44 bytes:
names(sort(-table(combn(scan(),2,prod))))[1]
Try it online!
In principle, the latter version outputs the relevant result even without the names
function, but followed by the count of such most frequent products, which isn't asked for...
Thanks to CriminallyVulgar for -4 and -1, and Giuseppe for -9 on both.
R, 54 50 41 bytes
order(-tabulate(combn(scan(),2,prod)))[1]
Try it online!
Alternatively, for 54 53 44 bytes:
names(sort(-table(combn(scan(),2,prod))))[1]
Try it online!
In principle, the latter version outputs the relevant result even without the names
function, but followed by the count of such most frequent products, which isn't asked for...
Thanks to CriminallyVulgar for -4 and -1, and Giuseppe for -9 on both.
edited Aug 6 at 13:29
answered Aug 6 at 12:50
Kirill L.
2,0961114
2,0961114
1
On the second on you can use -table() instead of descending=TRUE for -1. I really like the cleverness of the first one though. EDIT: Just realised you can also apply that to the first one for -4, so there's that. Try it online!
â CriminallyVulgar
Aug 6 at 12:59
1
combn(scan(),2,prod)
works instead of usingapply
â Giuseppe
Aug 6 at 13:24
add a comment |Â
1
On the second on you can use -table() instead of descending=TRUE for -1. I really like the cleverness of the first one though. EDIT: Just realised you can also apply that to the first one for -4, so there's that. Try it online!
â CriminallyVulgar
Aug 6 at 12:59
1
combn(scan(),2,prod)
works instead of usingapply
â Giuseppe
Aug 6 at 13:24
1
1
On the second on you can use -table() instead of descending=TRUE for -1. I really like the cleverness of the first one though. EDIT: Just realised you can also apply that to the first one for -4, so there's that. Try it online!
â CriminallyVulgar
Aug 6 at 12:59
On the second on you can use -table() instead of descending=TRUE for -1. I really like the cleverness of the first one though. EDIT: Just realised you can also apply that to the first one for -4, so there's that. Try it online!
â CriminallyVulgar
Aug 6 at 12:59
1
1
combn(scan(),2,prod)
works instead of using apply
â Giuseppe
Aug 6 at 13:24
combn(scan(),2,prod)
works instead of using apply
â Giuseppe
Aug 6 at 13:24
add a comment |Â
up vote
8
down vote
Brachylog, 11 bytes
âÂÂÃÂÃÂᶠá»ÂtáµÂth
Try it online!
Explanation
ᶠFind all:
âÂÂÃÂàProduct of a subset of 2 elements
á»Âtáµ Order by occurrences
th Take the last element and discard the number of occurrences
I don't know how code golf usually works but aren't some of these characters outside the standard 256 code points and therefore multiple bytes each?
â Holloway
Aug 7 at 12:02
2
@Holloway Brachylog uses a custom code page
â Fatalize
Aug 7 at 12:07
add a comment |Â
up vote
8
down vote
Brachylog, 11 bytes
âÂÂÃÂÃÂᶠá»ÂtáµÂth
Try it online!
Explanation
ᶠFind all:
âÂÂÃÂàProduct of a subset of 2 elements
á»Âtáµ Order by occurrences
th Take the last element and discard the number of occurrences
I don't know how code golf usually works but aren't some of these characters outside the standard 256 code points and therefore multiple bytes each?
â Holloway
Aug 7 at 12:02
2
@Holloway Brachylog uses a custom code page
â Fatalize
Aug 7 at 12:07
add a comment |Â
up vote
8
down vote
up vote
8
down vote
Brachylog, 11 bytes
âÂÂÃÂÃÂᶠá»ÂtáµÂth
Try it online!
Explanation
ᶠFind all:
âÂÂÃÂàProduct of a subset of 2 elements
á»Âtáµ Order by occurrences
th Take the last element and discard the number of occurrences
Brachylog, 11 bytes
âÂÂÃÂÃÂᶠá»ÂtáµÂth
Try it online!
Explanation
ᶠFind all:
âÂÂÃÂàProduct of a subset of 2 elements
á»Âtáµ Order by occurrences
th Take the last element and discard the number of occurrences
answered Aug 6 at 11:23
Fatalize
26.5k448133
26.5k448133
I don't know how code golf usually works but aren't some of these characters outside the standard 256 code points and therefore multiple bytes each?
â Holloway
Aug 7 at 12:02
2
@Holloway Brachylog uses a custom code page
â Fatalize
Aug 7 at 12:07
add a comment |Â
I don't know how code golf usually works but aren't some of these characters outside the standard 256 code points and therefore multiple bytes each?
â Holloway
Aug 7 at 12:02
2
@Holloway Brachylog uses a custom code page
â Fatalize
Aug 7 at 12:07
I don't know how code golf usually works but aren't some of these characters outside the standard 256 code points and therefore multiple bytes each?
â Holloway
Aug 7 at 12:02
I don't know how code golf usually works but aren't some of these characters outside the standard 256 code points and therefore multiple bytes each?
â Holloway
Aug 7 at 12:02
2
2
@Holloway Brachylog uses a custom code page
â Fatalize
Aug 7 at 12:07
@Holloway Brachylog uses a custom code page
â Fatalize
Aug 7 at 12:07
add a comment |Â
up vote
6
down vote
Pyth, 12 bytes
eo/QN=*M.cQ2
Test suite
First, we take all 2 element combinations of the input without replacement (.cQ2
). Then, we map each of these pairs to their product (*M
). Next, we overwrite the input variable with the list of products (=
). Next, we sort the list of products by the number of occurrences in the list of products (o/QN
). Finally, the take the final element of the sorted list (e
).
add a comment |Â
up vote
6
down vote
Pyth, 12 bytes
eo/QN=*M.cQ2
Test suite
First, we take all 2 element combinations of the input without replacement (.cQ2
). Then, we map each of these pairs to their product (*M
). Next, we overwrite the input variable with the list of products (=
). Next, we sort the list of products by the number of occurrences in the list of products (o/QN
). Finally, the take the final element of the sorted list (e
).
add a comment |Â
up vote
6
down vote
up vote
6
down vote
Pyth, 12 bytes
eo/QN=*M.cQ2
Test suite
First, we take all 2 element combinations of the input without replacement (.cQ2
). Then, we map each of these pairs to their product (*M
). Next, we overwrite the input variable with the list of products (=
). Next, we sort the list of products by the number of occurrences in the list of products (o/QN
). Finally, the take the final element of the sorted list (e
).
Pyth, 12 bytes
eo/QN=*M.cQ2
Test suite
First, we take all 2 element combinations of the input without replacement (.cQ2
). Then, we map each of these pairs to their product (*M
). Next, we overwrite the input variable with the list of products (=
). Next, we sort the list of products by the number of occurrences in the list of products (o/QN
). Finally, the take the final element of the sorted list (e
).
answered Aug 6 at 10:51
isaacg
34.4k553185
34.4k553185
add a comment |Â
add a comment |Â
up vote
6
down vote
MATL, 8 7 bytes
2XN!pXM
Try it online!
(-1 byte by using the method from @Mr. Xcoder's Jelly answer.)
2XN % nchoosek - get all combinations of 2 elements from input
!p % get the product of each combination
XM % 'mode': get the most common value from that
Older answer:
8 bytes
&*XRXzXM
Try it online!
&* % multiply input by its transpose,
% getting all elementwise products
XR % take the upper-triangular portion of that,
% zeroing out repetitions and mainly self-multiplications
Xz % remove the zeroed out parts
XM % 'mode' calculation - get the most common value from that
add a comment |Â
up vote
6
down vote
MATL, 8 7 bytes
2XN!pXM
Try it online!
(-1 byte by using the method from @Mr. Xcoder's Jelly answer.)
2XN % nchoosek - get all combinations of 2 elements from input
!p % get the product of each combination
XM % 'mode': get the most common value from that
Older answer:
8 bytes
&*XRXzXM
Try it online!
&* % multiply input by its transpose,
% getting all elementwise products
XR % take the upper-triangular portion of that,
% zeroing out repetitions and mainly self-multiplications
Xz % remove the zeroed out parts
XM % 'mode' calculation - get the most common value from that
add a comment |Â
up vote
6
down vote
up vote
6
down vote
MATL, 8 7 bytes
2XN!pXM
Try it online!
(-1 byte by using the method from @Mr. Xcoder's Jelly answer.)
2XN % nchoosek - get all combinations of 2 elements from input
!p % get the product of each combination
XM % 'mode': get the most common value from that
Older answer:
8 bytes
&*XRXzXM
Try it online!
&* % multiply input by its transpose,
% getting all elementwise products
XR % take the upper-triangular portion of that,
% zeroing out repetitions and mainly self-multiplications
Xz % remove the zeroed out parts
XM % 'mode' calculation - get the most common value from that
MATL, 8 7 bytes
2XN!pXM
Try it online!
(-1 byte by using the method from @Mr. Xcoder's Jelly answer.)
2XN % nchoosek - get all combinations of 2 elements from input
!p % get the product of each combination
XM % 'mode': get the most common value from that
Older answer:
8 bytes
&*XRXzXM
Try it online!
&* % multiply input by its transpose,
% getting all elementwise products
XR % take the upper-triangular portion of that,
% zeroing out repetitions and mainly self-multiplications
Xz % remove the zeroed out parts
XM % 'mode' calculation - get the most common value from that
edited Aug 6 at 13:37
answered Aug 6 at 13:27
sundar
4,383828
4,383828
add a comment |Â
add a comment |Â
up vote
6
down vote
Mathematica, 32 bytes
-17 bytes (and a fix) thanks to JungHwan Min.
Commonest[1##&@@@#~Subsets~2]&
Pure function. Takes a list of numbers as input and returns the list of MCMs as output.
Actually it looks like both of us misread the question. This fails for input3, 3, 3
. Fixed:Commonest[1##&@@@#~Subsets~2]&
â JungHwan Min
Aug 6 at 20:21
@JungHwanMin Huh. I thought thatSubsets
didn't count repeats as separate elements. It seems like it does, though, so thanks!
â LegionMammal978
Aug 7 at 1:28
add a comment |Â
up vote
6
down vote
Mathematica, 32 bytes
-17 bytes (and a fix) thanks to JungHwan Min.
Commonest[1##&@@@#~Subsets~2]&
Pure function. Takes a list of numbers as input and returns the list of MCMs as output.
Actually it looks like both of us misread the question. This fails for input3, 3, 3
. Fixed:Commonest[1##&@@@#~Subsets~2]&
â JungHwan Min
Aug 6 at 20:21
@JungHwanMin Huh. I thought thatSubsets
didn't count repeats as separate elements. It seems like it does, though, so thanks!
â LegionMammal978
Aug 7 at 1:28
add a comment |Â
up vote
6
down vote
up vote
6
down vote
Mathematica, 32 bytes
-17 bytes (and a fix) thanks to JungHwan Min.
Commonest[1##&@@@#~Subsets~2]&
Pure function. Takes a list of numbers as input and returns the list of MCMs as output.
Mathematica, 32 bytes
-17 bytes (and a fix) thanks to JungHwan Min.
Commonest[1##&@@@#~Subsets~2]&
Pure function. Takes a list of numbers as input and returns the list of MCMs as output.
edited Aug 7 at 1:29
answered Aug 6 at 10:53
LegionMammal978
14.7k41752
14.7k41752
Actually it looks like both of us misread the question. This fails for input3, 3, 3
. Fixed:Commonest[1##&@@@#~Subsets~2]&
â JungHwan Min
Aug 6 at 20:21
@JungHwanMin Huh. I thought thatSubsets
didn't count repeats as separate elements. It seems like it does, though, so thanks!
â LegionMammal978
Aug 7 at 1:28
add a comment |Â
Actually it looks like both of us misread the question. This fails for input3, 3, 3
. Fixed:Commonest[1##&@@@#~Subsets~2]&
â JungHwan Min
Aug 6 at 20:21
@JungHwanMin Huh. I thought thatSubsets
didn't count repeats as separate elements. It seems like it does, though, so thanks!
â LegionMammal978
Aug 7 at 1:28
Actually it looks like both of us misread the question. This fails for input
3, 3, 3
. Fixed: Commonest[1##&@@@#~Subsets~2]&
â JungHwan Min
Aug 6 at 20:21
Actually it looks like both of us misread the question. This fails for input
3, 3, 3
. Fixed: Commonest[1##&@@@#~Subsets~2]&
â JungHwan Min
Aug 6 at 20:21
@JungHwanMin Huh. I thought that
Subsets
didn't count repeats as separate elements. It seems like it does, though, so thanks!â LegionMammal978
Aug 7 at 1:28
@JungHwanMin Huh. I thought that
Subsets
didn't count repeats as separate elements. It seems like it does, though, so thanks!â LegionMammal978
Aug 7 at 1:28
add a comment |Â
up vote
5
down vote
Jelly, 6 bytes
Ã
ÂcPâ¬ÃÂá¹Â
Try it online! or Check out the test suite.
How it works
à ÂcPâ¬ÃÂá¹ â Full program / Monadic link.
à Âc â Unordered pairs without replacement.
P⬠â Product of each.
ÃÂá¹ â Mode (most common element).
Alternative: à ÂcZPÃÂá¹Â
add a comment |Â
up vote
5
down vote
Jelly, 6 bytes
Ã
ÂcPâ¬ÃÂá¹Â
Try it online! or Check out the test suite.
How it works
à ÂcPâ¬ÃÂá¹ â Full program / Monadic link.
à Âc â Unordered pairs without replacement.
P⬠â Product of each.
ÃÂá¹ â Mode (most common element).
Alternative: à ÂcZPÃÂá¹Â
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Jelly, 6 bytes
Ã
ÂcPâ¬ÃÂá¹Â
Try it online! or Check out the test suite.
How it works
à ÂcPâ¬ÃÂá¹ â Full program / Monadic link.
à Âc â Unordered pairs without replacement.
P⬠â Product of each.
ÃÂá¹ â Mode (most common element).
Alternative: à ÂcZPÃÂá¹Â
Jelly, 6 bytes
Ã
ÂcPâ¬ÃÂá¹Â
Try it online! or Check out the test suite.
How it works
à ÂcPâ¬ÃÂá¹ â Full program / Monadic link.
à Âc â Unordered pairs without replacement.
P⬠â Product of each.
ÃÂá¹ â Mode (most common element).
Alternative: à ÂcZPÃÂá¹Â
edited Aug 6 at 11:18
answered Aug 6 at 11:10
Mr. Xcoder
29.4k756192
29.4k756192
add a comment |Â
add a comment |Â
up vote
5
down vote
05AB1E, 8 6 bytes
æ2ùP.M
-2 bytes thanks to @Kaldo.
Try it online or verify all test cases.
Explanation:
æ # Take the powerset of the input-list
# i.e. [2,3,3] â [,[2],[3],[3],[2,3],[2,3],[3,3],[2,3,3]]
2ù # Leave only the inner lists of size 2:
# i.e. [,[2],[3],[3],[2,3],[2,3],[3,3],[2,3,3]] â [[2,3],[2,3],[3,3]]
P # Take the product of each remaining pair
# i.e. [[2,3],[2,3],[3,3]] â [6,6,9]
.M # Only leave the most frequent value(s) in the list
# i.e. [6,6,9] â [6]
1
æ2ùP.M for 6 bytes
â Kaldo
Aug 6 at 12:22
@Kaldo Thanks! Completely forgot aboutù
.
â Kevin Cruijssen
Aug 6 at 12:30
add a comment |Â
up vote
5
down vote
05AB1E, 8 6 bytes
æ2ùP.M
-2 bytes thanks to @Kaldo.
Try it online or verify all test cases.
Explanation:
æ # Take the powerset of the input-list
# i.e. [2,3,3] â [,[2],[3],[3],[2,3],[2,3],[3,3],[2,3,3]]
2ù # Leave only the inner lists of size 2:
# i.e. [,[2],[3],[3],[2,3],[2,3],[3,3],[2,3,3]] â [[2,3],[2,3],[3,3]]
P # Take the product of each remaining pair
# i.e. [[2,3],[2,3],[3,3]] â [6,6,9]
.M # Only leave the most frequent value(s) in the list
# i.e. [6,6,9] â [6]
1
æ2ùP.M for 6 bytes
â Kaldo
Aug 6 at 12:22
@Kaldo Thanks! Completely forgot aboutù
.
â Kevin Cruijssen
Aug 6 at 12:30
add a comment |Â
up vote
5
down vote
up vote
5
down vote
05AB1E, 8 6 bytes
æ2ùP.M
-2 bytes thanks to @Kaldo.
Try it online or verify all test cases.
Explanation:
æ # Take the powerset of the input-list
# i.e. [2,3,3] â [,[2],[3],[3],[2,3],[2,3],[3,3],[2,3,3]]
2ù # Leave only the inner lists of size 2:
# i.e. [,[2],[3],[3],[2,3],[2,3],[3,3],[2,3,3]] â [[2,3],[2,3],[3,3]]
P # Take the product of each remaining pair
# i.e. [[2,3],[2,3],[3,3]] â [6,6,9]
.M # Only leave the most frequent value(s) in the list
# i.e. [6,6,9] â [6]
05AB1E, 8 6 bytes
æ2ùP.M
-2 bytes thanks to @Kaldo.
Try it online or verify all test cases.
Explanation:
æ # Take the powerset of the input-list
# i.e. [2,3,3] â [,[2],[3],[3],[2,3],[2,3],[3,3],[2,3,3]]
2ù # Leave only the inner lists of size 2:
# i.e. [,[2],[3],[3],[2,3],[2,3],[3,3],[2,3,3]] â [[2,3],[2,3],[3,3]]
P # Take the product of each remaining pair
# i.e. [[2,3],[2,3],[3,3]] â [6,6,9]
.M # Only leave the most frequent value(s) in the list
# i.e. [6,6,9] â [6]
edited Aug 6 at 12:30
answered Aug 6 at 12:05
Kevin Cruijssen
28.1k546158
28.1k546158
1
æ2ùP.M for 6 bytes
â Kaldo
Aug 6 at 12:22
@Kaldo Thanks! Completely forgot aboutù
.
â Kevin Cruijssen
Aug 6 at 12:30
add a comment |Â
1
æ2ùP.M for 6 bytes
â Kaldo
Aug 6 at 12:22
@Kaldo Thanks! Completely forgot aboutù
.
â Kevin Cruijssen
Aug 6 at 12:30
1
1
æ2ùP.M for 6 bytes
â Kaldo
Aug 6 at 12:22
æ2ùP.M for 6 bytes
â Kaldo
Aug 6 at 12:22
@Kaldo Thanks! Completely forgot about
ù
.â Kevin Cruijssen
Aug 6 at 12:30
@Kaldo Thanks! Completely forgot about
ù
.â Kevin Cruijssen
Aug 6 at 12:30
add a comment |Â
up vote
5
down vote
Perl 6, 41 bytes
key max bag(@_ X*@_)âÂÂ@_ûò: *.value:
Try it online!
Could you please explain to me (or point me to the docs) what are the colons doing there? I can't quite make it out... I can see that it has something to do with argument passing but nothing more.
â Ramillies
Aug 6 at 22:08
1
@Ramillies That's the infix:
operator.
â nwellnhof
Aug 6 at 23:18
Ah, I see. Thank you.
â Ramillies
Aug 6 at 23:22
add a comment |Â
up vote
5
down vote
Perl 6, 41 bytes
key max bag(@_ X*@_)âÂÂ@_ûò: *.value:
Try it online!
Could you please explain to me (or point me to the docs) what are the colons doing there? I can't quite make it out... I can see that it has something to do with argument passing but nothing more.
â Ramillies
Aug 6 at 22:08
1
@Ramillies That's the infix:
operator.
â nwellnhof
Aug 6 at 23:18
Ah, I see. Thank you.
â Ramillies
Aug 6 at 23:22
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Perl 6, 41 bytes
key max bag(@_ X*@_)âÂÂ@_ûò: *.value:
Try it online!
Perl 6, 41 bytes
key max bag(@_ X*@_)âÂÂ@_ûò: *.value:
Try it online!
edited Aug 6 at 23:20
answered Aug 6 at 11:19
nwellnhof
3,053613
3,053613
Could you please explain to me (or point me to the docs) what are the colons doing there? I can't quite make it out... I can see that it has something to do with argument passing but nothing more.
â Ramillies
Aug 6 at 22:08
1
@Ramillies That's the infix:
operator.
â nwellnhof
Aug 6 at 23:18
Ah, I see. Thank you.
â Ramillies
Aug 6 at 23:22
add a comment |Â
Could you please explain to me (or point me to the docs) what are the colons doing there? I can't quite make it out... I can see that it has something to do with argument passing but nothing more.
â Ramillies
Aug 6 at 22:08
1
@Ramillies That's the infix:
operator.
â nwellnhof
Aug 6 at 23:18
Ah, I see. Thank you.
â Ramillies
Aug 6 at 23:22
Could you please explain to me (or point me to the docs) what are the colons doing there? I can't quite make it out... I can see that it has something to do with argument passing but nothing more.
â Ramillies
Aug 6 at 22:08
Could you please explain to me (or point me to the docs) what are the colons doing there? I can't quite make it out... I can see that it has something to do with argument passing but nothing more.
â Ramillies
Aug 6 at 22:08
1
1
@Ramillies That's the infix
:
operator.â nwellnhof
Aug 6 at 23:18
@Ramillies That's the infix
:
operator.â nwellnhof
Aug 6 at 23:18
Ah, I see. Thank you.
â Ramillies
Aug 6 at 23:22
Ah, I see. Thank you.
â Ramillies
Aug 6 at 23:22
add a comment |Â
up vote
5
down vote
MATLAB, 43 bytes
I=input('');i=I'*I*1-eye(nnz(I));mode(i(:))
It's also kind of a tongue twister!
Explanation
I=input(''); % Takes an input like "[2,3,4,5,6]"
i=I'*I % Multiplies the input by its own transverse
*1-eye(nnz(I)); % Multiplies by 1-identity matrix to remove diagonal
mode(i(:)) % Calculates most common value and prints it
i am not sure you need to doI'*I*1-eye
Why not justI'*I-eye
?
â aaaaaa
Aug 8 at 0:45
add a comment |Â
up vote
5
down vote
MATLAB, 43 bytes
I=input('');i=I'*I*1-eye(nnz(I));mode(i(:))
It's also kind of a tongue twister!
Explanation
I=input(''); % Takes an input like "[2,3,4,5,6]"
i=I'*I % Multiplies the input by its own transverse
*1-eye(nnz(I)); % Multiplies by 1-identity matrix to remove diagonal
mode(i(:)) % Calculates most common value and prints it
i am not sure you need to doI'*I*1-eye
Why not justI'*I-eye
?
â aaaaaa
Aug 8 at 0:45
add a comment |Â
up vote
5
down vote
up vote
5
down vote
MATLAB, 43 bytes
I=input('');i=I'*I*1-eye(nnz(I));mode(i(:))
It's also kind of a tongue twister!
Explanation
I=input(''); % Takes an input like "[2,3,4,5,6]"
i=I'*I % Multiplies the input by its own transverse
*1-eye(nnz(I)); % Multiplies by 1-identity matrix to remove diagonal
mode(i(:)) % Calculates most common value and prints it
MATLAB, 43 bytes
I=input('');i=I'*I*1-eye(nnz(I));mode(i(:))
It's also kind of a tongue twister!
Explanation
I=input(''); % Takes an input like "[2,3,4,5,6]"
i=I'*I % Multiplies the input by its own transverse
*1-eye(nnz(I)); % Multiplies by 1-identity matrix to remove diagonal
mode(i(:)) % Calculates most common value and prints it
edited Aug 7 at 8:22
answered Aug 6 at 16:02
Jacob Watson
1313
1313
i am not sure you need to doI'*I*1-eye
Why not justI'*I-eye
?
â aaaaaa
Aug 8 at 0:45
add a comment |Â
i am not sure you need to doI'*I*1-eye
Why not justI'*I-eye
?
â aaaaaa
Aug 8 at 0:45
i am not sure you need to do
I'*I*1-eye
Why not just I'*I-eye
?â aaaaaa
Aug 8 at 0:45
i am not sure you need to do
I'*I*1-eye
Why not just I'*I-eye
?â aaaaaa
Aug 8 at 0:45
add a comment |Â
up vote
4
down vote
Stax, 12 10 bytes
â¢êv^âÂÂöâºâ£ÃÂû
Run and debug it
add a comment |Â
up vote
4
down vote
Stax, 12 10 bytes
â¢êv^âÂÂöâºâ£ÃÂû
Run and debug it
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Stax, 12 10 bytes
â¢êv^âÂÂöâºâ£ÃÂû
Run and debug it
Stax, 12 10 bytes
â¢êv^âÂÂöâºâ£ÃÂû
Run and debug it
edited Aug 6 at 11:27
answered Aug 6 at 11:13
wastl
1,884424
1,884424
add a comment |Â
add a comment |Â
up vote
4
down vote
Python 3, 77 72 bytes
f=lambda k,*l,m=:l and f(*l,m=m+[k*x for x in l])or max(m,key=m.count)
Try it online!
add a comment |Â
up vote
4
down vote
Python 3, 77 72 bytes
f=lambda k,*l,m=:l and f(*l,m=m+[k*x for x in l])or max(m,key=m.count)
Try it online!
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Python 3, 77 72 bytes
f=lambda k,*l,m=:l and f(*l,m=m+[k*x for x in l])or max(m,key=m.count)
Try it online!
Python 3, 77 72 bytes
f=lambda k,*l,m=:l and f(*l,m=m+[k*x for x in l])or max(m,key=m.count)
Try it online!
edited Aug 6 at 12:42
answered Aug 6 at 11:53
ovs
16.6k2956
16.6k2956
add a comment |Â
add a comment |Â
up vote
4
down vote
JavaScript (ES6), 72 70 bytes
a=>a.map(m=o=(y,Y)=>a.map(x=>Y--<0?m=(o[x*=y]=-~o[x])<m?m:o[r=x]:0))|r
Try it online!
add a comment |Â
up vote
4
down vote
JavaScript (ES6), 72 70 bytes
a=>a.map(m=o=(y,Y)=>a.map(x=>Y--<0?m=(o[x*=y]=-~o[x])<m?m:o[r=x]:0))|r
Try it online!
add a comment |Â
up vote
4
down vote
up vote
4
down vote
JavaScript (ES6), 72 70 bytes
a=>a.map(m=o=(y,Y)=>a.map(x=>Y--<0?m=(o[x*=y]=-~o[x])<m?m:o[r=x]:0))|r
Try it online!
JavaScript (ES6), 72 70 bytes
a=>a.map(m=o=(y,Y)=>a.map(x=>Y--<0?m=(o[x*=y]=-~o[x])<m?m:o[r=x]:0))|r
Try it online!
edited Aug 6 at 23:13
answered Aug 6 at 11:18
Arnauld
61.6k575258
61.6k575258
add a comment |Â
add a comment |Â
up vote
3
down vote
Charcoal, 24 bytes
WøFÃÂâÂÂøøâÂÂÃÂ
úIâÂÂæÃÂ
â¼âÂÂÃÂ
ùâÂÂï¼¥ÃÂ
âÂÂÃÂ
û
Try it online! Link is to verbose version of code. Explanation:
Wø
While the input array is non-empty...
ÃÂâÂÂøø
... pop the last element and multiply the rest of the array by that element...
F...âÂÂÃÂ
ú
... and push the results to the predefined empty list.
âÂÂï¼¥ÃÂ
âÂÂÃÂ
û
Count the number of times each product appears in the list and take the maximum...
æÃÂ
â¼âÂÂÃÂ
ù...
... then filter for the products whose count equals that maximum...
IâÂÂ
...then pop the last element and cast to string for implicit print.
add a comment |Â
up vote
3
down vote
Charcoal, 24 bytes
WøFÃÂâÂÂøøâÂÂÃÂ
úIâÂÂæÃÂ
â¼âÂÂÃÂ
ùâÂÂï¼¥ÃÂ
âÂÂÃÂ
û
Try it online! Link is to verbose version of code. Explanation:
Wø
While the input array is non-empty...
ÃÂâÂÂøø
... pop the last element and multiply the rest of the array by that element...
F...âÂÂÃÂ
ú
... and push the results to the predefined empty list.
âÂÂï¼¥ÃÂ
âÂÂÃÂ
û
Count the number of times each product appears in the list and take the maximum...
æÃÂ
â¼âÂÂÃÂ
ù...
... then filter for the products whose count equals that maximum...
IâÂÂ
...then pop the last element and cast to string for implicit print.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Charcoal, 24 bytes
WøFÃÂâÂÂøøâÂÂÃÂ
úIâÂÂæÃÂ
â¼âÂÂÃÂ
ùâÂÂï¼¥ÃÂ
âÂÂÃÂ
û
Try it online! Link is to verbose version of code. Explanation:
Wø
While the input array is non-empty...
ÃÂâÂÂøø
... pop the last element and multiply the rest of the array by that element...
F...âÂÂÃÂ
ú
... and push the results to the predefined empty list.
âÂÂï¼¥ÃÂ
âÂÂÃÂ
û
Count the number of times each product appears in the list and take the maximum...
æÃÂ
â¼âÂÂÃÂ
ù...
... then filter for the products whose count equals that maximum...
IâÂÂ
...then pop the last element and cast to string for implicit print.
Charcoal, 24 bytes
WøFÃÂâÂÂøøâÂÂÃÂ
úIâÂÂæÃÂ
â¼âÂÂÃÂ
ùâÂÂï¼¥ÃÂ
âÂÂÃÂ
û
Try it online! Link is to verbose version of code. Explanation:
Wø
While the input array is non-empty...
ÃÂâÂÂøø
... pop the last element and multiply the rest of the array by that element...
F...âÂÂÃÂ
ú
... and push the results to the predefined empty list.
âÂÂï¼¥ÃÂ
âÂÂÃÂ
û
Count the number of times each product appears in the list and take the maximum...
æÃÂ
â¼âÂÂÃÂ
ù...
... then filter for the products whose count equals that maximum...
IâÂÂ
...then pop the last element and cast to string for implicit print.
answered Aug 6 at 13:41
Neil
74k743169
74k743169
add a comment |Â
add a comment |Â
up vote
3
down vote
Attache, 59 bytes
Last##~SortBy#`&&:`~##Flat[UpperTriangle&1!Table&_!`*]^^0
Try it online!
Still working on golfing this down a bit, but I think this is near optimal for the approach I've chosen.
Explanation
This is a composition of three functions:
Flat[UpperTriangle&1!Table&_!`*]^^0
SortBy#`&&:`~
Last
The first function does the bulk of the computation:
Flat[UpperTriangle&1!Table&_!`*]^^0
anonymous lambda; input: _ (e.g.: [2,3,4,5,6])
Table&_!`* shorthand for Table[`*, _]
this creates a multiplication table using the input
e.g.:
4 6 8 10 12
6 9 12 15 18
8 12 16 20 24
10 15 20 25 30
12 18 24 30 36
UpperTriangle&1! takes the strict upper triangle of this matrix
e.g.:
0 6 8 10 12
0 0 12 15 18
0 0 0 20 24
0 0 0 0 30
0 0 0 0 0
Flat[ ]^^0 flattens this list and removes all 0s
e.g.: [6, 8, 10, 12, 12, 15, 18, 20, 24, 30]
The second is a bit complicated but does something rather simple. First, it is useful to know that f&n
is a function which, when called with arguments ...x
, returns f[...x, n]
. f&:n
is similar, returning f[n, ...x]
. Now, let's decompose this:
( ~SortBy ) # (`& &: `~)
First, f#g
creates a fork. With input n
, it returns f[n, g[n]]
. However, in this case, f
is the function ~SortBy
. ~f
reverses the arguments of the function. This means that ~f#g
is equivalent to f[g[n], n]
, or here, SortBy[(`& &: `~)[n], n]
.
`& &: `~
follows the form f&:n
. But what are `&
and `~
? They are "operator quotes", and return a function equivalent to the quoted operator. So, in this case, `&
is the same thing as $ x & y
. With that in mind, this expression is equivalent to the following for binary operators:
f&:n <=> $ f[n, x]
<=> $ (`&)[`~, x]
<=> $ `~ & x
This yields the function `~&x
, where x
is the result from the first function. n ~ a
counts the occurrences of n
in a
. So, this returns a function which counts the occurrences of the argument in the computed array from function 1.
Going back to SortBy
, this each element in the array by the number of times it appears in it.
Finally, Last
takes the element which occurs the most. Ties are broken by the sorting algorithm.
Is the UpperTriangle part required? Can you just flatten the table and sort?
â svavil
Aug 7 at 12:14
@svavil Yes, it is required;[5, 2, 9, 10, 3, 4, 4, 4, 7] -> 16
instead of20
without it.
â Conor O'Brien
Aug 7 at 18:41
add a comment |Â
up vote
3
down vote
Attache, 59 bytes
Last##~SortBy#`&&:`~##Flat[UpperTriangle&1!Table&_!`*]^^0
Try it online!
Still working on golfing this down a bit, but I think this is near optimal for the approach I've chosen.
Explanation
This is a composition of three functions:
Flat[UpperTriangle&1!Table&_!`*]^^0
SortBy#`&&:`~
Last
The first function does the bulk of the computation:
Flat[UpperTriangle&1!Table&_!`*]^^0
anonymous lambda; input: _ (e.g.: [2,3,4,5,6])
Table&_!`* shorthand for Table[`*, _]
this creates a multiplication table using the input
e.g.:
4 6 8 10 12
6 9 12 15 18
8 12 16 20 24
10 15 20 25 30
12 18 24 30 36
UpperTriangle&1! takes the strict upper triangle of this matrix
e.g.:
0 6 8 10 12
0 0 12 15 18
0 0 0 20 24
0 0 0 0 30
0 0 0 0 0
Flat[ ]^^0 flattens this list and removes all 0s
e.g.: [6, 8, 10, 12, 12, 15, 18, 20, 24, 30]
The second is a bit complicated but does something rather simple. First, it is useful to know that f&n
is a function which, when called with arguments ...x
, returns f[...x, n]
. f&:n
is similar, returning f[n, ...x]
. Now, let's decompose this:
( ~SortBy ) # (`& &: `~)
First, f#g
creates a fork. With input n
, it returns f[n, g[n]]
. However, in this case, f
is the function ~SortBy
. ~f
reverses the arguments of the function. This means that ~f#g
is equivalent to f[g[n], n]
, or here, SortBy[(`& &: `~)[n], n]
.
`& &: `~
follows the form f&:n
. But what are `&
and `~
? They are "operator quotes", and return a function equivalent to the quoted operator. So, in this case, `&
is the same thing as $ x & y
. With that in mind, this expression is equivalent to the following for binary operators:
f&:n <=> $ f[n, x]
<=> $ (`&)[`~, x]
<=> $ `~ & x
This yields the function `~&x
, where x
is the result from the first function. n ~ a
counts the occurrences of n
in a
. So, this returns a function which counts the occurrences of the argument in the computed array from function 1.
Going back to SortBy
, this each element in the array by the number of times it appears in it.
Finally, Last
takes the element which occurs the most. Ties are broken by the sorting algorithm.
Is the UpperTriangle part required? Can you just flatten the table and sort?
â svavil
Aug 7 at 12:14
@svavil Yes, it is required;[5, 2, 9, 10, 3, 4, 4, 4, 7] -> 16
instead of20
without it.
â Conor O'Brien
Aug 7 at 18:41
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Attache, 59 bytes
Last##~SortBy#`&&:`~##Flat[UpperTriangle&1!Table&_!`*]^^0
Try it online!
Still working on golfing this down a bit, but I think this is near optimal for the approach I've chosen.
Explanation
This is a composition of three functions:
Flat[UpperTriangle&1!Table&_!`*]^^0
SortBy#`&&:`~
Last
The first function does the bulk of the computation:
Flat[UpperTriangle&1!Table&_!`*]^^0
anonymous lambda; input: _ (e.g.: [2,3,4,5,6])
Table&_!`* shorthand for Table[`*, _]
this creates a multiplication table using the input
e.g.:
4 6 8 10 12
6 9 12 15 18
8 12 16 20 24
10 15 20 25 30
12 18 24 30 36
UpperTriangle&1! takes the strict upper triangle of this matrix
e.g.:
0 6 8 10 12
0 0 12 15 18
0 0 0 20 24
0 0 0 0 30
0 0 0 0 0
Flat[ ]^^0 flattens this list and removes all 0s
e.g.: [6, 8, 10, 12, 12, 15, 18, 20, 24, 30]
The second is a bit complicated but does something rather simple. First, it is useful to know that f&n
is a function which, when called with arguments ...x
, returns f[...x, n]
. f&:n
is similar, returning f[n, ...x]
. Now, let's decompose this:
( ~SortBy ) # (`& &: `~)
First, f#g
creates a fork. With input n
, it returns f[n, g[n]]
. However, in this case, f
is the function ~SortBy
. ~f
reverses the arguments of the function. This means that ~f#g
is equivalent to f[g[n], n]
, or here, SortBy[(`& &: `~)[n], n]
.
`& &: `~
follows the form f&:n
. But what are `&
and `~
? They are "operator quotes", and return a function equivalent to the quoted operator. So, in this case, `&
is the same thing as $ x & y
. With that in mind, this expression is equivalent to the following for binary operators:
f&:n <=> $ f[n, x]
<=> $ (`&)[`~, x]
<=> $ `~ & x
This yields the function `~&x
, where x
is the result from the first function. n ~ a
counts the occurrences of n
in a
. So, this returns a function which counts the occurrences of the argument in the computed array from function 1.
Going back to SortBy
, this each element in the array by the number of times it appears in it.
Finally, Last
takes the element which occurs the most. Ties are broken by the sorting algorithm.
Attache, 59 bytes
Last##~SortBy#`&&:`~##Flat[UpperTriangle&1!Table&_!`*]^^0
Try it online!
Still working on golfing this down a bit, but I think this is near optimal for the approach I've chosen.
Explanation
This is a composition of three functions:
Flat[UpperTriangle&1!Table&_!`*]^^0
SortBy#`&&:`~
Last
The first function does the bulk of the computation:
Flat[UpperTriangle&1!Table&_!`*]^^0
anonymous lambda; input: _ (e.g.: [2,3,4,5,6])
Table&_!`* shorthand for Table[`*, _]
this creates a multiplication table using the input
e.g.:
4 6 8 10 12
6 9 12 15 18
8 12 16 20 24
10 15 20 25 30
12 18 24 30 36
UpperTriangle&1! takes the strict upper triangle of this matrix
e.g.:
0 6 8 10 12
0 0 12 15 18
0 0 0 20 24
0 0 0 0 30
0 0 0 0 0
Flat[ ]^^0 flattens this list and removes all 0s
e.g.: [6, 8, 10, 12, 12, 15, 18, 20, 24, 30]
The second is a bit complicated but does something rather simple. First, it is useful to know that f&n
is a function which, when called with arguments ...x
, returns f[...x, n]
. f&:n
is similar, returning f[n, ...x]
. Now, let's decompose this:
( ~SortBy ) # (`& &: `~)
First, f#g
creates a fork. With input n
, it returns f[n, g[n]]
. However, in this case, f
is the function ~SortBy
. ~f
reverses the arguments of the function. This means that ~f#g
is equivalent to f[g[n], n]
, or here, SortBy[(`& &: `~)[n], n]
.
`& &: `~
follows the form f&:n
. But what are `&
and `~
? They are "operator quotes", and return a function equivalent to the quoted operator. So, in this case, `&
is the same thing as $ x & y
. With that in mind, this expression is equivalent to the following for binary operators:
f&:n <=> $ f[n, x]
<=> $ (`&)[`~, x]
<=> $ `~ & x
This yields the function `~&x
, where x
is the result from the first function. n ~ a
counts the occurrences of n
in a
. So, this returns a function which counts the occurrences of the argument in the computed array from function 1.
Going back to SortBy
, this each element in the array by the number of times it appears in it.
Finally, Last
takes the element which occurs the most. Ties are broken by the sorting algorithm.
answered Aug 6 at 18:24
Conor O'Brien
27.9k262156
27.9k262156
Is the UpperTriangle part required? Can you just flatten the table and sort?
â svavil
Aug 7 at 12:14
@svavil Yes, it is required;[5, 2, 9, 10, 3, 4, 4, 4, 7] -> 16
instead of20
without it.
â Conor O'Brien
Aug 7 at 18:41
add a comment |Â
Is the UpperTriangle part required? Can you just flatten the table and sort?
â svavil
Aug 7 at 12:14
@svavil Yes, it is required;[5, 2, 9, 10, 3, 4, 4, 4, 7] -> 16
instead of20
without it.
â Conor O'Brien
Aug 7 at 18:41
Is the UpperTriangle part required? Can you just flatten the table and sort?
â svavil
Aug 7 at 12:14
Is the UpperTriangle part required? Can you just flatten the table and sort?
â svavil
Aug 7 at 12:14
@svavil Yes, it is required;
[5, 2, 9, 10, 3, 4, 4, 4, 7] -> 16
instead of 20
without it.â Conor O'Brien
Aug 7 at 18:41
@svavil Yes, it is required;
[5, 2, 9, 10, 3, 4, 4, 4, 7] -> 16
instead of 20
without it.â Conor O'Brien
Aug 7 at 18:41
add a comment |Â
up vote
3
down vote
APL (Dyalog Unicode), 29 27 19 bytes
âÂÂ/â¢/â£èâ¸âÂÂâµâ³âÂÂâ¢âÂÂèâ¢ÃÂâÂÂ
Try it online!
Tacit fn.
Thanks to Adám for the tacit version and 2 bytes.
Thanks to ngn for 8 bytes!
How:
âÂÂ/â¢/â£èâ¸âÂÂâµâ³âÂÂâ¢âÂÂèâ¢ÃÂâÂÂ
â¢ÃÂâ â Multiply each element with the entire argument, then
â³âÂÂâ¢âÂÂè â Remove 1 from the first, two from the next etc. (removes repeated multiplications);
â The result is then fed into the function:
âÂÂâµ â Flatten the result;
â£è⸠â Key; creates a matrix in which each row corresponds to a unique product;
â¢/ â Get the rightmost column of the matrix;
âÂÂ/ â Get the highest value.
1
This is only 27.
â Adám
Aug 7 at 15:18
add a comment |Â
up vote
3
down vote
APL (Dyalog Unicode), 29 27 19 bytes
âÂÂ/â¢/â£èâ¸âÂÂâµâ³âÂÂâ¢âÂÂèâ¢ÃÂâÂÂ
Try it online!
Tacit fn.
Thanks to Adám for the tacit version and 2 bytes.
Thanks to ngn for 8 bytes!
How:
âÂÂ/â¢/â£èâ¸âÂÂâµâ³âÂÂâ¢âÂÂèâ¢ÃÂâÂÂ
â¢ÃÂâ â Multiply each element with the entire argument, then
â³âÂÂâ¢âÂÂè â Remove 1 from the first, two from the next etc. (removes repeated multiplications);
â The result is then fed into the function:
âÂÂâµ â Flatten the result;
â£è⸠â Key; creates a matrix in which each row corresponds to a unique product;
â¢/ â Get the rightmost column of the matrix;
âÂÂ/ â Get the highest value.
1
This is only 27.
â Adám
Aug 7 at 15:18
add a comment |Â
up vote
3
down vote
up vote
3
down vote
APL (Dyalog Unicode), 29 27 19 bytes
âÂÂ/â¢/â£èâ¸âÂÂâµâ³âÂÂâ¢âÂÂèâ¢ÃÂâÂÂ
Try it online!
Tacit fn.
Thanks to Adám for the tacit version and 2 bytes.
Thanks to ngn for 8 bytes!
How:
âÂÂ/â¢/â£èâ¸âÂÂâµâ³âÂÂâ¢âÂÂèâ¢ÃÂâÂÂ
â¢ÃÂâ â Multiply each element with the entire argument, then
â³âÂÂâ¢âÂÂè â Remove 1 from the first, two from the next etc. (removes repeated multiplications);
â The result is then fed into the function:
âÂÂâµ â Flatten the result;
â£è⸠â Key; creates a matrix in which each row corresponds to a unique product;
â¢/ â Get the rightmost column of the matrix;
âÂÂ/ â Get the highest value.
APL (Dyalog Unicode), 29 27 19 bytes
âÂÂ/â¢/â£èâ¸âÂÂâµâ³âÂÂâ¢âÂÂèâ¢ÃÂâÂÂ
Try it online!
Tacit fn.
Thanks to Adám for the tacit version and 2 bytes.
Thanks to ngn for 8 bytes!
How:
âÂÂ/â¢/â£èâ¸âÂÂâµâ³âÂÂâ¢âÂÂèâ¢ÃÂâÂÂ
â¢ÃÂâ â Multiply each element with the entire argument, then
â³âÂÂâ¢âÂÂè â Remove 1 from the first, two from the next etc. (removes repeated multiplications);
â The result is then fed into the function:
âÂÂâµ â Flatten the result;
â£è⸠â Key; creates a matrix in which each row corresponds to a unique product;
â¢/ â Get the rightmost column of the matrix;
âÂÂ/ â Get the highest value.
edited Aug 7 at 18:23
answered Aug 7 at 14:48
J. Sallé
1,298218
1,298218
1
This is only 27.
â Adám
Aug 7 at 15:18
add a comment |Â
1
This is only 27.
â Adám
Aug 7 at 15:18
1
1
This is only 27.
â Adám
Aug 7 at 15:18
This is only 27.
â Adám
Aug 7 at 15:18
add a comment |Â
up vote
3
down vote
CJam, 70 68 bytes
q',/S*~_,(:LLX-,0a+[X1++*](;_X=_Y=@*fYfX]~;]__@e=$;_,(=
Try it online!
Explanation
q',/S*~ Turn input string into a valid CJam array
_,( Find the length of the array and subtract 1
:L Assign the result to L
fX Outer for loop
LX-,0a+[X1++*](; Create an array with all the array indexes bigger than X
fY Inner for loop
_X=_Y=@* Create a multiple of array[X] and array[Y] (Guaranteed to be from a unique combination of factors)
~;] Casts away all stack items except for an array of the multiples
__@e=$; Sorts array by number of occurrences (largest number of occurences at the end)
_,(= Gets the last element of the array
You will need to scroll right to see the explanations as the code is quite long, as well as the explanations.
This was an absolute nightmare to write. CJam doesn't have a powerset function (unlike a ton of other golfing languages - great choice on my part), which means that I had to manually find the powerset. However, this gave me the opportunity to ignore any invalid number of factors, unlike other answers with a powerset function.
This should be golfable, considering I'm terrible at CJam.
Changes:
Helen cut off 2 bytes!
Old: q',/S*~_,1-:LLX-,0a+[X1++*](;_X=_Y=@*fYfX]~;]__@e=$;_,1-=
New: q',/S*~_,(:LLX-,0a+[X1++*](;_X=_Y=@*fYfX]~;]__@e=$;_,(=
By changing the 1-
s to simply (
we get the same effect but with a lower byte count.
add a comment |Â
up vote
3
down vote
CJam, 70 68 bytes
q',/S*~_,(:LLX-,0a+[X1++*](;_X=_Y=@*fYfX]~;]__@e=$;_,(=
Try it online!
Explanation
q',/S*~ Turn input string into a valid CJam array
_,( Find the length of the array and subtract 1
:L Assign the result to L
fX Outer for loop
LX-,0a+[X1++*](; Create an array with all the array indexes bigger than X
fY Inner for loop
_X=_Y=@* Create a multiple of array[X] and array[Y] (Guaranteed to be from a unique combination of factors)
~;] Casts away all stack items except for an array of the multiples
__@e=$; Sorts array by number of occurrences (largest number of occurences at the end)
_,(= Gets the last element of the array
You will need to scroll right to see the explanations as the code is quite long, as well as the explanations.
This was an absolute nightmare to write. CJam doesn't have a powerset function (unlike a ton of other golfing languages - great choice on my part), which means that I had to manually find the powerset. However, this gave me the opportunity to ignore any invalid number of factors, unlike other answers with a powerset function.
This should be golfable, considering I'm terrible at CJam.
Changes:
Helen cut off 2 bytes!
Old: q',/S*~_,1-:LLX-,0a+[X1++*](;_X=_Y=@*fYfX]~;]__@e=$;_,1-=
New: q',/S*~_,(:LLX-,0a+[X1++*](;_X=_Y=@*fYfX]~;]__@e=$;_,(=
By changing the 1-
s to simply (
we get the same effect but with a lower byte count.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
CJam, 70 68 bytes
q',/S*~_,(:LLX-,0a+[X1++*](;_X=_Y=@*fYfX]~;]__@e=$;_,(=
Try it online!
Explanation
q',/S*~ Turn input string into a valid CJam array
_,( Find the length of the array and subtract 1
:L Assign the result to L
fX Outer for loop
LX-,0a+[X1++*](; Create an array with all the array indexes bigger than X
fY Inner for loop
_X=_Y=@* Create a multiple of array[X] and array[Y] (Guaranteed to be from a unique combination of factors)
~;] Casts away all stack items except for an array of the multiples
__@e=$; Sorts array by number of occurrences (largest number of occurences at the end)
_,(= Gets the last element of the array
You will need to scroll right to see the explanations as the code is quite long, as well as the explanations.
This was an absolute nightmare to write. CJam doesn't have a powerset function (unlike a ton of other golfing languages - great choice on my part), which means that I had to manually find the powerset. However, this gave me the opportunity to ignore any invalid number of factors, unlike other answers with a powerset function.
This should be golfable, considering I'm terrible at CJam.
Changes:
Helen cut off 2 bytes!
Old: q',/S*~_,1-:LLX-,0a+[X1++*](;_X=_Y=@*fYfX]~;]__@e=$;_,1-=
New: q',/S*~_,(:LLX-,0a+[X1++*](;_X=_Y=@*fYfX]~;]__@e=$;_,(=
By changing the 1-
s to simply (
we get the same effect but with a lower byte count.
CJam, 70 68 bytes
q',/S*~_,(:LLX-,0a+[X1++*](;_X=_Y=@*fYfX]~;]__@e=$;_,(=
Try it online!
Explanation
q',/S*~ Turn input string into a valid CJam array
_,( Find the length of the array and subtract 1
:L Assign the result to L
fX Outer for loop
LX-,0a+[X1++*](; Create an array with all the array indexes bigger than X
fY Inner for loop
_X=_Y=@* Create a multiple of array[X] and array[Y] (Guaranteed to be from a unique combination of factors)
~;] Casts away all stack items except for an array of the multiples
__@e=$; Sorts array by number of occurrences (largest number of occurences at the end)
_,(= Gets the last element of the array
You will need to scroll right to see the explanations as the code is quite long, as well as the explanations.
This was an absolute nightmare to write. CJam doesn't have a powerset function (unlike a ton of other golfing languages - great choice on my part), which means that I had to manually find the powerset. However, this gave me the opportunity to ignore any invalid number of factors, unlike other answers with a powerset function.
This should be golfable, considering I'm terrible at CJam.
Changes:
Helen cut off 2 bytes!
Old: q',/S*~_,1-:LLX-,0a+[X1++*](;_X=_Y=@*fYfX]~;]__@e=$;_,1-=
New: q',/S*~_,(:LLX-,0a+[X1++*](;_X=_Y=@*fYfX]~;]__@e=$;_,(=
By changing the 1-
s to simply (
we get the same effect but with a lower byte count.
edited Aug 8 at 18:37
answered Aug 7 at 23:21
Helen
1055
1055
add a comment |Â
add a comment |Â
up vote
2
down vote
Java (JDK 10), 132 bytes
a->int l=a.length,i=l,j=0,r=99999,m=new int[r];for(;i-->0;)for(j=l;--j>i;)m[a[i]*a[j]]++;for(;r-->0;)i=m[r]<i?i:m[j=r];return j;
Try it online!
add a comment |Â
up vote
2
down vote
Java (JDK 10), 132 bytes
a->int l=a.length,i=l,j=0,r=99999,m=new int[r];for(;i-->0;)for(j=l;--j>i;)m[a[i]*a[j]]++;for(;r-->0;)i=m[r]<i?i:m[j=r];return j;
Try it online!
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Java (JDK 10), 132 bytes
a->int l=a.length,i=l,j=0,r=99999,m=new int[r];for(;i-->0;)for(j=l;--j>i;)m[a[i]*a[j]]++;for(;r-->0;)i=m[r]<i?i:m[j=r];return j;
Try it online!
Java (JDK 10), 132 bytes
a->int l=a.length,i=l,j=0,r=99999,m=new int[r];for(;i-->0;)for(j=l;--j>i;)m[a[i]*a[j]]++;for(;r-->0;)i=m[r]<i?i:m[j=r];return j;
Try it online!
answered Aug 7 at 8:35
Olivier Grégoire
7,37311739
7,37311739
add a comment |Â
add a comment |Â
up vote
2
down vote
Japt -h
, 20 bytes
Not the best. I dont know how to make this code shorter
ãUsYàî*X
c n óÃÂ¥ ñÃÂo
Try it online!
add a comment |Â
up vote
2
down vote
Japt -h
, 20 bytes
Not the best. I dont know how to make this code shorter
ãUsYàî*X
c n óÃÂ¥ ñÃÂo
Try it online!
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Japt -h
, 20 bytes
Not the best. I dont know how to make this code shorter
ãUsYàî*X
c n óÃÂ¥ ñÃÂo
Try it online!
Japt -h
, 20 bytes
Not the best. I dont know how to make this code shorter
ãUsYàî*X
c n óÃÂ¥ ñÃÂo
Try it online!
answered Aug 7 at 15:12
Luis felipe De jesus Munoz
2,4451039
2,4451039
add a comment |Â
add a comment |Â
up vote
2
down vote
Husk, 7 bytes
á¹ âº#màá¹Â2
Try it online!
Explanation
á¹ âº#màá¹Â2 -- example input [3,3,3]
á¹Â2 -- subsequences of length 2: [[3,3],[3,3],[3,3]]
mÃÂ -- map product: [9,9,9]
á¹ -- apply
# -- | count occurences in list
⺠-- to maxOn that list: [9]
add a comment |Â
up vote
2
down vote
Husk, 7 bytes
á¹ âº#màá¹Â2
Try it online!
Explanation
á¹ âº#màá¹Â2 -- example input [3,3,3]
á¹Â2 -- subsequences of length 2: [[3,3],[3,3],[3,3]]
mÃÂ -- map product: [9,9,9]
á¹ -- apply
# -- | count occurences in list
⺠-- to maxOn that list: [9]
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Husk, 7 bytes
á¹ âº#màá¹Â2
Try it online!
Explanation
á¹ âº#màá¹Â2 -- example input [3,3,3]
á¹Â2 -- subsequences of length 2: [[3,3],[3,3],[3,3]]
mÃÂ -- map product: [9,9,9]
á¹ -- apply
# -- | count occurences in list
⺠-- to maxOn that list: [9]
Husk, 7 bytes
á¹ âº#màá¹Â2
Try it online!
Explanation
á¹ âº#màá¹Â2 -- example input [3,3,3]
á¹Â2 -- subsequences of length 2: [[3,3],[3,3],[3,3]]
mÃÂ -- map product: [9,9,9]
á¹ -- apply
# -- | count occurences in list
⺠-- to maxOn that list: [9]
answered Aug 7 at 15:34
BWO
9,06111569
9,06111569
add a comment |Â
add a comment |Â
up vote
2
down vote
Haskell, 96 94 bytes
-2 bytes thanks to nimi (using sortOn(0<$)
instead of length
)!
import Data.List
f a|b<-zip[0..]a=last.last.sortOn(0<$).group$sort[x*y|(i,x)<-b,(j,y)<-b,i/=j]
Try it online!
1
sortOn(0<$)
.
â nimi
Aug 7 at 19:26
add a comment |Â
up vote
2
down vote
Haskell, 96 94 bytes
-2 bytes thanks to nimi (using sortOn(0<$)
instead of length
)!
import Data.List
f a|b<-zip[0..]a=last.last.sortOn(0<$).group$sort[x*y|(i,x)<-b,(j,y)<-b,i/=j]
Try it online!
1
sortOn(0<$)
.
â nimi
Aug 7 at 19:26
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Haskell, 96 94 bytes
-2 bytes thanks to nimi (using sortOn(0<$)
instead of length
)!
import Data.List
f a|b<-zip[0..]a=last.last.sortOn(0<$).group$sort[x*y|(i,x)<-b,(j,y)<-b,i/=j]
Try it online!
Haskell, 96 94 bytes
-2 bytes thanks to nimi (using sortOn(0<$)
instead of length
)!
import Data.List
f a|b<-zip[0..]a=last.last.sortOn(0<$).group$sort[x*y|(i,x)<-b,(j,y)<-b,i/=j]
Try it online!
edited Aug 7 at 19:29
answered Aug 7 at 15:50
BWO
9,06111569
9,06111569
1
sortOn(0<$)
.
â nimi
Aug 7 at 19:26
add a comment |Â
1
sortOn(0<$)
.
â nimi
Aug 7 at 19:26
1
1
sortOn(0<$)
.â nimi
Aug 7 at 19:26
sortOn(0<$)
.â nimi
Aug 7 at 19:26
add a comment |Â
up vote
2
down vote
MATLAB 39 bytes
a=input('');
b=triu(a'*a,1);
mode(b(b~=0))
Also check out answer by Jacob Watson
1
Having the second line beb=triu(a'*a,1);
saves 4 bytes.
â sundar
Aug 8 at 12:32
@sundar Oh snap, you are right :) I hadtriu
intially but drifted away somehow
â aaaaaa
Aug 8 at 14:47
Good solution, I didn't realise the upper triangle function was so short!
â Jacob Watson
Aug 10 at 8:19
add a comment |Â
up vote
2
down vote
MATLAB 39 bytes
a=input('');
b=triu(a'*a,1);
mode(b(b~=0))
Also check out answer by Jacob Watson
1
Having the second line beb=triu(a'*a,1);
saves 4 bytes.
â sundar
Aug 8 at 12:32
@sundar Oh snap, you are right :) I hadtriu
intially but drifted away somehow
â aaaaaa
Aug 8 at 14:47
Good solution, I didn't realise the upper triangle function was so short!
â Jacob Watson
Aug 10 at 8:19
add a comment |Â
up vote
2
down vote
up vote
2
down vote
MATLAB 39 bytes
a=input('');
b=triu(a'*a,1);
mode(b(b~=0))
Also check out answer by Jacob Watson
MATLAB 39 bytes
a=input('');
b=triu(a'*a,1);
mode(b(b~=0))
Also check out answer by Jacob Watson
edited Aug 8 at 14:48
answered Aug 8 at 0:40
aaaaaa
2816
2816
1
Having the second line beb=triu(a'*a,1);
saves 4 bytes.
â sundar
Aug 8 at 12:32
@sundar Oh snap, you are right :) I hadtriu
intially but drifted away somehow
â aaaaaa
Aug 8 at 14:47
Good solution, I didn't realise the upper triangle function was so short!
â Jacob Watson
Aug 10 at 8:19
add a comment |Â
1
Having the second line beb=triu(a'*a,1);
saves 4 bytes.
â sundar
Aug 8 at 12:32
@sundar Oh snap, you are right :) I hadtriu
intially but drifted away somehow
â aaaaaa
Aug 8 at 14:47
Good solution, I didn't realise the upper triangle function was so short!
â Jacob Watson
Aug 10 at 8:19
1
1
Having the second line be
b=triu(a'*a,1);
saves 4 bytes.â sundar
Aug 8 at 12:32
Having the second line be
b=triu(a'*a,1);
saves 4 bytes.â sundar
Aug 8 at 12:32
@sundar Oh snap, you are right :) I had
triu
intially but drifted away somehowâ aaaaaa
Aug 8 at 14:47
@sundar Oh snap, you are right :) I had
triu
intially but drifted away somehowâ aaaaaa
Aug 8 at 14:47
Good solution, I didn't realise the upper triangle function was so short!
â Jacob Watson
Aug 10 at 8:19
Good solution, I didn't realise the upper triangle function was so short!
â Jacob Watson
Aug 10 at 8:19
add a comment |Â
up vote
2
down vote
SQL Server, 93 bytes
SELECT TOP 1a.a*b.a
FROM @ a
JOIN @ b ON a.i<b.i
GROUP BY a.a*b.a
ORDER BY COUNT(a.a*b.a)DESC
Input is assumed to come from a table of the form
DECLARE @ TABLE (A int, i int identity);
Example table population:
INSERT INTO @ VALUES (9), (7), (10), (9), (7), (8), (5), (10), (1);
Explanation:
I assume a "list of integers" will have an index associated with them, which in my case is the column i
. The column a
contains the values of the list.
I create products of every pair, where the left pair comes in the list earlier than the right pair. I then group on the product, and sort by the most populous number.
I'm a little sad I didn't get to use any cte or partitioning clauses, but they were just too long. SELECT
is a very expensive keyword.
Alternative, 183 bytes
WITH c
AS(SELECT a,ROW_NUMBER()OVER(ORDER BY a)r
FROM @),d AS(SELECT a.a*b.a p,COUNT(a.a*b.a)m
FROM c a
JOIN c b ON a.r<b.r GROUP BY a.a*b.a)SELECT TOP 1p
FROM d
ORDER BY m DESC
If SQL doesn't get to have a separate index column, here is a solution where I create an index using the ROW_NUMBER
function. I personally don't care about the order, but an order is required and using the a
column is the shortest.
add a comment |Â
up vote
2
down vote
SQL Server, 93 bytes
SELECT TOP 1a.a*b.a
FROM @ a
JOIN @ b ON a.i<b.i
GROUP BY a.a*b.a
ORDER BY COUNT(a.a*b.a)DESC
Input is assumed to come from a table of the form
DECLARE @ TABLE (A int, i int identity);
Example table population:
INSERT INTO @ VALUES (9), (7), (10), (9), (7), (8), (5), (10), (1);
Explanation:
I assume a "list of integers" will have an index associated with them, which in my case is the column i
. The column a
contains the values of the list.
I create products of every pair, where the left pair comes in the list earlier than the right pair. I then group on the product, and sort by the most populous number.
I'm a little sad I didn't get to use any cte or partitioning clauses, but they were just too long. SELECT
is a very expensive keyword.
Alternative, 183 bytes
WITH c
AS(SELECT a,ROW_NUMBER()OVER(ORDER BY a)r
FROM @),d AS(SELECT a.a*b.a p,COUNT(a.a*b.a)m
FROM c a
JOIN c b ON a.r<b.r GROUP BY a.a*b.a)SELECT TOP 1p
FROM d
ORDER BY m DESC
If SQL doesn't get to have a separate index column, here is a solution where I create an index using the ROW_NUMBER
function. I personally don't care about the order, but an order is required and using the a
column is the shortest.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
SQL Server, 93 bytes
SELECT TOP 1a.a*b.a
FROM @ a
JOIN @ b ON a.i<b.i
GROUP BY a.a*b.a
ORDER BY COUNT(a.a*b.a)DESC
Input is assumed to come from a table of the form
DECLARE @ TABLE (A int, i int identity);
Example table population:
INSERT INTO @ VALUES (9), (7), (10), (9), (7), (8), (5), (10), (1);
Explanation:
I assume a "list of integers" will have an index associated with them, which in my case is the column i
. The column a
contains the values of the list.
I create products of every pair, where the left pair comes in the list earlier than the right pair. I then group on the product, and sort by the most populous number.
I'm a little sad I didn't get to use any cte or partitioning clauses, but they were just too long. SELECT
is a very expensive keyword.
Alternative, 183 bytes
WITH c
AS(SELECT a,ROW_NUMBER()OVER(ORDER BY a)r
FROM @),d AS(SELECT a.a*b.a p,COUNT(a.a*b.a)m
FROM c a
JOIN c b ON a.r<b.r GROUP BY a.a*b.a)SELECT TOP 1p
FROM d
ORDER BY m DESC
If SQL doesn't get to have a separate index column, here is a solution where I create an index using the ROW_NUMBER
function. I personally don't care about the order, but an order is required and using the a
column is the shortest.
SQL Server, 93 bytes
SELECT TOP 1a.a*b.a
FROM @ a
JOIN @ b ON a.i<b.i
GROUP BY a.a*b.a
ORDER BY COUNT(a.a*b.a)DESC
Input is assumed to come from a table of the form
DECLARE @ TABLE (A int, i int identity);
Example table population:
INSERT INTO @ VALUES (9), (7), (10), (9), (7), (8), (5), (10), (1);
Explanation:
I assume a "list of integers" will have an index associated with them, which in my case is the column i
. The column a
contains the values of the list.
I create products of every pair, where the left pair comes in the list earlier than the right pair. I then group on the product, and sort by the most populous number.
I'm a little sad I didn't get to use any cte or partitioning clauses, but they were just too long. SELECT
is a very expensive keyword.
Alternative, 183 bytes
WITH c
AS(SELECT a,ROW_NUMBER()OVER(ORDER BY a)r
FROM @),d AS(SELECT a.a*b.a p,COUNT(a.a*b.a)m
FROM c a
JOIN c b ON a.r<b.r GROUP BY a.a*b.a)SELECT TOP 1p
FROM d
ORDER BY m DESC
If SQL doesn't get to have a separate index column, here is a solution where I create an index using the ROW_NUMBER
function. I personally don't care about the order, but an order is required and using the a
column is the shortest.
answered Aug 8 at 17:52
Brian J
653613
653613
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f170047%2fmost-common-multiple%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sandbox (deleted)
â Jo King
Aug 6 at 10:39
4
Suggested test case: one where all elements are the same (i.e.
[3,3,3] -> 9
). With all your current test cases filtering out any pairs where elements are the same (even for test cases like[2,3,3]
containing the same values) will still hold the correct test-results, but will fail for this test case because none will remain after filtering.â Kevin Cruijssen
Aug 6 at 12:07
@Kevin Good suggestion, added
â Jo King
Aug 6 at 12:38