因数分解!…ひどく


15

好奇心の強い子供は、数値または式を次の形式に分解できるプログラムを使用します。 p1^e1 * p2^e2 * ... * pn^en。に等しい指数1は省略されます360 = 2^3 * 3^2 * 5

子供はこの出力を新しい入力としてプログラムに入力しますが、^符号を理解していないため、対応する素数と指数を連結する1つ以上をスキップすることがあります。例えば(360 =) 2^3 * 3^2 * 5 => 2^3 * 32 * 5 (= 1280)

これらの間違いのために、彼女は別の因数分解を取得する可能性があり、それを再度入力できます(0個以上をスキップします^)。彼女は、因数分解がそれ以上変化しなくなるまでプロセスを繰り返します(多分それ以上ない^か、出力を正しくコピーしたかもしれません)。

整数nn>1)が与えられたすべての可能な数を昇順で出力するプログラムまたは関数を作成する必要がありますn。たとえば、入力16の最終的な因数分解は次のとおりです。(16 =) 2^4, (24 =) 2^3 * 3, (23*3 =) 3 * 23

入力の詳細:

  • 入力は、より大きな単一の整数です 1
  • より大きい出力数を生成する入力は与えられません 2^31-1
  • 1000出力以上の数を生成する入力は与えられません

出力の詳細:

  • あなたの言語にとって便利な形式の整数のリスト

例:

入力=>出力

11    => 11
16    => 16 24 69
360   => 140 360 770 1035 1219 1280 2875 3680
605   => 560 605 840 2415
2048  => 211 2048
58564 => 230 456 1311 2508 9975 12768 13794 20748 58564 114114 322102

これはコードゴルフなので、最短のプログラムが勝ちます。


すでにFactorize Itがありませんか?
オプティマイザー

5
@Optimizerこれはまったく異なります。
-randomra

1
360の最後の数は3 6 80である必要があります:2 ^ 3 * 3 ^ 2 * 5 => 23 * 32 * 5 = 3680
blutorange

@blutorangeありがとう、編集。
-randomra

回答:


5

CJam-66

ria{_{:XmF{1=1>},La\{a1$\f++}/La-{XI{~#}%:*/I{si}%:**}fI}%|}1e3*$p

http://cjam.aditsu.net/で試してください

説明:

ria                       read token, convert to integer and wrap in array
{…}1e3*                   repeat 1000 times
    _                     duplicate array
    {…}%                  transform each array item (number) using the block
        :X                store the number in X
        mF                factorize with exponents
        {1=1>},           keep only the factors with exponent > 1
        La\{a1$\f++}/     get all the subsets (*)
        La-               remove the empty subset
        {…}fI             for I = each subset of prime factors with exponent > 1
            XI{~#}%:*/    divide X by all the factors in I
            I{si}%:**     multiply with the primes from I
                          concatenated with their exponents
    |                     add the new numbers to the array, removing duplicates
$                         sort
p                         print the final array

(*)マーティンに感謝


cjam神からのcjamコード
ケイン

^1つの手順で任意の数のを削除できます。だから、58564 = 2^2 * 11^4それを生成することができるはず2508 = 22 * 114です。
-randomra

@randomraこの種のものの例を追加する必要があります
-aditsu

@randomraは今より良いはずです
-aditsu

すごい!例を追加しました。スキップしてすみません。
randomra

4

ルビー、219

これを開始するには:

s=->(x){A=[];def k(x)A<<x
y=Prime.prime_division x;n=0..y.size-1
n.each{|i|n.to_a.combination(i+1).each{|c|c.each{|z|v=y.dup
v[z][1]>1?v[z]=[v[z].join.to_i,1]:next
k v.inject(1){|s,b|s*b[0]**b[1]}}}}end;k x;A.uniq.sort}

数値の配列を返す関数を作成します。

https://ideone.com/iOMGny

次のように使用します。

#usage

#load from the standard library
require"prime"

#read from stdin and print to stdout
p s.call $<.read.to_i

このすべてを携帯電話で書くのはとても楽しい...


3

Perl、193バイト

sub R{my($k,$v,@z)=@_;map{$k**$v*$_,$v>1?($k.$v)*$_:()}@z?R(@z):1}
@q=(0+<>);
while($x=pop@q){
my%f;@r=sort{$a<=>$b}@r,$x;
for(2..$x){$x/=$_,$f{$_}++while$x%$_<1}
$_~~@r||push@q,$_ for R%f
}
print"@r"

読みやすくするために改行が追加されました。

forループは、次の数$x%f)を素数とべきのハッシュ()に分解します。再帰関数(R)は、このハッシュを使用して、符号を削除することで達成できるすべての数値を生成します^。これらの番号はキュー(@q)に追加され、プロセスは外側のwhileループによって繰り返されます。キューの各番号は、@r印刷用に一意のソートされた配列()に保持されます。


3

Pyth、46 45 44

Su{smmu*/N^T/PdTv+`T`/PdTkdyft/PdT{PdGU^T3]Q

ここで試してみてください。

複数の^バグを修正しました。例えば:

Input:  58564
Output: [230, 456, 1311, 2508, 9975, 12768, 13794, 20748, 58564, 114114, 322102]

Note that this code relies on a couple of bugfixes to the official compiler that were pushed after the question was asked. However, it does not use any new language features.


What do you get for 58564?
aditsu

[230, 456, 1311, 58564, 322102], which is wrong.
isaacg

@aditsu Fixed the issue.
isaacg

As Pyth isn't rigorously documented (based on my findings) it is difficult to distinguish between bug fixes and new features so I decided not to choose this entry as the winning answer.
randomra

@randomra I understand your decision not to accept this answer. However, I'd just like to mention what the bugfix was: Using a reduce (u) inside another reduce was impossible. I changed a 2 to a 3 in the appropriate location so that reduce would take 3 inputs instead of 2. That's all.
isaacg
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.