挿入により素因数の数を最小限に抑える


12

2つの正の整数ABを指定すると、BApで挿入さときに、結果の整数の素因数(多重度をカウント)の数を最小化する位置pを返します。

たとえば、A = 1234およびB = 32の場合、挿入の可能性(pは0から始まる)および対応する素因数に関する情報です。

p | 結果| 素因数| Ω(N)/カウント

0 | 321234 | [2、3、37、1447] | 4
1 | 132234 | [2、3、22039] | 3
2 | 123234 | [2、3、19、23、47] | 5
3 | 123324 | [2、2、3、43、239] | 5
4 | 123432 | [2、2、2、3、37、139] | 6

pが1の場合、結果には最小数の素因数3があることがわかります。したがって、この特定のケースでは1を出力する必要があります。

スペック

  • 結果を最小化する複数の位置pがある場合、それらすべてを出力するか、いずれか1つを出力するかを選択できます。

  • pには0-indexingまたは1-indexingを選択できますが、この選択には一貫性が必要です。

  • AおよびBは、整数、文字列、または数字のリストとして使用できます。

  • デフォルトではこれらの抜け穴が禁止されていることに注意しながら、任意のプログラミング言語で競争し、標準的な方法で入力を取得し、出力を提供できます。これはコードゴルフなので、最短の提出(バイト単位)が勝ちです!

テストケース

A、B-> p(0から始まる)/ p(1から始まる)

1234、32-> 1/2
3456、3-> 4/5
378、1824-> 0/1
1824、378-> 4/5
67、267-> [1、2] / [2、3]のいずれかまたはすべて
435、1-> [1、2、3] / [2、3、4]のいずれかまたはすべて
378100、1878980901-> [5、6] / [6、7]のいずれかまたはすべて

便宜上、入力の各ペアを表すタプルのリストは次のとおりです。

[(1234, 32), (3456, 3), (378, 1824), (1824, 378), (67, 267), (435, 1), (378100, 1878980901)]

1
私は...これは05AB1Eに偏っている感覚を得る
caird coinheringaahing

1
挿入のインデックスの代わりに、素因数を最小化した結果の数値を出力できますか?たとえば、最初のテストケースでは132234なく1
ディルナン

2
@dylnan今回はノーと言うつもりです。
Xcoder氏18年

回答:


8

、16バイト

§◄öLpr§·++⁰↑↓oΘŀ

入力を文字列として期待します。オンライン試してください!

説明

§◄(öLpr§·++⁰↑↓)(Θŀ)  -- input A implicit, B as ⁰ (example "1234" and "32")
§ (           )(  )  -- apply A to the two functions and ..
  (ö          )      --   | "suppose we have an argument N" (eg. 2)
  (    §      )      --   | fork A and ..
  (         ↑ )      --     | take N: "12"
  (          ↓)      --     | drop N: "34"
  (     ·++⁰  )      --   | .. join the result by B: "123234"
  (   r       )      --   | read: 123234
  (  p        )      --   | prime factors: [2,3,19,23,47]
  ( L         )      --   | length: 5
  (öLpr§·++⁰↑↓)      --   : function taking N and returning number of factors
                            in the constructed number
               ( ŀ)  --   | range [1..length A]
               (Θ )  --   | prepend 0
               (Θŀ)  --   : [0,1,2,3,4]
 ◄                   -- .. using the generated function find the min over range

7

MATL、25バイト

sh"2GX@q:&)1GwhhUYfn]v&X<

入力は逆順の文字列です。出力は1ベースです。同点の場合、最下位の位置が出力されます。

オンラインでお試しください!または、すべてのテストケースを確認します

説明

s         % Implicitly input B as a string. Sum (of code points). Gives a number
h         % Implicitly input A as a string. Concatenate. Gives a string of length
          % N+1, where N is the length of A
"         % For each (that is, do N+1 times)
  2G      %   Push second input
  X@      %   Push 1-based iteration index
  q       %   Subtract 1
  :       %   Range from 1 to that. Gives [] in the first iteration, [1] in
          %   the second, ..., [1 2 ... N] in the last
  &)      %   Two-output indexing. Gives a substring with the selected elements,
          %   and then a substring with the remaining elements
  1G      %   Push first input
  whh     %   Swap and concatenate twice. This builds the string with B inserted
          %   in A at position given by the iteration index minus 1
  U       %   Convert to string
  Yf      %   Prime factors
  n       %   Number of elements
]         % End
v         % Concatenate stack vertically
&X<       % 1-based index of minimum. Implicitly display

6

Pyth、20 13 11バイト

.mlPsXbQzhl

オンラインで試す

説明

.mlPsXbQzhl
.m    b         Find the minimum value...
         hl     ... over the indices [0, ..., len(first input)]...
  lP            ... of the number of prime factors...
    sX Qz       ... of the second input inserted into the first.


3

Japt22 21バイト

私はそれを書いている間、これは長すぎると感じましたが、他のソリューションのいくつかを見ると、実際にはいくらか競争力があるようです。それでも、おそらく少し改善の余地cNq)があります- 特に、私は迷惑です。続く説明。

最初の入力を文字列として、2番目の入力を整数または文字列として受け取ります。結果は0から始まり、複数のソリューションがある場合は最初のインデックスを返します。

