最小倍数为9,然后是可选的0


22

给定一个正整数,找到其最小的正整数倍,它是9的游程,然后是一个可选的0 /^9+0*$/

例如,如果给定的正整数为2,则返回90,因为90是2的正整数倍,并且是正则表达式匹配的最小整数/^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
我认为更多的测试用例会很好地检查解决方案是否要求9出现在0之前。
xnor

2
@LeakyNun可能不是,但是9900099是,并且不应根据规则被允许。
DrQuarius

2
@koita_pisw_sou的规则是,程序应“理论上”为给定任意精度,内存和时间的任何整数工作。
Leaky Nun

Answers:


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
短一个字节: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;}

由于@JollyJoker, -4个字节(更快的执行速度)。

说明:

在这里尝试。

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

是的,优化!^^
OlivierGrégoire17年

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

我已经尝试过,并尝试着继续进行整数和数学运算,但是我无法超越!恭喜:)
OlivierGrégoire'17


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数学,你可以节省一些字节,"9"..~~"0"...而不是RegularExpression@"9+0*"
不是一棵树

1
@Notatree谢谢,稍后我会记住它,但我会坚持使用数学。我不想使用我不了解的语法,这是我第一次看到这样的语法。
帕维尔

很公平。(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
Pavel

您明显的解决方案不能执行17 ;-)
J42161217

我能说什么,而不是最快的代码
帕维尔

顺便说一下,您的解决方案可在Mathics中工作,您可以将其更改为该值并添加TIO链接。
Pavel


2

哈斯克尔,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^ba>=1a>b>=0。出于打高尔夫球的目的,它还使用以下事实:对于极小值a,只能工作一个 b,这使得它可以以b稍短的“错误”顺序生成。


1

红宝石,38 +1 = 39字节

使用-p标志。

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

-p 围绕程序:

while gets
    ...
end
puts $_

gets将其结果存储在中$_eval用于将其转换为数字,因为它比短.to_i,然后使用蛮力,将$ _递增,直到它与正则表达式匹配。"#{}"插值很困难,比.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个字节。本质上,将其作为函数任务来保存字节,保存不需要的括号,使用lambda函数保存类型命名和类型。
尼迪尔

无法使用lambda进行编译。你能帮忙吗?
koita_pisw_sou

这可能是我在括号中加了太多括号的原因。
艾德尼尔2017年

此外,lambda可能不必在全局范围内存在,即使将其包装到常规函数中需要97个字节。
埃德尼尔2016年

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**bn>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

@Cyoce let r =检查n.range是否返回nil值。您可以使用let _ =。我在这里使用可选绑定来减少字节数。
A. Pooja'7

1

Python 3 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 a  -10 b且带有<b的数字(是的,这是一个小于号-占用更少的字节!),直到被输入可整除,然后打印10 b  -10 a来工作。这比蛮力法(无论如何在<<>中都很难)要快得多。


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}

说明:这里唯一的魔力是自定义迭代器(CS意义上的“迭代器”,而不是M'ma意义上的)

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

它作用于全局变量x,前导“ 9”的y数量,尾随“ 0”的d数量以及数字的总数。我们希望迭代数字的位数,并且对于每种数字位数的选择,均以最大的“ 0”和最小的“ 9”开始。因此,代码要做的第一件事是初始化d为1,强制是所需的。)值。x为1和y0。自定义迭代器检查“ 0”的字符串可以缩短。如果是这样,它将字符串“ 0”缩短一,并将字符串“ 1”增加一。如果不是,它将增加位数,将“ 0”的数量设置为小于位数的数量,并将“ 9”的数量设置为1。dy


但是,仍然比蛮力和正则表达式更长。
帕维尔

@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在程序执行过程中输入-ed。输出存储在中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个9)的股息,而17应该输出9999999999999999(16个9)。

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**bn>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

1

Perl 5、23 + 2(-pa)= 25个字节

蛮力法

$_+=$F[0]while!/^9+0*$/

在线尝试!

它很慢,但是很小。

更有效的方法:

41 + 2(-pa)= 43字节

$_=9;s/0/9/||($_=9 .y/9/0/r)while$_%$F[0]

在线尝试!

它适用于任何输入,但代码较长。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.