Berechnen Sie die MU-Nummern

19

Die ersten beiden MU-Nummern sind 2 und 3. Jede andere MU-Nummer ist die kleinste noch nicht aufgetauchte Zahl, die auf genau eine Weise als Produkt zweier früherer eindeutiger MU-Nummern ausgedrückt werden kann.

Hier sind die ersten 10

2, 3, 6, 12, 18, 24, 48, 54, 96, 162

Aufgabe

Bei einer positiven Zahl wird die n- te ME-Zahl berechnet und ausgegeben .

Da dies ein Wettbewerb ist, sollten Sie versuchen, Ihren Quellcode so klein wie möglich zu halten.

OEIS A007335

Weizen-Assistent
quelle
1
0-Indizierung oder 1-Indizierung?
HyperNeutrino
1
@HyperNeutrino Entweder ist in Ordnung.
Weizen-Assistent
2
Irgendeine Idee, warum diese MU-Nummern genannt werden? (Wilde Vermutung: Multiplikation einzigartig?)

Antworten:

5

Pyth, 22 21 Bytes

@u+Gfq2l@GcLTGheGQhB2

Probieren Sie es online aus. Testsuite.

0-indiziert.

Erläuterung

@u+Gfq2l@GcLTGheGQhB2Q    Implicitly append Q and read+eval input to it.
                  hB2     Take the list [2, 2 + 1].
 u               Q        Put the list in G and apply this Q times:
               eG           Get last number in G.
              h             Add one.
    f                       Starting from that, find the first T such that:
          cLTG                Divide T by each of the numbers in G.
        @G                    Find the quotients that are also in G.
       l                      Get the number of such quotients.
     q2                       Check that it equals 2.
  +G                        Append that T to G.
@                    Q    Get the Q'th number in G.
PurkkaKoodari
quelle
Das @Vorzeichen in der letzten Zeile ist falsch ausgerichtet. Ich kann keine vorgeschlagene Änderung vornehmen, da es sich um eine Änderung mit zwei Zeichen handelt.
user2357112 unterstützt Monica
@ user2357112 Behoben.
PurkkaKoodari
4

Haskell, 80 77 Bytes

