2021-06-15 07:49:18 +00:00
---
id: 5900f4151000cf542c50ff27
2021-11-10 16:27:30 +00:00
title: 'Problema 168: Rotações de números'
2021-06-15 07:49:18 +00:00
challengeType: 5
forumTopicId: 301802
dashedName: problem-168-number-rotations
---
# --description--
2021-11-10 16:27:30 +00:00
Considere o número 142857. Podemos girar esse número para a direita movendo o último algarismo (7) para a frente dele, nos dando 714285.
2021-06-15 07:49:18 +00:00
2021-11-10 16:27:30 +00:00
Pode-se ver que $714285 = 5 × 142857$.
2021-06-15 07:49:18 +00:00
2021-11-10 16:27:30 +00:00
Isto demonstra uma propriedade incomum de 142857: ele é um divisor de sua rotação à direita.
2021-06-15 07:49:18 +00:00
2022-06-14 17:15:11 +00:00
Para um número inteiro de dígitos $a$ e $b$, encontre os 5 últimos dígitos da soma de todos os inteiros $n$, $10^a < n < 10^b$ que têm essa propriedade.
2021-06-15 07:49:18 +00:00
# --hints--
2022-06-14 17:15:11 +00:00
`numberRotations(2, 10)` deve retornar `98311` .
2021-06-15 07:49:18 +00:00
```js
2022-06-14 17:15:11 +00:00
assert.strictEqual(numberRotations(2, 10), 98311);
```
`numberRotations(2, 100)` deve retornar `59206` .
```js
assert.strictEqual(numberRotations(2, 100), 59206);
2021-06-15 07:49:18 +00:00
```
# --seed--
## --seed-contents--
```js
2022-06-14 17:15:11 +00:00
function numberRotations(a, b) {
2021-06-15 07:49:18 +00:00
2022-06-14 17:15:11 +00:00
return 0;
2021-06-15 07:49:18 +00:00
}
2021-11-10 16:27:30 +00:00
numberRotations();
2021-06-15 07:49:18 +00:00
```
# --solutions--
```js
2022-06-14 17:15:11 +00:00
function numberRotations(minDigits, maxDigits) {
const DIGITS_TO_KEEP = 100000n;
const powersOfTen = Array(maxDigits).fill(0);
powersOfTen[0] = 1n;
for (let i = 1; i < maxDigits ; i + + ) {
powersOfTen[i] = powersOfTen[i - 1] * 10n;
}
// We want numbers of the form xd * m = dx
// Or more precisely:
// (x * 10 + d) * m = d*10^(n-1) + x
// Solving for x:
// x = d (10^(n-1) - m) / (10 * m - 1)
let total = 0n;
for (let numDigits = minDigits; numDigits < = maxDigits; numDigits++) {
// Check all multiplier - digit pairs to see if a candidate can be built
// with the correct number of digits
for (let multiplier = 1n; multiplier < 10n ; multiplier + + ) {
for (let lastDigit = 1n; lastDigit < 10n ; lastDigit + + ) {
const numerator = lastDigit * (powersOfTen[numDigits - 1] - multiplier);
const denominator = (powersOfTen[1] * multiplier - 1n);
if (numerator % denominator === 0n) {
const candidate = (numerator / denominator) * 10n + lastDigit;
if (candidate.toString().length === numDigits) {
total = (total + candidate) % DIGITS_TO_KEEP;
}
}
}
}
}
return parseInt(total);
}
2021-06-15 07:49:18 +00:00
```