给定一个正整数,找到其最小的正整数倍,它是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
给定一个正整数,找到其最小的正整数倍,它是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
Answers:
ṚḌ‘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.
9
或者0
在你的代码
@ 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))
n=>g=(i=0)=>/^9+0*$/.test(i+=n)?i:g(i)
叫做f(5)()
。到达Chrome和Firefox的最大调用堆栈大小n=7
,n=13
和n=14
。
n=>eval('for(i=0;!/^9+0*$/.test(i);)i+=n')
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
r%n
检查,n->{int r=0;for(;!(""+(r+=n)).matches("9+0*"););return r;}
for(;!(""+r).matches("9+0*");r+=n)
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.
(x=#;While[!StringMatchQ[ToString@x,RegularExpression@"9+0*"],x+=#];x)&
虽然不是很吸引人的蛮力解决方案,但是它击败了Mathematica的其他答案,后者使用了一些巧妙的技巧。
Mathematica在应对这一挑战方面具有的一种品质就是StringMatchQ
需要一场完整比赛,因此我可以做9+0*
而不是^9+0*$
。
"9"..~~"0"...
而不是RegularExpression@"9+0*"
。
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}
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
。出于打高尔夫球的目的,它还使用以下事实:对于极小值a
,只能工作一个 b
,这使得它可以以b
稍短的“错误”顺序生成。
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函数保存类型命名和类型。
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
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
提到其他地方
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时中断。
也许可以打些高尔夫球,但是已经花了很长时间才能使它起作用。
é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
使用@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和y
0。自定义迭代器检查“ 0”的字符串可以缩短。如果是这样,它将字符串“ 0”缩短一,并将字符串“ 1”增加一。如果不是,它将增加位数,将“ 0”的数量设置为小于位数的数量,并将“ 9”的数量设置为1。d
y
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)
{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)
#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