Dado um número inteiro n > 9
, para cada inserção possível entre dígitos nesse número inteiro, insira uma adição +
e avalie. Em seguida, pegue o número original modulo desses resultados. Emita a soma total dessas operações.
Um exemplo com n = 47852
:
47852 % (4785+2) = 4769
47852 % (478+52) = 152
47852 % (47+852) = 205
47852 % (4+7852) = 716
-----
5842
Entrada
Um único número inteiro positivo em qualquer formato conveniente ,n > 9
.
Saída
A única saída inteira seguindo a técnica de construção acima.
Regras
- Você não precisa se preocupar com entradas maiores que o tipo padrão do seu idioma.
- Um programa completo ou uma função são aceitáveis. Se uma função, você pode retornar a saída em vez de imprimi-la.
- Lacunas padrão são proibidas.
- Isso é código-golfe, portanto todas as regras usuais de golfe se aplicam e o código mais curto (em bytes) vence.
Exemplos
47852 -> 5842
13 -> 1
111 -> 6
12345 -> 2097
54321 -> 8331
3729105472 -> 505598476
code-golf
math
number-theory
AdmBorkBork
fonte
fonte
D.s¨s.p¨R+¹s%O
without seeing this ;PJavaScript,
4347 bytesTakes input as string.
Edit:
+4 bytes: Leading zeroes in JavaScript converts the number to octal ):
fonte
(+'$&$''+$`)
?$`
is empty, and it will throw error trying to eval(13+)
(as example).Brachylog, 20 bytes
Try it online!
Explanation
This implements the formula given. The only thing we have to be careful about is when a
0
is in the middle of the input: in that case Brachylog gets pretty quirky, for example it won't accept that a list of integers starting with a0
can be concatenated into an integer (which would require ignoring the leading0
— this is mainly programmed that way to avoid infinite loops). Therefore to circumvent that problem, we convert the input to a string and then convert back all splitted inputs into integers.fonte
ES6 (Javascript),
42, 40 bytesEDITS:
Golfed
Test
fonte
m<2**31
then you can start withx=1
for a saving of a byte.Python 2, 45 bytes
Uses arithmetic rather than strings to divide the input
n
into partsn/c
andn%c
, whichc
recurses through powers of 10.fonte
Jelly, 12 bytes
TryItOnline!
How?
fonte
Perl
35 3227 BytesIncludes +3 for
-p
Saved 8 bytes thanks to Dada
fonte
C 77 + 4 = 81 bytes
golfed
Ungolfed
fonte
r=0
such that if the function is called again the result is correct. It is somewhere in Meta, if you use global variables then you have to deal with the side effects of calling a function more than once.r
global but inside the function as a statement you can sayr=0;
, see my answer for example.Python 2,
686468 bytes-4 bytes thanks to atlasologist
*Input is a string
fonte
lambda n:sum(int(n)%eval(n[:i]+'+'+n[i:])for i in range(len(n)))
8
or9
after it and gives wrong answers for others (like the last test case). Numbers starting with a zero are octal. repl.it/EmMmC, 59 bytes
t
is10,100,1000,...
and represents the cut in the big number.n/t
is the right part andn%t
the left part. Ift
is bigger than the number, it is finished.Ungolfed and usage:
fonte
Retina, 38 bytes
Byte count assumes ISO 8859-1 encoding.
Not exactly efficient...
Try it online! (The first line enables a linefeed-separated test suite.)
Explanation
Between every pair of characters, we insert a comma, everything in front of the match, a semicolon, the entire input, a linefeed, and everything after the match. For input
12345
this gives us:I.e. every possible splitting of the input along with a pair of the input. We don't need that last line though so:
We discard it.
This replaces each number as well as the comma with its unary representation. Since the comma isn't a number, it's treated as zero and simply removed. This adds the two parts in each splitting.
This computes the modulo by removing all copies of the first number from the second number.
That's it, we simply count how many
1
s are left in the string and print that as the result.fonte
Pyth, 14 bytes
A program that takes input of an integer and prints the result.
Test suite
How it works
fonte
Haskell, 62 bytes
Defines a function
f
. See it pass all test cases.fonte
Perl 6, 33 bytes
Expanded:
fonte
Mathematica, 75 bytes
This uses pattern matching on the list of digits to extract all partitions of them into two parts. Each such partition into
a
andb
is then replaced withThe notable thing here is that sums of lists of unequal length remain unevaluated, so e.g. if
a
is1,2
andb
is3,4,5
then we first replace this with{1,2} + {3,4,5} + {}
. The last term is there to ensure that it still remains unevaluated when we evenly split an even number of digits. Now theMap
operation in Mathematica is sufficiently generalised that it works with any kind of expression, not just lists. So if we mapFromDigits
over this sum, it will turn each of those lists back into a number. At that point, the expression is a sum of integers, which now gets evaluated. This saves a byte over the more conventional solutionTr[FromDigits/@{{a},{b}}]
which converts the two lists first and then sums up the result.fonte
Actually,
1615 bytesGolfing suggestions welcome! Try it online!
Edit: -1 byte thanks to Teal pelican.
Ungolfing
fonte
╤╜d+╜%
MΣ)Ruby, 64 bytes
Takes the input as a string
fonte
0
as octal, meaning this fails for the last test case. Here's a 78-byte solution addressing that.Befunge,
10196 bytesTry it online!
Explanation
fonte
APL, 29 bytes
⎕IO
must be1
. Explanation (I'm not good at explaining, any imporvements to this are very welcome):fonte
C#, 67 bytes
Full program with ungolfed, explained method and test cases:
fonte
Attache, 48 bytes
Try it online!
Explanation
fonte
Clojure,
9181 bytesEdit: this is shorter as it declares an anonymous function
(fn[v](->> ...))
and not using->>
macro although it was easier to read and call that way.Original:
Generates a sequence of 1, 10, 100, ... and takes first 10 items (assuming input values are less than 10^11), maps to modulos as specified in specs and calculates the sum. Long function names makes this solution quite long but at least even the golfed version should be quite easy to follow.
First I tried juggling strings around but it required tons of boilerplate.
fonte
Racket 134 bytes
Ungolfed:
Testing:
Output:
fonte
R, 50 bytes
Try it online!
fonte
SNOBOL4 (CSNOBOL4), 92 bytes
Try it online!
fonte
Ruby 45 Bytes
This is a really neat solution. It is technically correct, but it is super inefficient. It would be much more efficient to write q.to_s.size.times{...}. We use q.times because it saves characters, and the extra number of times it goes through the proc the expression just evaluates to zero.
fonte
->q{(0..q).reduce{|s,x|p=10**x;s+q%(q/p+q%p)}}
PHP, 60 bytes
Try it online!
fonte
Java 8,
12766 bytes-61 bytes by creating a port of @adrianmp's C# answer.
Try it here.
fonte
Pari/GP, 42 bytes
Try it online!
fonte
Japt,
1110 bytesTry it
Explanation
fonte