Faktoryzacja 2 czynników

14

Biorąc pod uwagę liczbę naturalną, nnapisz 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 nto 16777216otrzymujemy 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.

sergiol
źródło
2
Czy możesz dodać mniejszy przypadek testowy, na przykład 30?
caird coinheringaahing
1
@cairdcoinheringaahing Za pomocą factornumber.com można wygenerować więcej przypadków testowych.
Jonathan Frech,
1
Ostatnio widziałem ten konkurs „na język”. Jaki jest sens? Większość Q nie otrzymuje więcej niż 1 lub 2 W zależności od języka i nadal możesz wybrać tylko jedno A jako prawidłowe.
Fede s.
5
@fedes. Zwykle dzieje się tak, ponieważ nie ma sensu porównywać języków (tj. Java vs. Jelly).
całkowicie ludzki,
1
@ totalniehuman tak, wiem. Większość moich odpowiedzi jest w Factor, a nawet Smalltalk. Brak szans w stosunku do języków golfowych. Być może istnieje jakiś sposób na uszeregowanie języków według gadatliwości i bojlerów
fede s.

Odpowiedzi:

6

Java (OpenJDK 8) , 81 66 65 bajtów

  • -15 Bajtów dzięki Olivier Grégoire.
  • -1 bajt: ++j<=i/j-> j++<i/j.
i->{for(int j=1;j++<i/j;)if(i%j<1)System.out.println(j+" "+i/j);}

Wypróbuj online!


Stary (w celach informacyjnych)

Java (OpenJDK 8) , 126 bajtów

i->{java.util.stream.IntStream.range(2,i).filter(d->d<=i/d&&i%d==0).forEach(e->System.out.println(""+e+"x"+i/e));return null;}

Wypróbuj online!

First codegolf submit and first lambda usage. Future self, please forgive me for the code.

Bernat
źródło
1
Nice first entry! Welcome to PPCG! Here is it golfed down to 66 bytes by removing all the superfluous: I couldn't golf your algorithm though.
Olivier Grégoire
5

05AB1E, 8 bytes

Ñ‚ø2äн¦

Try it online!

Emigna
źródło
2
+1 from me we have nearly the same solutions. I thought of this 8-byter
Mr. Xcoder
@Mr.Xcoder: Ah yes, nice :) It's too bad that the map is required there.
Emigna
5

Python 2, 51 bytes

f=lambda n,k=2:n/k/k*[f]and[(k,n/k)][n%k:]+f(n,k+1)

Try it online!


51 bytes (thanks to Luis Mendo for a byte)

lambda n:[(n/k,k)for k in range(1,n)if(k*k<=n)>n%k]

Try it online!


51 bytes

lambda n:[(n/k,k)for k in range(1,n)if n/k/k>n%k*n]

Try it online!

xnor
źródło
I like the use of [f].
Jonathan Frech
1
You can save 1 byte in the second version with lambda n:[(n/k,k)for k in range(1,n)if(k*k<=n)>n%k]
Luis Mendo
MemoryError on all approaches for 1512518520
sergiol
4

Haskell, 38 bytes

f x=[(a,b)|a<-[2..x],b<-[2..a],a*b==x]

Try it online!

nimi
źródło
Time out for 1512518520
sergiol
3

Perl 6, 38 bytes

{map {$^a,$_/$a},grep $_%%*,2.. .sqrt}

Try it

Expanded:

{   # bare block lambda with implicit parameter 「$_」

  map
    { $^a, $_ / $a },  # map the number with the other factor

    grep
      $_ %% *,         # is the input divisible by *
      2 .. .sqrt       # from 2 to the square root of the input
}
Brad Gilbert b2gills
źródło
3

Brachylog, 8 bytes

{~×≜Ċo}ᵘ

Try it online!

Explanation

{~×≜Ċo}ᵘ
{     }ᵘ  List the unique outputs of this predicate.
 ~×       Pick a list of integers whose product is the input.
   ≜      Force concrete values for its elements.
    Ċ     Force its length to be 2.
     o    Sort it and output the result.

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...

Zgarb
źródło
1
The 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.
Fatalize
2

Japt, 9 bytes

â¬Å£[XZo]

Test it online! Returns an array of arrays, with some nulls at the end; -R flag added to show output more clearly.

ETHproductions
źródło
1
So I think the ` -R` should be considered for the byte count...
sergiol
3
@sergiol, no, in this case it's just for formatting the output for better readability.
Shaggy
Exactly the solution I had, except I filtered the nulls out at the end.
Shaggy
2

Jelly, 8 bytes

½ḊpP⁼¥Ðf

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?

