Biorąc pod uwagę liczbę naturalną, n
napisz program lub funkcję, aby uzyskać listę wszystkich możliwych mnożników dwóch czynników, które można wykorzystać do osiągnięcia n
. Aby lepiej zrozumieć to, co udawał można przejść do http://factornumber.com/?page=16777216 aby zobaczyć, kiedy n
to 16777216
otrzymujemy następującą listę:
2 × 8388608
4 × 4194304
8 × 2097152
16 × 1048576
32 × 524288
64 × 262144
128 × 131072
256 × 65536
512 × 32768
1024 × 16384
2048 × 8192
4096 × 4096
Nie musisz drukować takich rzeczy jak tutaj. Wymagane jest, aby każdy wpis (para czynników) był dobrze odróżniony od siebie, a wewnątrz każdej pary pierwszy czynnik był również dobrze odróżniony od drugiego. Jeśli zdecydujesz się zwrócić listę / tablicę, element wewnętrzny może być listą / tablicą z dwoma elementami lub strukturą twojego języka, która obsługuje parę rzeczy takich jak C ++ std::pair
.
Nie drukuj mnożenia przez 1 wpis, ani nie powtarzaj wpisów z pierwszym czynnikiem zamienionym na drugi, ponieważ są one dość bezużyteczne.
Brak zwycięzcy; będzie to golf oparty na języku.
źródło
30
?Odpowiedzi:
Java (OpenJDK 8) ,
816665 bajtów++j<=i/j
->j++<i/j
.Wypróbuj online!
Stary (w celach informacyjnych)
Java (OpenJDK 8) , 126 bajtów
Wypróbuj online!
First codegolf submit and first lambda usage. Future self, please forgive me for the code.
źródło
05AB1E, 8 bytes
Try it online!
źródło
C (gcc),
585453 bytesTry it online!
źródło
Python 2, 51 bytes
Try it online!
51 bytes (thanks to Luis Mendo for a byte)
Try it online!
51 bytes
Try it online!
źródło
[f]
.lambda n:[(n/k,k)for k in range(1,n)if(k*k<=n)>n%k]
Haskell, 38 bytes
Try it online!
źródło
APL (Dyalog), 28 bytes
Try it online!
źródło
Perl 6, 38 bytes
Try it
Expanded:
źródło
Brachylog, 8 bytes
Try it online!
Explanation
The
~×
part does not include 1s in its output, so for input N it gives [N] instead of [1,N], which is later culled byĊ
. I'm not entirely sure why≜
is needed...źródło
≜
is needed because otherwise there are no choice points forᵘ
: a length-2 list whose product is the input is the only answer if you don't actually ask for the values of the list.Japt, 9 bytes
Test it online! Returns an array of arrays, with some nulls at the end;
-R
flag added to show output more clearly.źródło
null
s out at the end.Jelly, 8 bytes
A monadic link taking a number and returning a list of lists (pairs) of numbers.
Try it online! (times out on TIO for the
16777216
example since it would create a list of 68.7 billion pairs and filter down to those with the correct product!)How?
*
Ḋ
, dequeue, implicitly makes a range of a numeric input prior to acting, and the range function implicitly floors its input, so with, say,n=24
the result of½
is4.898...
; the range becomes[1,2,3,4]
; and the dequeued result is[2,3,4]
** Similarly to above,
p
, Cartesian product, makes ranges for numeric input - here the right argument isn
hence the right argument becomes[1,2,3,...,n]
prior to the actual Cartisian product taking place.źródło
Husk, 8 bytes
Try it online!
Explanation
źródło
JavaScript (ES6), 55 bytes
Demo
Show code snippet
Try It Online!
źródło
6
?Python 2, 59 bytes
Try it online!
źródło
range(2,N)
and store it as a list, yet the allocated memory does not suffice. One could try replacerange
withxrange
(Python 2's range generator), though this exceeds TIO's one minute of maximum runtime. On a machine with enough memory and time, this program should terminate and return the correct answer.Jelly, 9 bytes
Try it online!
źródło
Octave, 42 bytes
Try it online!
źródło
PHP, 70 bytes
As string (70 bytes):
As array dump (71 bytes):
(im not sure if i can use return $b; instead of print_r since it no longer outputs the array, otherwise i can save 2 bytes here. )
The array gives the results like:
źródło
Jelly, 12 bytes
Try it online!
How it works
źródło
Wolfram Language (Mathematica), 41 bytes
Try it online!
is theFunction
operator, which introduces an unnamed function with named parametern
.źródło
Factor, 58
Well, there has to be some Factor in this question!
It's a quotation.
call
it with the number on the stack, leaves anassoc
(an array of pairs) on the stack.I'm never sure if all the imports count or not, as they're part of the language. This one uses:
(If they count, I should look for a longer solution with shorter imports, which is kind of silly)
As a word:
źródło
Ruby, 43 bytes
Try it online!
How it works:
For every number up to sqrt(n), generate the pair
[[x, n/x]]
, then take then%x
th element of this array. Ifn%x==0
this is[x, n/x]
, otherwise it'snil
. when done, remove allnil
from the list.źródło
Pari/GP,
493438 bytesTry it online!
Set builder notation for all pairs
[d, n/d]
whered
runs through all divisorsd
ofn
subject tod > 1
andd <= n/d
.Huge improvement by alephalpha.
źródło
n->[[d,n/d]|d<-divisors(n),d<=n/d]
1
.Husk,
1412 bytesTry it online!
Explanation
źródło
APL+WIN, 32 bytes
Explanation:
źródło
Add++,
1815 bytesTry it online!
How it works
źródło
Mathematica, 53 bytes
Try it online!
źródło
Befunge-93, 56 bytes
Try It Online
źródło
Julia 0.6, 41 bytes
Try it online!
Redefines the inbuild unary operator
~
and uses an array comprehension to build the output.div(x,y)
is neccessary for integer division.x/y
saves 5 bytes but the output is~4=(2,2.0)
.Int(floor(√x))
.źródło
APL NARS 99 chars
9+46+41+3=99 Test: (where not print nothing, it return something it return ⍬ the list null one has to consider as "no solution")
źródło
Pyt,
6765 bytesI'm pretty sure this can be golfed.
Basically, the algorithm generates a list of all of the divisors of the input (let's call it n), makes the same list, but flipped, interleaves the two (e.g., if n=24, then, at this point, it has [1,24,2,12,3,8,4,6,6,4,8,3,12,2,24,1]), and prints out the elements from index 2 until half the array length, printing each number on a new line, and with an extra new line in between every pair.
Most of the work is done in actually managing the stack.
Saved 2 bytes by using increment function.
źródło
Perl 5, 50 bytes
Ungolfed:
Try it online.
źródło