2つの数値Nとxを指定して、桁の積がNであるx桁の数値の数を求めます
limits: N(<10^6) and x(<12)
Sample Input:
8 3
Sample Output:
10
2つの数値Nとxを指定して、桁の積がNであるx桁の数値の数を求めます
limits: N(<10^6) and x(<12)
Sample Input:
8 3
Sample Output:
10
回答:
~10\?.10/\,>{10base{*}*1$=},,\;
入力:(スペースで区切られた)数値N
とx
コマンドライン引数として期待されます。
Code took longer than 5 seconds to run, so it was aborted.
し、パラメータを逆にして得ました。:)
x
4を超えると、非常に非効率になります。たとえば、パラメーター3 5
を指定して自分のマシンで実行すると、30秒以上後に結果が得られます。だから3 8
私はそれが数時間かもしれないと思う...
{h.&t~lℕẹ≜×}ᶜ
{h.&t~lℕẹ≜×}ᶜ
{ }ᶜ Count the number of
≜ values at this point
&t formed via taking the last element of the input,
ℕ generating an integer
~l of that length,
ẹ and splitting it into digits
× such that the product of those digits
h. is the first element of {the input}
ここで使用される巧妙なゴルフトリックの1つは、ほとんどすべてのメタ述語とは異なりᶜ
、.
(メタ述語の出力を作成するために通常使用される)の実際の値をまったく気にしません。そのため、.
他の変数と同様に使用できます(バイトは暗黙的にの前に現れるため、バイトを節約します}
)。ここには暗黙のラベル付けはありません。そのため≜
、ᶜ
何かをカウントするためにを使用して明示的なラベル付けを追加する必要がありました。
def f(N:Int,x:Int,s:Int=1):Int=if(s==N&&x==0)1 else
if(s>N||x==0)0 else
((1 to 9).map(i=>f(N,x-1,s*i))).sum
非ゴルフ、デバッグフレンドリーバージョン:
def findProduct (N: Int, sofar: Int, togo:Int, collected:String=""):Int={
if (sofar == N && togo == 0) {
println (collected)
1
} else
if (sofar > N || togo == 0) 0 else
(1 to 9).map (x=> findProduct (N, sofar*x, togo-1, collected + " " + x)).sum
}
デバッグ出力を伴う呼び出し:
scala> findProduct (3, 1, 8)
1 1 1 1 1 1 1 3
1 1 1 1 1 1 3 1
1 1 1 1 1 3 1 1
1 1 1 1 3 1 1 1
1 1 1 3 1 1 1 1
1 1 3 1 1 1 1 1
1 3 1 1 1 1 1 1
3 1 1 1 1 1 1 1
res175: Int = 8
scala> findProduct (8, 1, 3)
1 1 8
1 2 4
1 4 2
1 8 1
2 1 4
2 2 2
2 4 1
4 1 2
4 2 1
8 1 1
res176: Int = 10
int Z(int n,int x){var i=(int)Math.Pow(10,x-1);return Enumerable.Range(i,i*9).Count(j=>(j+"").Aggregate(1,(a,c)=>a*(c-48))==n);}
このC#メソッドは、x
桁の積がである桁数を返しますn
。これは、その名前空間を必要とSystem
し、System.Linq
現在のコンテキストにインポートされます。
オンライン版:http : //ideone.com/0krup
,’⁵*r/ḊDP€ċƓ
コマンドライン引数としてxを取り、標準入力ではNを受け取ります。
,’⁵*r/ḊDP€ċƓ
, On {the input} and
’ {the input} minus 1
⁵* take 10 to the power of each of those numbers
r/ then form a range between them
Ḋ without its first element;
D treat each element as a list of digits,
P€ take the product of each of those digit lists,
ċ then count the number of times
Ɠ the value from standard input appears
難しいのは、x桁の数値のリストを生成することです。そのような最小の数は10 x −1であり、最大の数は10 x −1です。ここでの範囲は、最初にペア(x、x -1)を生成し、次にそれらの両方の10乗を行い、次にそれらの間の範囲を生成することによって生成されます。範囲には、デフォルトで両方のエンドポイントが含まれます。念のNが 0であることを起こる、我々は(それが「後方」の範囲ですので、最初に来る)レンジ使用の上端を削除する必要がありますḊ
。