このタスクでは、数値の素因数を計算するプログラムを作成する必要があります。入力は自然数1 <n <2 ^ 32です。出力は、次の形式の数値の素因数のリストです。指数は1の場合、省略する必要があります。素数のみを出力します。(入力が131784であると仮定):
131784 = 2 ^ 3 * 3 * 17 ^ 2 * 19
同じ量の空白を使用する必要はありません。空白は適切な場所に挿入できます。プログラムは、入力があれば10分以内に完了するはずです。最短のキャラクター数のプログラムが勝ちます。
このタスクでは、数値の素因数を計算するプログラムを作成する必要があります。入力は自然数1 <n <2 ^ 32です。出力は、次の形式の数値の素因数のリストです。指数は1の場合、省略する必要があります。素数のみを出力します。(入力が131784であると仮定):
131784 = 2 ^ 3 * 3 * 17 ^ 2 * 19
同じ量の空白を使用する必要はありません。空白は適切な場所に挿入できます。プログラムは、入力があれば10分以内に完了するはずです。最短のキャラクター数のプログラムが勝ちます。
回答:
N=input()
print N,"=",factor(N)
テストケース:
83891573479027823458394579234582347590825792034579235923475902312344444
出力:
83891573479027823458394579234582347590825792034579235923475902312344444 = 2^2 * 3^2 * 89395597 * 98966790508447596609239 * 263396636003096040031295425789508274613
perl -pe '$_=`factor $_`;s%( \d+)\K\1+%-1-length($&)/length$1%ge;y, -,*^,;s;\D+;=;'
標準入力から入力番号を取得します。提供されている場合、複数の入力の係数を計算します。
との差としてカウントされperl -eます。\K正規表現のメタ文字には5.10が必要です。
factor。
pオプションを数えるべきではありません か?
split/\D/,~factor $_~;$_="@_";書くことができます$_=~factor $_~;s/\D/ /g;か?(もちろん~バック
$_=`factor $_`;s/\D/ /g;ですか?二重バックティックケースが役立ちます。
最高のPythonコードの直接命令型変換:
let(%)s d=if!d>1then Printf.printf"%s%d"s!d
let f n=let x,d,e,s=ref n,ref 1,ref 0,ref"="in""%x;while!d<65536do
incr d;e:=0;while!x mod!d=0do x:=!x/ !d;incr e
done;if!e>0then(!s%d;"^"%e;s:="*")done;!s%x
例えば、
# f 4294967292;;
4294967292=2^2*3^2*7*11*31*151*331- : unit = ()
(最後の最終行の出力を省略していることに注意してください。)楽しみのために、純粋に機能的なバージョンである213文字で、演算子のリベラルな使用により完全に難読化されています。
let(%)s d=if d>1then Printf.printf"%s%d"s d
let f x=let s=ref"="in""%x;let rec(@)x d=if d=65536then!s%x else
let rec(^)x e=if x/d*d<x then x,e else x/d^e+1in
let x,e=x^0in if e>0then(!s%d;"^"%e;s:="*");x@d+1in x@2
M=N=input()
s=''
f=1
while f<4**8:
f+=1;e=0
while N%f<1:e+=1;N/=f
if e:s+='*%d'%f+'^%d'%e*(e>1)
print M,'=',(s+'*%d'%N*(N>1))[1:]
' * %d'...そして、さらに2つの事を:65536 == 4**8; 7行目:if e:s+='*%d'%f+'^%d'%e*(e>1)
この解決策は、randomrarplcの解決策からトリックを取りますが、いくつかの独自のアイデアも思い付きます。
":,'=',(":@{.,'^','*',~":@#)/.~@q:}:@rplc'^1*';'*'"_
非暗黙表記では、この関数は次のようになります
f =: 3 : 0
(": y) , '=' , }: (g/.~ q: y) rplc '^1*' ; '*'
)
どこgで定義されています
g =: 3 : 0
": {. y) , '^' , (": # y) , '*'
)
q: yはの素因数のベクトルですy。たとえば、q: 60yields 2 2 3 5。x u/. y適用uにy キー付きによってx、すなわち、uの要素のベクトルに適用さyれるのエントリはx同じです。これは説明するのが少し複雑ですが、特別な場合y u/. yまたはでu/.~ y、uの異なる要素の各ベクトルに適用されますy。ここで、各要素はに現れる回数だけ繰り返されyます。例えば、</.~ 1 2 1 2 3 1 2 2 3利回り
┌─────┬───────┬───┐
│1 1 1│2 2 2 2│3 3│
└─────┴───────┴───┘
# yあるタリーのy、内の項目の数です、y。
": y y文字列としてフォーマットします。x , y を追加 xしyます。{. yはhead y、つまり最初のアイテムです。(": {. y), '^' , (": # y) , '*'ベクトルをk ^ n *の形式の文字列にフォーマットします。暗黙の表記法でのこのフレーズはであり、これを上記の副詞に渡します。:@{.,'^','*',~":@#/.x rplc yライブラリ関数は文字を置き換えます。 yには形式がa ; bあり、文字列ainのすべてのインスタンスxはに置き換えられbます。xここで使用される操作が行われる前に解かれます(つまり、ランク1になるように再形成されます)。このコードは、置き換え^1*で*義務付けられた出力フォーマットに準拠するよう。}: yはの省略ですy。つまり、最後の項目以外はすべてです。これは、末尾を削除するために使用され*ます。__ q:か?オンラインでお試しください!
use ntheory":all";say join" * ",map{(join"^",@$_)=~s/\^1$//r}factor_exp(shift)
Perl 5.14のs /// r機能を使用して^ 1sを削除します。ループで実行する81文字:
perl -Mntheory=:all -nE 'chomp;say join" * ",map{(join"^",@$_)=~s/\^1$//r}factor_exp($_);'
$f[$n=$c=$argv[1]]++;echo"$n=";while($c){$c=0;foreach($f as$k=>$n)for($r=~~($k/2);$r>1;$r--){if($k%$r==0){unset($f[$k]);$f[$r]++;$f[$k/$r]++;$c=1;break;}}}foreach($f as$k=>$n)if(--$n)$f[$k]="$k^".++$n;else$f[$k]=$k;echo implode("*",$f);
131784の出力:2 ^ 3 * 3 * 17 ^ 2 * 19
テスト中に数秒以内にすべての数字を完了します。
4294967296=2^32
Time: 0.000168
入力は指定されなかったため、コマンドライン引数を使用して呼び出すことにしました。
php factorize.php 4294967296
def f(i:Int,c:Int=2):List[Int]=if(i==c)List(i)else
if(i%c==0)c::f(i/c,c)else f(i,c+1)
val r=f(readInt)
class A(val v:Int,val c:Int,val l:List[(Int,Int)])
def g(a:A,i:Int)=if(a.v==i)new A(a.v,a.c+1,a.l)else new A(i,1,(a.v,a.c)::a.l)
val a=(new A(r.head,1,Nil:List[(Int,Int)])/:(r.tail:+0))((a,i)=>g(a,i))
a.l.map(p=>if(p._2==1)p._1 else p._1+"^"+p._2).mkString("", "*", "")
なし:
def factorize (i: Int, c: Int = 2) : List [Int] = {
if (i == c) List (i) else
if (i % c == 0) c :: f (i/c, c) else
f (i, c+1)
}
val r = factorize (readInt)
class A (val value: Int, val count: Int, val list: List [(Int, Int)])
def g (a: A, i: Int) =
if (a.value == i)
new A (a.value, a.count + 1, a.list) else
new A (i, 1, (a.value, a.count) :: a.list)
val a = (new A (r.head, 1, Nil: List[(Int,Int)]) /: (r.tail :+ 0)) ((a, i) => g (a, i))
a.l.map (p => if (p._2 == 1) p._1 else
p._1 + "^" + p._2).mkString ("", "*", "")
f=.3 :0
(":y),'=',' '-.~('^1 ';'')rplc~}:,,&' *'"1(,'^'&,)&":/"{|:__ q:y
)
f 131784
131784=2^3*3*17^2*19
変数に入力がある64文字x:
x=.131784
(":x),'=',' '-.~('^1 ';'')rplc~}:,,&' *'"1(,'^'&,)&":/"{|:__ q:x
131784=2^3*3*17^2*19
3 : 0定義を使用することもできます。
3 : 0バージョンにエスケープされていない文字列を入れることができると思っていましたが、どういうわけか機能しませんでした。後で暗黙のうちに試すかもしれません。これは私が試した3:0です:pastebin.com/rmTVAk4j。
y想定通りに引数に名前を付けましたか?
''なく使用しa:ます。たぶんそれが違いますか?
n->{var r=n+"=";for(int i=1,f;i++<n;r+=f<1?"":(f<2?i:i+"^"+f)+(n>1?"*":""))for(f=0;n%i<1;n/=i)f++;return r;}
class M{public static void main(String[]a){long n=new Long(a[0]),i=1,f;String r=n+"=";for(;i++<n;r+=f<1?"":(f<2?i:i+"^"+f)+(n>1?"*":""))for(f=0;n%i<1;n/=i)f++;System.out.print(r);}}
@ceilingcatのおかげで-1バイト。
説明:
n->{ // Method with integer parameter and String return-type
var r=n+"="; // Result-String, starting at the input with an appended "="
for(int i=1,f;i++<n;
// Loop in the range [2, n]
r+= // After every iteration: append the following to the result-String:
f<1? // If the factor `f` is 0:
"" // Append nothing
: // Else:
(f<2? // If the factor `f` is 1:
i // Append the current prime `i`
: // Else:
i+"^"+f) // Append the current prime `i` with it's factor `f`
+(n>1? // And if we're not done yet:
"*" // Also append a "*"
: // Else:
"")) // Append nothing more
for(f=0; // Reset the factor `f` to 0
n%i<1; // Loop as long as `n` is divisible by `i`
n/=i) // Divide `n` by `i`
f++; // Increase the factor `f` by 1
return r;} // Return the result-String
param($x)(2..$x|%{for(;!($x%$_)){$_
$x/=$_}}|group|%{$_.Name+"^"+$_.Count-replace'\^1$'})-join'*'
テストスクリプトの説明:
$f = {
param($x) # let $x stores a input number > 0
(2..$x|%{ # loop from 2 to initial input number
for(;!($x%$_)){ # loop while remainder is 0
$_ # push a current value to a pipe
$x/=$_ # let $x is $x/$_ (new $x uses in for condition only)
}
}|group|%{ # group all values
$_.Name+"^"+$_.Count-replace'\^1$' # format and remove last ^1
})-join'*' # make string with *
}
&$f 2
&$f 126
&$f 129
&$f 86240
#&$f 7775460
出力:
2
2*3^2*7
3*43
2^5*5*7^2*11
³”=³ÆFḟ€1j€”^j”*
私の最初のゼリーの1つが答えたので、間違いなくゴルフができます(特に³”=³)。
説明:
³ # Push the first argument
”= # Push string "="
³ÆF # Get the prime factor-exponent pairs of the first argument
ḟ€1 # Remove all 1s from each pair
j€”^ # Join each pair by "^"
j”* # Join the pair of strings by "*"
# (implicitly join the entire 'stack' together)
# (which is output implicitly as result)
(non-competing)ます。:)
ÐfsÓ0Køε1K'^ý}'*ý'=ý
@Emignaのおかげで-2バイト。
説明:
Ð # Triplicate the (implicit) input-integer
f # Pop and push all prime factors (without counting duplicates)
s # Swap to take the input again
Ó # Get all prime exponents
0K # Remove all 0s from the exponents list
ø # Zip it with the prime factors, creating pairs
ε # Map each pair to:
1K # Remove all 1s from the pair
'^ý '# And then join by "^"
}'*ý '# After the map: join the string/integers by "*"
'=ý '# And join the stack by "=" (with the input we triplicated at the start)
# (after which the result is output implicitly)
1Kループ内で`≠iyの代わりに動作するはずです。
{(⍕⍵),'=',3↓∊{m←' * ',⍕↑⍵⋄1=w←2⊃⍵:m⋄m,'^',⍕w}¨v,¨+/¨{k=⍵}¨v←∪k←π⍵}
テストとコメント:
f←{(⍕⍵),'=',3↓∊{m←' * ',⍕↑⍵⋄1=w←2⊃⍵:m⋄m,'^',⍕w}¨v,¨+/¨{k=⍵}¨v←∪k←π⍵}
f 131784
131784=2^3 * 3 * 17^2 * 19
f 2
2=2
f (2*32)
4294967296=2^32
{(⍕⍵),'=',3↓∊{m←' * ',⍕↑⍵⋄1=w←2⊃⍵:m⋄m,'^',⍕w}¨v,¨+/¨{k=⍵}¨v←∪k←π⍵}
k←π⍵ find the factors with repetition of ⍵ and assign that array to k example for 12 k is 2 2 3
v←∪ gets from k unique elements and put them in array v
+/¨{k=⍵}¨ for each element of v count how many time it appear in k (it is array exponents)
v,¨ make array of couples from element of v (factors unique) and the array above (exponents unique)
∊{m←' * ',⍕↑⍵⋄1=w←2⊃⍵:m⋄m,'^',⍕w}¨ pretty print the array of couples factor exponent as array chars
3↓ but not the first 3 chars
(⍕⍵),'=' but print first the argument and '=' in char format
誰かがこれらのプリミティブで多くの時間を持っている場合、それらを非常によく知っているので、私にとってはコードがコメントよりも明確である可能性があります...
n=prompt()
s=n+'='
c=0
for(i=2;;){if(n%i<1){c++
n/=i}else{if(c)s+=i+'^'+c+'*'
c=0
if(++i>n)break}}
alert(s)
120
n=prompt()
o={2:0}
for(i=2,c=n;i<=c;)!(c%i)?++o[i]?c/=i:0:o[++i]=0
s=n+'='
for(i in o)s+=o[i]?i+'^'+o[i]+'*':''
alert(s)
*出力に末尾があり、指数が1であっても出力します。
*はで乗算することを想定してい1ます。それがそれほど大きな問題である場合、私はそれを修正します。
1印刷すべきではないことを示しています。そして、いいえ、トレーリング *もそれに反対です。自由に出力形式を選択できる場合は、シェルを作成factor(1)するのが最も簡単です。回答は、すべてが同じ問題を解決する場合にのみ合理的に比較できます。
<?=$a=$argn,"=";for($i=2;1<$a;)$a%$i?$i++:$a/=$i+!++$r[$i];foreach($r as$k=>$v)echo$t?"*":!++$t,$v<2?$k:"$k^$v";
<?=$n=$argn;for($i=2;$n>1;$k&&$p=print($p?"*":"=")."$i^$k",$i++)for($k=0;$n%$i<1;$n/=$i)$k++;
PHP 5.5(またはそれ以降)で89バイトを実行できましたが、それは2年以上前の課題に後れを取っています。
<?=$n=$argn;for($i=2;$n>1;$k&&$p=print"=*"[$p]."$i^$k",$i++)for($k=0;$n%$i<1;$n/=$i)$k++;
パイプとして実行する-nFか、オンラインで試してください。