l#(a:b)|[x]<-[a|i<-l,j<-l,i<j,i*j==a]=a:(a:l)#b|1<2=l#b
((2:3:[2,3]#[4..])!!)

Probieren Sie es online!

Wie es funktioniert

2:3:             -- start the list with 2 and 3 and append a call to # with
    [2,3]        -- the list so far and
         #[4..]  -- list of candidate elements

l # (a:b)        -- l -> list so far, a -> next candidate element, b -> rest c.el.
  | [x]<-[...]   -- if the list [...] is a singleton list
    =a:(a:l#b) -- the result is a followed by a recursive call with l extended
                    by a and b
  | 1<2=l#b      -- if it's not a singleton list, drop a and retry with b

                 -- the [...] list is
 [ i<-l,j<-l,    -- loop i through l and j through l and whenever   
       i<j,      -- i<j and
       i*j==a]   -- i*j==a
  a|             -- add a to the list              
nimi
quelle
3

Jelly , 22 Bytes

ŒcP€ḟ⁸ṢŒgLÞḢḢṭ
2,3Ç¡ị@

Ein monadischer Link, 1-indiziert.

Probieren Sie es online!

Wie?

ŒcP€ḟ⁸ṢŒgLÞḢḢṭ - Link 1, add the next number: list, a  e.g. [2,3,6,12,18,24]
Œc             - unordered pairs                            [[2,3],[2,6],[2,12],[2,18],[2,24],[3,6],[3,12],[3,18],[3,24],[6,12],[6,18],[6,24],[12,18],[12,24],[18,24]]
  P€           - product of €ach                            [6,12,24,36,48,18,36,54,72,72,108,144,216,288,432]
     ⁸         - chain's left argument, a                   [2,3,6,12,18,24]
    ḟ          - filter discard                             [36,48,36,54,72,72,108,144,216,288,432]
      Ṣ        - sort                                       [36,36,48,54,72,72,108,144,216,288,432]
       Œg      - group runs of equal elements               [[36,36],[48],[54],[72,72],[108],[144],[216],[288],[432]]
          Þ    - sort by:
         L     -   length                                   [[48],[54],[108],[144],[216],[288],[432],[36,36],[72,72]]
           Ḣ   - head                                       [48]
            Ḣ  - head                                       48
             ṭ - tack to a                                  [2,3,6,12,18,24,48]

2,3Ç¡ị@ - Link: number, i                              e.g. 7
2,3     - literal [2,3]                                     [2,3]
    ¡   - repeat i times:
   Ç    -   call last link (1) as a monad                   [2,3,6,12,18,24,48,54,96]
     ị@ - index into with swapped @rguments (with i)        48
Jonathan Allan
quelle
3

R , 127 118 111 108 105 100 98 90 Bytes

8 Bytes dank Giuseppe.

r=3:2;for(i in 1:scan())r=c(min((g=(r%o%r)[i:-1<i])[colSums(g%o%g==g*g)+g%in%r<3]),r);r[3]

Probieren Sie es online!

Undichte Nonne
quelle
Es hat ewig gedauert, bis mir klar wurde, dass das <eine niedrigere Priorität hat, als +dies. Ich konnte nicht herausfinden, was zum Teufel +g%in%r<3war, und während ich das tat, haben Sie die beiden Teile, die ich vorschlagen wollte, golfen ... +1
Giuseppe
@ Giuseppe Ich habe gerade angefangen, R zu lernen ... schön, einen anständigen R-Golfer zu treffen.
Undichte Nonne
Ich wollte dir dasselbe sagen .............
Giuseppe
Ah, eine weitere Sache, Sie können n=scan()anstelle einer Funktionsdefinition verwenden, um von stdin zu lesen; das
Giuseppe
Eingabe fehlgeschlagen:0
Rift
2

CJam (32 Bytes)

4,{_2m*{~>},::*1$-$e`$0=|}qi*-2=

Online-Demo mit 0-Indexierung.

Ich bin mir nicht sicher, ob es über eine triviale Übersetzung der Spezifikation hinaus noch viel zu tun gibt, mit einer Ausnahme: Wenn ich mit einer Liste von [0 1 2 3](anstelle von [2, 3]) beginne, spare ich ein Byte sofort bei der Initialisierung und zwei weitere, indem ich in der Lage bin, dies zu tun 0=|(indem ich nur das hinzufüge) neues Element, weil seine Häufigkeit 1in der Liste ist und bereits vorhanden ist), aber füge keine falschen Elemente ein, weil für jedes xin der Liste 0*xund 1*xbereits in der Liste.

Peter Taylor
quelle
2

Python 2 , 127 118 Bytes

n=input()
l=[2,3]
exec't=sorted(x*y for i,x in enumerate(l)for y in l[i+1:]);l+=min(t,key=(l+t).count),;'*n
print l[n]

Probieren Sie es online!

Stange
quelle
1

Mathematica, 154 Bytes

einfache änderung des codes unter oeis link

(s={2,3};Do[n=Select[Split@Sort@Flatten@Table[s[[j]]s[[k]],{j,Length@s},{k,j+1,Length@s}],#[[1]]>s[[-1]]&&Length@#==1&][[1,1]];AppendTo[s,n],{#}];s[[#]])&
J42161217
quelle
1

PHP , 130 Bytes

0-indiziert

for($r=[2,3];!$r[$argn];$r[]=$l=min($m)/2){$m=[];foreach($r as$x)foreach($r as$y)($p=$x*$y)<=$l|$y==$x?:$m[$p]+=$p;}echo$r[$argn];

Probieren Sie es online!

Erweitert

for($r=[2,3];!$r[$argn]; #set the first to items and loop till search item exists
$r[]=$l=min($m)/2){ # add the half of the minimum of found values to the result array
  $m=[]; # start with empty array
  foreach($r as$x) # loop through result array
    foreach($r as$y) # loop through result array
      ($p=$x*$y)<=$l|$y==$x? # if product is greater as last value and we do multiple two distinct values
        :$m[$p]+=$p; # add 2 times or more the product to array so we drop 36 cause it will be 144  
}
echo$r[$argn]; # Output 

PHP , 159 Bytes

0-indiziert

for($r=[2,3];!$r[$argn];$r[]=$l=min(array_diff_key($m,$d))){$d=$m=[];foreach($r as$x)foreach($r as$y)$x<$y?${dm[$m[$p=$x*$y]<1&$p>$l]}[$p]=$p:0;}echo$r[$argn];

Probieren Sie es online!

PHP , 161 Bytes

0-indiziert

for($r=[2,3];!$r[$argn];$r[]=$l=min(array_diff($m,$d))){$d=$m=[];foreach($r as$x)foreach($r as$y)$x<$y?${dm[!in_array($p=$x*$y,$m)&$p>$l]}[]=$p:0;}echo$r[$argn];

Probieren Sie es online!

Jörg Hülsermann
quelle
1

Mathematica, 140 Bytes

(t=1;s={2,3};While[t<#,s=AppendTo[s,Sort[Select[First/@Select[Tally[Times@@@Permutations[s,{2}]],#[[2]]==2&],#>Last@s&]][[1]]];t++];s[[#]])&
J42161217
quelle
1

MATL , 25 Bytes

3:i:"t&*9B#u2=)yX-X<h]2_)

Probieren Sie es online!

Erläuterung

3:     % Push [1 2 3]. Initial array of MU numbers, to be extended with more numbers
i:     % Input n. Push [1 2 ... n]
"      % Do this n times
  t    %   Duplicate array of MU numbers so far
  &*   %   Matrix of pair-wise products
  9B   %   Push 9 in binary, that is, [1 0 0 1]
  #    %   Specify that next function will produce its first and fourth ouputs
  u    %   Unique: pushes unique entries (first output) and their counts (fourth)
  2=   %   True for counts that equal 2
  )    %   Keep only unique entries with count 2
  y    %   Duplicate (from below) array of MU numbers so far
  X-   %   Set difference
  X<   %   Minimum. This is the new MU number
  h    %   Concatenate vertically horizontally to extend the array
]      % End
2_     % Push 2 negated, that is, -2
)      % Get entry at position -2, that is, third-last. Implicitly display
Luis Mendo
quelle
1

Perl 6 , 96 Bytes

{(2,3,{first *∉@_,@_.combinations(2).classify({[*]
$_}).grep(*.value==1)».key.sort}...*)[$_]}

Probieren Sie es online!

  • 2, 3, { ... } ... *ist eine unendliche Folge, bei der jedes Element, das mit dem dritten beginnt, vom durch geschweifte Klammern getrennten Codeblock berechnet wird. Da der Codeblock seine Argumente über das Slurpy- @_Array erhält, erhält er die gesamte aktuelle Sequenz in diesem Array.
  • @_.combinations(2)ist eine Folge aller 2-Element-Kombinationen von @_.
  • .classify({ [*] $_ }) klassifiziert jedes 2-Tupel nach seinem Produkt und erzeugt einen Hash, in dem die Produkte die Schlüssel und die Werte die Liste der 2-Tupel sind, die dieses Produkt enthalten.
  • .grep(*.value == 1) wählt die Schlüssel-Wert-Paare aus dem Hash aus, bei denen der Wert (dh die Liste der Paare mit diesem Schlüssel als Produkt) eine Größe von 1 hat.
  • ».keyWählt nur die Schlüssel eines jeden Paares aus. Dies ist die Liste der Produkte, die sich aus nur einer Kombination von Faktoren der aktuellen Sequenz ergeben.
  • .sort sortiert die Produkte numerisch.
  • first * ∉ @_, ... findet das erste dieser Produkte, das noch nicht in der Sequenz vorkommt.
Sean
quelle
1

JavaScript (ES6), 119 118 117 Byte

Eine rekursive Funktion, die einen 0-basierten Index annimmt.

f=(n,a=[2,m=3])=>a[n]||a.map(c=>a.map(d=>c<d&(d*=c)>m?b[d]=b[d]/0||d:0),b=[])|f(n,a.push(m=b.sort((a,b)=>a-b)[0])&&a)

Wie?

Bei jeder Iteration von f () verwenden wir den letzten Term m der Sequenz und ein anfänglich leeres Array b , um den nächsten Term zu identifizieren. Für jedes Produkt d> m zweier früherer eindeutiger MU-Nummern führen wir Folgendes aus:

b[d] = b[d] / 0 || d

und behalten Sie dann den Mindestwert von b .

Der obige Ausdruck wird wie folgt bewertet:

b[d]               | b[d] / 0  | b[d] / 0 || d
-------------------+-----------+--------------
undefined          | NaN       | d
already equal to d | +Infinity | +Infinity
+Infinity          | +Infinity | +Infinity

Dies garantiert, dass Produkte, die auf mehr als eine Weise ausgedrückt werden können, niemals ausgewählt werden.

Formatiert und kommentiert

f = (n, a = [2, m = 3]) =>           // given: n = input, a[] = MU array, m = last term
  a[n] ||                            // if a[n] is defined, return it
  a.map(c =>                         // else for each value c in a[]:
    a.map(d =>                       //   and for each value d in a[]:
      c < d &                        //     if c is less than d and
      (d *= c) > m ?                 //     d = d * c is greater than m:
        b[d] = b[d] / 0 || d         //       b[d] = either d or +Infinity (see 'How?')
      :                              //     else:
        0                            //       do nothing
    ),                               //   end of inner map()
    b = []                           //   initialization of b[]
  ) |                                // end of outer map()
  f(                                 // do a recursive call:
    n,                               //   - with n
    a.push(                          //   - push in a[]:
      m = b.sort((a, b) => a - b)[0] //     m = minimum value of b[]
    ) && a                           //     and use a[] as the 2nd parameter
  )                                  // end of recursive call

Demo

Arnauld
quelle
0

Haskell , 117 115 113 Bytes

n x=[a*b|[a,b]<-mapM id[1:x,x]]
d x=minimum[a|a<-n x,2==sum[1|b<-n x,b==a]]:x
l x|x<3=x+1:[2]|1>0=d$l$x-1
(!!0).l

Probieren Sie es online!

Weizen-Assistent
quelle
Die erste Zeile kann als nützliche Redewendung für das kartesische Operatorprodukt geschrieben werden:n x=(*)<$>x<*>1:x
xnor
0

Python 3 2 , 167 139 136 133 123 121 120 118 Bytes

a=[2,3];exec'p=[x*y for x in a for y in a if x-y];a+=min(q for q in p if p.count(q)+(q in a)<3),;'*input();print a[-2]

Probieren Sie es online!


Danke an @ Mr.Xcoder und @LeakyNun für die Verbesserungen!

Chase Vogeli
quelle
159 Bytes , indem unnötige Leerzeichen und Klammern entfernt werden.
Mr. Xcoder
@ Mr.Xcoder Danke für die Verbesserungen. Ich bin mir nicht sicher, ob der Wechsel p.count(q)==1zu p.count(q)>0gültig ist, da dies der Code ist, der den "in genau einer Richtung" -Zustand der Herausforderung sicherstellt.
Chase Vogeli
p.count(q)-~(q in a)<=3entsprichtp.count(q)+(q in a)<3
Leaky Nun
@LeakyNun danke!
Chase Vogeli