最小の倍数が9回実行され、その後にオプションの0回の実行が続く


22

正の整数が与えられた場合、9の実行とそれに続くオプションの0の実行である最小の正の整数倍数を見つけます。つまり、regexに一致する最小の正の整数倍数を見つけます/^9+0*$/

たとえば、与えられた正の整数が2の場合、90は2の正の整数倍であり、正規表現と一致する最小の整数であるため、90を返し/^9+0*$/ます。

テストケース:

n  f(n)
1  9
2  90
3  9
4  900
5  90
6  90
7  999999
8  9000
9  9
10 90
11 99
12 900
13 999999
14 9999990
15 90
16 90000

これはです。バイト単位の最短回答が優先されます。標準の抜け穴が適用されます。


3
明確さの証明?
破壊可能なレモン

2
@DestructibleLemon 結果に9を掛けることができるため、この証明で十分です
。– xnor

1
ソリューションが0の前に来るには9が必要であることを確認するために、より多くのテストケースが良いと思います。
-xnor

2
@LeakyNunはそうではないかもしれませんが、9900099は許可されており、規則に従って許可されるべきではありません。
DrQuarius

2
@koita_pisw_souルールは、プログラムが任意の精度とメモリと時間を与えられた整数に対して「理論的に」動作することです。
リーキー修道女

回答:


6

ゼリー13 11バイト

ṚḌ‘DS=ḍ@ð1#

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

使い方

ṚḌ‘DS=ḍ@ð1#  Main link. Argument: n

        ð    Start a dyadic chain with arguments n and n.
         1#  Execute the chain to the left with left argument k = n, n+1, n+2, ...
             and right argument n until 1 match has been found. Return the match.
Ṛ                Get the decimal digits of k, reversed.
 Ḍ               Convert from base 10 to integer.
                 This essentially removes trailing zeroes. As a side effect, it
                 reverses the digits, which doesn't matter to us.
  ‘              Increment the resulting integer. If and only if it consisted
                 entirely of 9's, the result is a power of 10.
   DS            Compute the sum of the digits. The sum is 1 if and only if the
                 integer is a power of 10. Note that the sum cannot be 0.
      ḍ@         Test k for divisibility by n.
     =           Compare the results.

4
ಠ_ಠどのようにあなたがどちらもそれをやったの9か、0あなたのコードで
パベル

説明を追加しました。
デニス



5

JavaScript(ES6)、47 43 42バイト

@Arnauldのおかげで-4バイト@Lukeのおかげで
-1バイト

n=>eval('for(i=0;!/^9+0*$/.test(i);)i+=n')

テスト

let f=
n=>eval('for(i=0;!/^9+0*$/.test(i);)i+=n')

for(let i=1;i<=16;i++)console.log(`f(${i}) = `+f(i))

再帰的ソリューション(7、13、および14で失敗)、38バイト

n=>g=(i=0)=>/^9+0*$/.test(i+=n)?i:g(i)

と呼ばれf(5)()ます。以下のためのChromeとFirefoxで最大コールスタックのサイズに達したn=7n=13n=14


3
1バイト短縮:n=>eval('for(i=0;!/^9+0*$/.test(i);)i+=n')
ルーク


4

Java 8、61 57バイト

n->{int r=0;for(;!(""+r).matches("9+0*");r+=n);return r;}

-4バイト(および高速実行)@JollyJokerのおかげ

説明:

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

n->{                              // Method with integer as parameter and return-type
  int r=0;                        //  Result-integer
  for(;!(""+r).matches("9+0*");   //  Loop as long as `r` doesn't match the regex
    r+=n                          //   And increase `r` by the input every iteration
  );                              //  End of loop
  return r;                       //  Return the result-integer
}                                 // End of method

最適化が必要です!^^
オリビエグレゴワール

1
nをインクリメントすると、r%nチェックが回避されますn->{int r=0;for(;!(""+(r+=n)).matches("9+0*"););return r;}
JollyJoker

for(;!(""+r).matches("9+0*");r+=n)
JollyJoker

整数と数学を試してみましたが、これに勝てません!おめでとう:)
オリビエグレゴワール


3

Brachylog、16バイト

;I×≜.ẹḅhᵐc~a₀90∧

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

これはかなり遅いです

説明

