MathsReadCompetitive mathematics
Algorithmic puzzle · integer answerhard90 min

Far-apart decimal neighbors

Digit Algorithms

Count seven-digit positive integers in which neighboring digits differ in value by at least 3 and the sum of the seven digits is divisible by 9.

Solve using mathematics, algorithms, or your own code offline. Submit only the final integer here—no program, expression, graph, or written proof.

Check your answer without signing in. Sign in to save your progress.

Accepted format: a whole number such as 42 or 123456789. No commas, formulas, or source code.

How to approach this problem

Use a state consisting of length so far, last digit and sum modulo 9. Extend only with digits far enough from the previous one; prohibit zero in the first position.

Worked example with different values

For 147, both differences are 3 and the digit sum is 12, whose remainder modulo 9 is 3. Track this remainder rather than storing the entire prefix.

Apply the same reasoning to the challenge. This example is a separate practice problem.

Learn the technique: Number theory: divisors, gcd and modular arithmetic

Report a question or explanation issue

Back to problems →