ÊÆiYVÃcNq)®°k Ê
b@e¨X

それを試してみてください


説明

      :Implicit input of string U and integer V.
Ê     :Get the length of U.
Æ     :Generate an array of the range [0,length) and map over each element returning ...
iYV   :  U with V inserted at index Y.
à    :End mapping
c     :Append ...
Nq    :  The array of inputs joined to a string.
®     :Map over the array.
°     :Postfix increment - casts the current element to an integer.
k     :Get the prime divisors.
Ê     :Get the length.
\n    :The newline allows the array above to be assigned to variable U.
b     :Get the first index in U that returns true ...
@     :  when passed through a function that ...
e     :    checks that every element in U...
¨     :    is greater than or equal to...
X     :    the current element.
      : Implicit output of resulting integer.

2

PowerShell、228バイト

param($a,$b)function f($a){for($i=2;$a-gt1){if(!($a%$i)){$i;$a/=$i}else{$i++}}}
$p=@{};,"$b$a"+(0..($x=$a.length-2)|%{-join($a[0..$_++]+$b+$a[$_..($x+1)])})+"$a$b"|%{$p[$i++]=(f $_).count};($p.GetEnumerator()|sort value)[0].Name

オンラインでお試しください!

(長いようです/ゴルフの提案を歓迎します。また、最後のテストケースのTIOでタイムアウトしますが、アルゴリズムは問題なくそのケースで動作するはずです。)

PowerShellには素因数分解が組み込まれていないため、これはPrime Factors Buddiesに関する私の答えからコードを借用しています。それが最初の行ですfunction宣言です。

入力を取得して、空のハッシュテーブルに$a,$b設定$pします。次に、文字列を取得$b$aし、カンマ演算子,でシングルトン配列に変換し、stuffで配列連結します。スタッフは、ループを介している$a挿入、$b全ての点において、最終的でアレイ連結$a$b

この時点で、の$bすべてのポイントにの配列が挿入されてい$aます。次に、forループを介してその配列を送信します|%{...}。各繰り返しは、我々は位置で私たちのハッシュテーブルに挿入するのか、多くの素因数その特定の要素$i++.countf$_があります。

最後に、sにsort基づいてハッシュテーブルを作成しvalue0その1番目を取得して、そのName(つまり、$iインデックスの)を選択します。それはパイプラインに残り、出力は暗黙的です。



2

05AB1E27 21バイト

ηõ¸ì¹.sRõ¸«)øεIýÒg}Wk

オンラインでお試しください!

最小の0インデックス付きpを返します。

-6バイトの@Enigmaに感謝!

説明

ηõ¸ì                  # Push the prefixes of A with a leading empty string -- [, 1, 12, 123, 1234]
    ¹.sRõ¸«)          # Push the suffixes of A with a tailing empty space. -- [1234, 123, 12, 1, ]
            ø         # Zip the prefixes and suffixes
             ε    }   # Map each pair with...
              IýÒg    # Push B, join prefix - B - suffix, map with number of primes
                   Wk # Push the index of the minimum p

1
同じ方法を使用して、これをに書き換えて6バイト節約できますηõ¸ì¹.sRõ¸«)øεIýÒg}Wk
エミグナ



0

JavaScript(ES6)、120バイト

入力を2つの文字列として受け取ります。0インデックスの位置を返します。

f=(a,b,i=0,m=a)=>a[i>>1]?f(a,b,i+1,eval('for(n=a.slice(0,i)+b+a.slice(i),x=k=2;k<n;n%k?k++:n/=x++&&k);x')<m?(r=i,x):m):r

テストケース


0

J、60バイト

4 :'(i.<./)#@q:>".@(,(":y)&,)&.>/"1({.;}.)&(":x)"0 i.>:#":x'

明示的なダイアド。Bを右側、Aを左側にします。

0インデックス付き出力。

ボックスを使用しないことで改善できる場合があります。

説明:

  4 :'(i.<./)#@q:>".@(,(":x)&,)&.>/"1({.;}.)&(":y)"0 i.>:#":y'  | Whole program
  4 :'                                                       '  | Define an explicit dyad
                                                     i.>:#":y   | Integers from 0 to length of y
                                                  "0            | To each element
                                     ({.;}.)&(":y)              | Split y at the given index (result is boxed)
                     (,(":x)&,)&.>/"1                           | Put x inbetween, as a string
                  ".@                                           | Evaluate
                 >                                              | Unbox, makes list
             #@q:                                               | Number of prime factors of each
      (i.>./)                                                   | Index of the minimum

0

Python 3、128バイト

0インデックス付き。パラメータとして文字列を受け取ります。ジョナサン・フレッシュのおかげで-6バイト。

from sympy.ntheory import*
def f(n,m):a=[sum(factorint(int(n[:i]+m+n[i:])).values())for i in range(len(n)+1)];return a.index(min(a))

:\n a-> :a
ジョナサンフレッチ

0

Python、122バイト

f=lambda n,i=2:n>1and(n%i and f(n,i+1)or 1+f(n/i,i))
g=lambda A,B:min(range(len(A)+1),key=lambda p:f(int(A[:p]+B+A[p:])))

実際には、これはデフォルトの最大再帰深度をすぐに超えます。

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.