Dado um número inteiro positivo, encontre o menor múltiplo inteiro positivo positivo, que é uma execução de 9 seguida por uma execução opcional de 0. Em outras palavras, encontre o menor múltiplo inteiro positivo positivo que é correspondido pelo regex /^9+0*$/
.
Por exemplo, se o número inteiro positivo especificado for 2, retorne 90, pois 90 é um número inteiro positivo de 2 e é o menor que é correspondido pelo regex /^9+0*$/
.
Casos de teste:
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
Isso é código-golfe . A resposta mais curta em bytes vence. Aplicam-se brechas padrão .
code-golf
arithmetic
Freira Furada
fonte
fonte
Respostas:
Geléia ,
1311 bytesExperimente online!
Como funciona
fonte
9
ou0
em seu códigoPython 2 ,
5554 bytesExperimente online!
fonte
Python 2 , 51 bytes
Experimente online!
fonte
JavaScript (ES6),
474342 bytes-4 bytes graças a @Arnauld
-1 byte graças a @Luke
Testes
Solução recursiva (falha para 7, 13 e 14), 38 bytes
Chamado como
f(5)()
. Atinge o tamanho da pilha de chamadas max no Chrome e Firefox paran=7
,n=13
, en=14
.Mostrar snippet de código
fonte
n=>eval('for(i=0;!/^9+0*$/.test(i);)i+=n')
Ruby , 36 bytes
Força bruta - leva uma eternidade para x = 17.
Experimente online!
fonte
Java 8,
6157 bytes-4 bytes (e execução mais rápida) graças a @JollyJoker .
Explicação:
Experimente aqui.
fonte
r%n
check,n->{int r=0;for(;!(""+(r+=n)).matches("9+0*"););return r;}
for(;!(""+r).matches("9+0*");r+=n)
Python 3, 56 bytes
Try it online!
fonte
Brachylog, 16 bytes
Try it online!
This is pretty slow
Explanation
fonte
05AB1E, 10 bytes
Try it online!
It just keeps adding the input to 0, until the result minus leading 9's equals 0.
fonte
RProgN 2, 18 bytes
Explained
Try it online!
fonte
Mathics, 71 bytes
Try it online!
Not very intresting brute force solution, but it beats the other Mathematica answer, which uses some clever tricks.
The one redeeming quality Mathematica has in regards to this challenge is the fact that
StringMatchQ
requires a full match, so I can do9+0*
rather than^9+0*$
.fonte
"9"..~~"0"...
instead ofRegularExpression@"9+0*"
.Batch, 175 bytes
Takes input on STDIN. Not a brute force solution but in fact based on my answer to Fraction to exact decimal so it will work for 17, 19, etc. which would otherwise exceed its integer limit anyway.
fonte
Mathematica, 127 bytes
Input
Output
here are the first 20 terms
fonte
Haskell, 53 bytes
f
takes and returns an integer.Try it online!
This times out for 17, which conveniently is just beyond the test cases. A faster version in 56 bytes:
Try it online!
How it works
f
generates all multiples ofn
, converts each to a string, filters out those with the right format, then takes the first one.The faster version instead uses that the required numbers are of the form
10^a-10^b
,a>=1
,a>b>=0
. For golfing purposes it also uses the fact that for the minimala
, only oneb
can work, which allows it to generate theb
s in the slightly shorter "wrong" order.fonte
Ruby, 38 + 1 = 39 bytes
Uses
-p
flag.-p
surrounds the program with:gets
stores its result in$_
.eval
is used to convert it to a number, as it's shorter than.to_i
, then brute force is used, incrementing $_ until it matches the regex."#{}"
is sttring interpolation, it's shorter than a.to_s
call as that would require parantheses around$_+=y
. Finally,$_
is printed.Try it online!
Try all test cases!
fonte
Dyalog APL, 34 bytes
Recursive dfns, based on Dennis' Python solution.
fonte
C++, 106 bytes
Detailed Form:
TRY it online!
fonte
[](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;}}
, takes 94 bytes. Essentially, treat it as a function task to save bytes, save up on unneeded parentheses, use lambda function to save on type naming and type.Python 2, 79 bytes
Try it online!
Some explanations It finds the smallest natural of form
10**n-10**b
withn>b>=0
that divides the input.Some IO
fonte
PHP, 39 bytes
Try it online!
PHP, 52 bytes
Try it online!
fonte
Swift 3.0, Bytes:121
Try it online!
fonte
let r=
do? I don't seer
referred to anywhere elsePython 3, 62 bytes
This function takes an integer
n
and initializesm
to zero. Then it removes all zeros from the ends ofm
and checks if the result only contains 9's, returningm
if it does. If not, it addsn
tom
and checks again, etc.Try it online!
fonte
Java (OpenJDK 8), 66 bytes, doesn't choke on 17
Try it online!
Longer than @KevinCruijssen's solution but can handle slightly larger numbers. It calculates the candidate numbers like 10^6 - 10^3 = 999000. 64-bit longs are still the limit, breaking for n=23.
Can probably be golfed a bit but already took too long to make it work...
fonte
><>, 35 bytes
Try it online, or watch it at the fish playground!
Assumes the input is already on the stack. Works by looking for numbers of the form 10a − 10b, with a < b (yes, that's a less than sign — it takes fewer bytes!) until that's divisible by the input, then printing 10b − 10a. This is much faster than the brute force method (which would be difficult in ><> anyway).
fonte
V,
1914 bytesTry it online!
Explanation
fonte
JavaScript (ES6),
5149 bytesNot the shortest approach, but it is wicked fast.
fonte
Mathematica, 82 bytes
Using the pattern of submission from @Jenny_mathy 's answer...
Input:
Output:
And relative to the argument in comments at @Jenny_mathy's answer with @Phoenix ...
RepeatedTiming[]
of application to the input[17]
givesso half a millisecond. Going to a slightly larger input,
[2003]
:a bit under 4 seconds.
Test table: On the first 30 positive integers, the results are
Explanation: The only magic here is the custom iterator ("iterator" in the CS sense, not the M'ma sense)
which acts on the global variables
x
, the number of leading "9"s,y
, the number of trailing "0"s, andd
, the total number of digits. We wish to iterate through the number of digits, and, for each choice of number of digits, start with the most "0"s and the least "9"s. Thus the first thing the code does is initialized
to 1, forcingx
to 1 andy
to 0. The custom iterator checks that the string of "0"s can be shortened. If so, it shortens the string of "0"s by one and increases the string of "1"s by one. If not, it increments the number of digits, sets the number of "0"s to one less than the number of digits, and sets the number of "9"s to 1. (This is actually done in a slightly different order to shave a few characters, since the old value ofd
is the desired value ofy
.)fonte
Ti-Basic (TI-84 Plus CE),
4841 bytesInput is
Prompt
-ed during the program; output is stored inAns
.Explanation:
Tries numbers of the form (10n)(10m-1) = 10k-10m, where m+n=k starts at 1 and increases, and for each value of k, it tries m=1,n=k-1; m=2,n=k-2; ... m=k,n=0; until it finds a multiple of
X
.This works up to 16; 17 gives a domain error because
remainder(
can only accept dividends up to 9999999999999 (13 nines), and 17 should output 9999999999999999 (16 nines).fonte
QBIC, 53 bytes
Explanation
fonte
C (gcc), 126 bytes
Try it online!
Some explanations It finds the smallest natural of form
10**n-10**b
withn>b>=0
that divides the input.Some IO
fonte
Perl 5, 23 + 2 (-pa) = 25 bytes
Brute Force Method
Try it online!
It's slow, but it's tiny.
More Efficient Method:
41 + 2 (-pa) = 43 bytes
Try it online!
It works well for any input, but it's longer code.
fonte