½ḊpP⁼¥Ðf - Link: number, n     e.g. 144
½        - square root of n          12
 Ḋ       - dequeue*                 [2,3,4,5,6,7,8,9,10,11,12]
  p      - Cartesian product**      [[2,1],[2,2],...[2,144],[3,1],...,[3,144],...,[12,144]
      Ðf - filter keep if:
     ¥   -   last two links as a dyad (n is on the right):
   P     -     product
    ⁼    -     equals
         -                          [[2,72],[3,48],[4,36],[6,24],[8,18],[9,16],[12,12]]

* , 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 ½ is 4.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 is n hence the right argument becomes [1,2,3,...,n] prior to the actual Cartisian product taking place.

Jonathan Allan
źródło
2

Husk, 8 bytes

tüOSze↔Ḋ

Try it online!

Explanation

tüOSze↔Ḋ  Implicit input, say n=30.
       Ḋ  List of divisors: [1,2,3,5,6,10,15,30]
      ↔   Reverse: [30,15,10,6,5,3,2,1]
   Sze    Zip with original: [[1,30],[2,15],[3,10],[5,6],[6,5],[10,3],[15,2],[30,1]]
 üO       Deduplicate by sort: [[1,30],[2,15],[3,10],[5,6]]
t         Drop first pair: [[2,15],[3,10],[5,6]]
Zgarb
źródło
2

JavaScript (ES6), 55 bytes

n=>eval('for(k=1,a=[];k*++k<n;n%k||a.push([k,n/k]));a')

Demo

Try It Online!

Arnauld
źródło
Is it me or does this fail for 6?
Neil
@Neil "We can fix it." (Thanks for reporting!)
Arnauld
How can I supply a number to test?
sergiol
You can Try it online!
Arnauld
1

Python 2, 59 bytes

lambda N:{(n,N/n,n)[n>N/n:][:2]for n in range(2,N)if N%n<1}

Try it online!

Jonathan Frech
źródło
@sergiol Yes, a MemoryError, since Python tries to evaluate range(2,N) and store it as a list, yet the allocated memory does not suffice. One could try replace range with xrange (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.
Jonathan Frech
1

PHP, 70 bytes

As string (70 bytes):

$i++;while($i++<sqrt($a=$argv[1])){echo !($a%$i)?" {$i}x".($a/$i):'';}

As array dump (71 bytes):

$i++;while($i++<sqrt($a=$argv[1]))!($a%$i)?$b[$i]=$a/$i:'';print_r($b);

(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:

Array
(
    [2] => 8388608
    [4] => 4194304
    [8] => 2097152
    [16] => 1048576
RFSnake
źródło
"If you choose to return a list/array" To me it means you can print or return as you see fit.
fede s.
On second thought, returning should be valid for a function, and printing for a program. You seem to have a snippet/program, not a function, so I'd say in this case you should be printing.
fede s.
1

Jelly, 12 bytes

ÆDµżUḣLHĊ$$Ḋ

Try it online!

How it works

ÆDµżUḣLHĊ$$Ḋ - Main monadic link;
             - Argument: n (integer) e.g. 30
ÆD           - Divisors                   [1, 2, 3, 5, 6, 10, 15, 30]
    U        - Reverse                    [30, 15, 10, 6, 5, 3, 2, 1]
   ż         - Interleave                 [[1, 30], [2, 15], [3, 10], [5, 6], [6, 5], [10, 3], [15, 2], [30, 1]]
         $$  - Last 3 links as a monad
      L      -   Length                   8
       H     -   Halve                    4
        Ċ    -   Ceiling                  4
     ḣ       - Take first elements        [[1, 30], [2, 15], [3, 10], [5, 6]]
           Ḋ - Dequeue                    [[2, 15], [3, 10], [5, 6]]
caird coinheringaahing
źródło
1

Wolfram Language (Mathematica), 41 bytes

nRest@Union[Sort@{#,n/#}&/@Divisors@n]

Try it online!

is the Function operator, which introduces an unnamed function with named parameter n.

Martin Ender
źródło
1

Factor, 58

Well, there has to be some Factor in this question!

[ divisors dup reverse zip dup length 1 + 2 /i head rest ]

It's a quotation. call it with the number on the stack, leaves an assoc (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:

USING: math.prime.factors sequences assocs math ;

(If they count, I should look for a longer solution with shorter imports, which is kind of silly)

As a word:

: 2-factors ( x -- a ) divisors dup reverse zip dup length 1 + 2 /i head rest ;

50 2-factors .
 --> { { 2 25 } { 5 10 } }
fede s.
źródło
1

Ruby, 43 bytes

->n{(2..n**0.5).map{|x|[[x,n/x]][n%x]}-[p]}

Try it online!

How it works:

For every number up to sqrt(n), generate the pair [[x, n/x]], then take the n%xth element of this array. If n%x==0 this is [x, n/x], otherwise it's nil. when done, remove all nil from the list.

G B
źródło
1

Pari/GP, 49 34 38 bytes

n->[[d,n/d]|d<-divisors(n),d>1&d<=n/d]

Try it online!

Set builder notation for all pairs [d, n/d] where d runs through all divisors d of n subject to d > 1 and d <= n/d.

Huge improvement by alephalpha.

Jeppe Stig Nielsen
źródło
@alephalpha Good one. But had to change it a bit because it output also the factorization with 1.
Jeppe Stig Nielsen
0

Husk, 14 12 bytes

tumoOSe`/⁰Ḋ⁰

Try it online!

Explanation

tum(OSe`/⁰)Ḋ⁰  -- input ⁰, eg. 30
           Ḋ⁰  -- divisors [1..⁰]: [1,2,3,5,6,10,15,30]
  m(      )    -- map the following function (example on 10):
     Se        --   create list with 10 and ..
       `/⁰     --   .. flipped division by ⁰ (30/10): [10,3]
    O          --   sort: [3,10]
               -- [[1,30],[2,15],[3,10],[5,6],[5,6],[3,10],[2,15],[1,30]]
 u             -- remove duplicates: [[1,30],[2,15],[3,10],[5,6]]
t              -- tail: [[2,15],[3,10],[5,6]]
ბიმო
źródło
0

APL+WIN, 32 bytes

m,[.1]n÷m←(0=m|n)/m←1↓⍳⌊(n←⎕)*.5

Explanation:

(n←⎕) Prompts for screen input

m←(0=m|n)/m←1↓⍳⌊(n←⎕)*.5 Calculates the factors dropping the first

m,[.1]n÷ Identifies the pairs and concatenates into a list.
Graham
źródło
0

Add++, 18 15 bytes

L,F@pB]dBRBcE#S

Try it online!

How it works

L,   - Create a lambda function
     - Example argument:     30
  F  - Factors;     STACK = [1 2 3 5 6 10 15]
  @  - Reverse;     STACK = [15 10 6 5 3 2 1]
  p  - Pop;         STACK = [15 10 6 5 3 2]
  B] - Wrap;        STACK = [[15 10 6 5 3 2]]
  d  - Duplicate;   STACK = [[15 10 6 5 3 2] [15 10 6 5 3 2]]
  BR - Reverse;     STACK = [[15 10 6 5 3 2] [2 3 5 6 10 15]]
  Bc - Zip;         STACK = [[15 2] [10 3] [6 5] [5 6] [3 10] [2 15]]
  E# - Sort each;   STACK = [[2 15] [3 10] [5 6] [5 6] [3 10] [2 15]]
  S  - Deduplicate; STACK = [[[2 15] [3 10] [5 6]]]
caird coinheringaahing
źródło
0

Befunge-93, 56 bytes

&1vg00,+55./.:   <
+1< v`\g00/g00:p00
_ ^@_::00g%!00g\#v

Try It Online

Jo King
źródło
0

Julia 0.6, 41 bytes

~x=[(y,div(x,y))for y=2:x if x%y<1>y^2-x]

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).
  • Julia allows chaining the comparisons, saving one byte.
  • Looping all the way to x avoids Int(floor(√x)).
LukeS
źródło
0

APL NARS 99 chars

r←f w;i;h
r←⍬⋄i←1⋄→0×⍳0≠⍴⍴w⋄→0×⍳''≡0↑w⋄→0×⍳w≠⌊w⋄→0×⍳w≠+w
A:i+←1⋄→A×⍳∼0=i∣w⋄→0×⍳i>h←w÷i⋄r←r,⊂i h⋄→A

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")

  f 101    

  f 1 2 3

  f '1'

  f '123'

  f 33 1.23

  f 1.23

  ⎕←⊃f 16777216      
   2 8388608
   4 4194304
   8 2097152
  16 1048576
  32  524288
  64  262144
 128  131072
 256   65536
 512   32768
1024   16384
2048    8192
4096    4096
  f 123
3 41 
RosLuP
źródło
0

Pyt, 67 65 bytes

←ĐðĐ0↔/⅟ƖŽĐŁ₂20`ŕ3ȘĐ05Ș↔ŕ↔Đ4Ș⇹3Ș⦋ƥ⇹⁺Ɩ3ȘĐ05Ș↔ŕ↔Đ4Ș⇹3Ș⦋ƤĐ3Ș⁺ƖĐ3Ș<łĉ

I'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.

mudkip201
źródło
0

Perl 5, 50 bytes

sub{map[$_,$_[0]/$_],grep!($_[0]%$_),2..sqrt$_[0]}

Ungolfed:

sub {
    return map  { [$_, $_[0] / $_] }
           grep { !($_[0] % $_) }
           (2 .. sqrt($_[0]));
}

Try it online.

Denis Ibaev
źródło