;I×≜.              Output = Input × I
    .ẹḅ            Deconcatenate into runs of consecutive equal digits
       hᵐ          Take the head of each run
         c         Concatenate into a number
          ~a₀90∧   That number is a prefix of 90 (i.e. it's 9 or 90)


2

RProgN 2、18バイト

x={x*'^9+0*$'E}éx*

説明した

x={x*'^9+0*$'E}éx*
x=                  # Set the value of "x" to the input.
  {           }é    # Find the first positive integer in which passing it to the defined function returns truthy.
   x*               # Multiply the index by x, this essentially searches multiples now.
     '^9+0*$'       # A Regex defined by a literal string.
             E      # Does the multiple match the regex?
                x*  # Multiple the outputted index by x, giving the result.

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


2

数学、71バイト

(x=#;While[!StringMatchQ[ToString@x,RegularExpression@"9+0*"],x+=#];x)&

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

あまり魅力的なブルートフォースソリューションではありませんが、いくつかの巧妙なトリックを使用する他のMathematicaの答えに勝っています。

Mathematicaがこの課題に関して持っている品質を引き換えるのStringMatchQは、完全な一致を必要とするという事実であるので、9+0*ではなく私ができる^9+0*$


2
Mathicsの代わりにMathematicaを使用する場合は、の"9"..~~"0"...代わりに数バイトを節約できますRegularExpression@"9+0*"
ツリーではない

1
@Notatree、ありがとう、後ほど心に留めておきますが、私はMathicsに固執します。理解できない構文を使用したくないのですが、そのような構文を見たのは初めてです。
パベル

けっこうだ。(Mathematicaのパターンマッチング構文は強力なツールですが、正規表現に精通している場合は、おそらくすでにそれを知っているでしょう!)
ツリーではない

2

バッチ、175バイト

@set/pn=
@set s=
:g
@set/ag=-~!(n%%2)*(!(n%%5)*4+1)
@if not %g%==1 set s=0%s%&set/an/=g&goto g
@set r=1
:r
@set s=9%s%
@set/ar=r*10%%n
@if %r% gtr 1 goto r
@echo %s%

STDINで入力を受け取ります。総当たりの解決策ではありませんが、実際には分数に対する正確な小数に対する答えに基づいているため、17、19などで機能します。


2

Mathematica、127バイト

Select[FromDigits/@Select[Tuples[{0,9},c=#],Count[#,9]==1||Union@Differences@Flatten@Position[#,9]=={1}&],IntegerQ[#/c]&][[1]]&


入力

[17]

出力

9999999999999999

ここに最初の20用語があります

{9、90、9、900、90、90、999999、9000、9、90、99、900、999999、9999990、90、90000、9999999999999999、90、999999999999999999、900}


1
賢いですが、明らかな解決策は最短のようです:codegolf.stackexchange.com/a/130115/60042-
パベル

あなたの明白な解決策は17を行うことができません
;

私が言えることは、最速のコード
パベル

ところで、あなたのソリューションはMathicsで動作します。それを変更してTIOリンクを追加することができます。
パベル


2

Haskell、53バ​​イト

f 整数を取り、返します。

f n=filter(all(<'1').snd.span(>'8').show)[n,n+n..]!!0

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

これは17でタイムアウトになりますが、これはテストケースをわずかに超えています。56バイトの高速バージョン:

f n=[x|a<-[1..],b<-[0..a-1],x<-[10^a-10^b],mod x n<1]!!0

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

使い方

  • f のすべての倍数を生成します n、それぞれを文字列に変換し、正しい形式のフィルタを除外してから、最初のものを取得します。

  • 代わりに、より高速なバージョンでは、必要な番号が、、、の形式10^a-10^bであることa>=1を使用しa>b>=0ます。ゴルフの目的では、minimal aでは1 bつしか動作できないという事実も使用します。これによりb、わずかに短い「間違った」順序でs を生成できます。


1

ルビー、38 + 1 = 39バイト

-pフラグを使用します。

$_=y=eval$_
1until"#{$_+=y}"=~/^9+0*$/

-p プログラムを次のように囲みます。

while gets
    ...
end
puts $_

gets結果をに保存し$_ます。evalは、より短いため、数値に変換するために使用され.to_i、その後、ブルートフォースが使用され、正規表現に一致するまで$ _が増分されます。"#{}"sttring補間であり、それは周りのparantheses .to_sを必要とするため、呼び出しよりも短くなり$_+=yます。最後に、$_印刷されます。

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

すべてのテストケースを試してください!



1

C ++、106バイト

int main(){long N,T=9,j=10,M;cin>>N;while(T%N){if(T/j){T+=(M/j);j*=10;}else{T=(T+1)*9;j=10;M=T;}}cout<<T;}

詳細フォーム:

int main()
{
    long N,T=9,j=10,M;
    cin >> N;

    while (T%N)
    {
        if (T/j)
        {
            T += (M/j);
            j *= 10;
        }
        else
        {
            T = (T+1)*9;
            j = 10;
            M = T;
        }
    } 

    cout << T;
}

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


より良いゴルフ:[](int n){int T=9,j=10,m;while(t%n)if(t/j){t+=m/j;j*=10;}else{t=(t+1)*9;j=10;m=t;}return t;}}、94バイトかかります。基本的に、バイトを保存する関数タスクとして扱い、不要な括弧を節約し、ラムダ関数を使用して型の命名と型を保存します。
-eneil

ラムダを使用してコンパイルすることはできません。手を貸してもらえますか?
koita_pisw_sou

最後に括弧を付けすぎた理由かもしれません。
enedil

さらに、ラムダは通常の関数にラップするのに97バイトかかりますが、おそらくグローバルスコープに存在する必要はありません。
enedil

1

Python 2、79バイト

x=input();n=10;y=9
while y%x:
 b=n
 while(b-1)*(y%x):b/=10;y=n-b
 n*=10
print y

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

いくつかの説明入力を分割する 最小の自然な形10**n-10**bを見つけn>b>=0ます。

いくつかのIO

f(1) = 9
f(2) = 90
f(3) = 9
f(4) = 900
f(5) = 90
f(6) = 90
f(7) = 999999
f(8) = 9000
f(9) = 9
f(10) = 90
f(11) = 99
f(12) = 900
f(13) = 999999
f(14) = 9999990
f(15) = 90
f(16) = 90000
f(17) = 9999999999999999
f(18) = 90
f(19) = 999999999999999999


1

Swift 3.0、バイト:121

var i=2,m=1,n=""
while(i>0){n=String(i*m)
if let r=n.range(of:"^9+0*$",options:.regularExpression){print(n)
break};m=m+1}

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


何をlet r=するの?r他のどこにも言及されていない
チョイス

@Cyoce let r =は、n.rangeが値nilを返すかどうかを確認します。let_ =を使用できます。ここでは、バイト数を減らすためにオプションのバインディングを使用しています。
A.プージャ

1

Python 3、62バイト

この関数は整数nを取り、mゼロに初期化します。次に、末尾のすべてのゼロを削除しm、結果に9のみが含まれているかどうかを確認し、含まれている場合は返しmます。そうでない場合は、追加nmて再度チェックするなど。

def f(n,m=0):
 while{*str(m).strip('0')}!={'9'}:m+=n
 return m

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


1

Java(OpenJDK 8)、66バイト、17でチョークしない

n->{long a=10,b=1;for(;(a-b)%n>0;b=(b<10?a*=10:b)/10);return a-b;}

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

@KevinCruijssenのソリューションよりも長いが、わずかに大きな数値を処理できます。10 ^ 6-10 ^ 3 = 999000のような候補数を計算します。64ビットの長さは依然として制限であり、n = 23で中断します。

おそらく少しゴルフをすることができますが、それを機能させるにはすでに時間がかかりすぎています...


1

> <>、35バイト

&a:v ;n-<
:,a/?(1:^!?%&:&-}:{
a*:\~

オンライン試すか、魚の遊び場で見る

入力がすでにスタック上にあると仮定します。フォーム10の番号を探しての作品 - 10 B <Bとは、(はい、少ない記号よりだこと-それは少ないバイトを取る!)それは、その後10印刷し、入力で割り切れるだまで、B  - 10 。これはブルートフォース法よりもはるかに高速です(とにかく> <>では困難です)。


1

V19 14バイト

é0òÀ/[1-8]ü09

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

説明

é0              ' <m-i>nsert a 0
  ò             ' <m-r>ecursively
   À            ' <m-@>rgument times
               ' <C-A> increment the number (eventually gives all multiples)
     /[1-8]ü09  ' find ([1-8]|09) if this errors, the number is of the form
                ' (9+0*) (because there won't ever be just zeros)
                ' implicitly end the recursion which breaks on the above error

1

JavaScript(ES6)、51 49バイト

let
f=(n,x=1,y=1)=>(x-y)%n?f(n,x,y*10):x-y||f(n,x*10)
<input type=number value=1 step=1 min=1 oninput=O.value=f(value)>
<input type=number value=9 id=O disabled>

最短のアプローチではありませんが、高速です。


1

Mathematica、82バイト

@Jenny_mathyの回答からの提出パターンを使用して...

(d=x=1;y=0;f:=(10^x-1)10^y;n:=If[y>0,y--;x++,y=d;d++;x=1];While[Mod[f,#]!=0,n];f)&

入力:

[17]

出力:

9999999999999999

そして、とJenny_mathyの答え@にあるコメントの引数に対する@Phoenix ... RepeatedTiming[]入力への適用が[17]できます

{0.000518, 9999999999999999}

半ミリ秒です。少し大きい入力に移動し[2003]ます:

{3.78, 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999}

4秒未満。

テストテーブル:最初の30個の正の整数では、結果は

{9, 90, 9, 900, 90, 90, 999999, 9000, 9, 90, 99, 900, 999999, 
9999990, 90, 90000, 9999999999999999, 90, 999999999999999999, 900, 
999999, 990, 9999999999999999999999, 9000, 900, 9999990, 999, 
99999900, 9999999999999999999999999999, 90}

説明:ここでの唯一の魔法はカスタムイテレータ(M'maセンスではなく、CSセンスの「イテレータ」)です

n := If[ y>0  ,  y-- ; x++  ,  y=d ; d++ ; x=1]

これは、グローバル変数x(先頭の「9」の数)に作用し、y数、末尾の「0」のd数、および合計桁数にます。桁数を反復処理し、桁数の選択ごとに、最大の「0」と最小の「9」で開始します。したがって、コードが最初に行うことは、1に初期化dすることであり、1に強制xy 0にます。カスタムイテレータは、「0」の文字列を短縮できることを確認します。その場合、「0」の文字列を1つ短くし、「1」の文字列を1つ増やします。そうでない場合は、桁数を増やし、「0」の数を桁数より1少ない数に設定し、「9」の数を1に設定します。dの望ましい値ですy。)


それでも、ブルートフォースや正規表現よりも長くなります。
パベル

@Phoenix:それで、2003年のタイミングは?
エリックタワーズ

1

Ti-Basic(TI-84 Plus CE)、48 41バイト

Prompt X
For(K,1,0
For(M,-K+1,0
10^(K)-10^(-M
If 0=remainder(Ans,X
Return
End
End

Promptプログラム中に入力が行われます。出力はに保存されAnsます。

説明:

(10 n)(10 m -1)= 10 k -10 mの形式の数値を試行します。ここで、m + n = kは1から始まり増加し、kの各値に対して、m = 1、n = kを試行します。 -1; m = 2、n = k-2; ... m = k、n = 0; の倍数が見つかるまでX

これは最大16まで機能します。17は、remainder(9999999999999(13ナイン)までの配当しか受け入れられず、17は9999999999999999(16ナイン)を出力するため、ドメインエラーになります。

Prompt X               # 3 bytes, input number
For(K,1,0              # 7 bytes, k in the description above; until a match is found
For(M,-K+1,0           # 10 bytes, start with n=1, m=(k-n)=k-1;
                           # then n=2, m=(k-n)=k-2, up to n=k, m=(k-n)=0
                           # (M=-m because it saved one byte)
10^(K)-10^(-M           # 8 bytes, n=(k-m) nines followed by m zeroes → Ans
If not(remainder(Ans,X # 8 bytes, If X is a factor of Ans (remainder = 0)
Return                 # 2 bytes, End program, with Ans still there
End                    # 2 bytes,
End                    # 1 byte (no newline)

1

QBIC、53バ​​イト

{p=p+1┘o=p*9_F!o$|┘n=!A!~(_l!n$|=_l!n+1$|)-(o%:)|\_Xo

説明

{        infinitely DO
p=p+1    raise p (starts out as 0)
┘o=p*9   Get the mext multiple of 9 off of p
_F!o$|   Flip a string representation of p*9
┘n=!A!   and set 'n' to be an int version of the flipped p*9 
         (this effectively drops trailing 0's)
~        This IF will subtract two values: the first is either 0 for n=x^10, or -1
         and the second bit does (p*9) modulo 'a' (input number): also 0 for the numbers we want
(
 _l!n$|  the length of n's string representation
=        is equal to
_l!n+1$| the length of (n+1)'s string rep (81 + 1 = 82, both are 2 long; 99 + 1 = 100, there's a difference)
)        The above yields -1 (Qbasic's TRUE value) for non-9 runs, 0 for n=x^10
-        Subtract from that 
(o%:)    (p*9) modulo a     0 for p*9 = a*y
|       THEN (do nothing, since we want 0+0=0 in the conditionals above, execution of the right path jumps to ELSE
\_Xo    ELSE quit, printing (p*9)

1

C(gcc)、126バイト

#include<stdio.h>
main(x,n,y,b){n=10;y=9;scanf("%d",&x);while(y%x){b=n;while((b-1)*(y%x)){b/=10;y=n-b;}n*=10;}printf("%d",y);}

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

いくつかの説明入力を分割する 最小の自然な形10**n-10**bを見つけn>b>=0ます。

いくつかのIO

f(1) = 9
f(2) = 90
f(3) = 9
f(4) = 900
f(5) = 90
f(6) = 90
f(7) = 999999
f(8) = 9000
f(9) = 9
f(10) = 90
f(11) = 99
f(12) = 900
f(13) = 999999
f(14) = 9999990
f(15) = 90
f(16) = 90000

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