41
\$\begingroup\$

Consider taking some non-negative integer such as 8675309 and computing the absolute values of the differences between all the pairs of neighboring digits.

For \$8675309\$ we get \$|8-6| = 2\$, \$|6-7| = 1\$, \$|7-5| = 2\$, \$|5-3| = 2\$, \$|3-0| = 3\$, \$|0-9| = 9\$. Stringing these results together yields another, smaller non-negative integer: \$212239\$. Repeating the process gives \$11016\$, then \$0115\$, which by the convention that leading zeros are not written simplifies as \$115\$, which becomes \$04\$ or \$4\$, which can't be reduced any further. Summing all these values up we get \$8675309 + 212239 + 11016 + 115 + 4 = 8898683\$.

Let's define the Digit Difference Sum (or DDS) as this operation of repeatedly taking the digit differences of a number to form a new number, then adding all the resulting numbers to the original.

Here are the first 20 values in the corresponding DDS sequence:

N   DDS(N)
0   0
1   1
2   2
3   3
4   4
5   5
6   6
7   7
8   8
9   9
10  11
11  11
12  13
13  15
14  17
15  19
16  21
17  23
18  25
19  27

Here are the first 10000 values, the graph for which is quite curious:

DDS 10000 plot

Especially since it looks the same when you plot it to 1000 or even 100:

DDS 1000 plot

DDS 100 plot

(I'd call it the dentist's staircase...)

Challenge

Write a program or function that takes in a non-negative integer and prints or returns its DDS value. For example, if the input was 8675309, the output should be 8898683.

The shortest code in bytes wins.

\$\endgroup\$
4
  • \$\begingroup\$ dentist's staircase? \$\endgroup\$
    – Martijn
    Commented Sep 20, 2015 at 12:47
  • 12
    \$\begingroup\$ @MartijnR Dentist's staircase. \$\endgroup\$ Commented Sep 20, 2015 at 13:14
  • \$\begingroup\$ @Calvin'sHobbies Orthodontist's staircase? \$\endgroup\$
    – Beta Decay
    Commented Sep 21, 2015 at 16:03
  • 1
    \$\begingroup\$ @BetaDecay Dentist's staircase. \$\endgroup\$
    – Alex A.
    Commented Sep 21, 2015 at 17:08

32 Answers 32

1
2
0
\$\begingroup\$

05AB1E, 10 9 bytes

λ£üαJï}þO

Try it online or verify the first 1001 test cases.

Explanation:

Unfortunately the Cumulative fixed-point builtin (to reduce until the result no longer changes and collecting all intermediate results) won't include the starting value. In addition, we'll have to remove the trailing empty string value. So it would be 10 bytes: .ΓüαJï}¨O+ - try it online.

So using this unorthodox method with the recursive environment saves a byte in the end:

λ      # Start a recursive environment,
 £     # to calculate the values in the range [a(0),a(implicit input)]
       # Starting with a(0) = (implicit) input
       # Where every following a(n) is calculated as:
       #  (implicitly push a(n-1))
  ü    #  For each overlapping digit:
   α   #   Get the absolute difference
    J  #  Join these differences together to a string
     ï #  Remove potential leading 0s (by casting to an integer)
}      # After the recursive environment
 þ     # Remove all empty trailing strings by only keeping numbers
  O    # Sum those numbers together
       # (which is output implicitly as result)
\$\endgroup\$
0
\$\begingroup\$

Julia 1.4, 47 bytes

!n=n+(n>9&&!evalpoly(10,abs.(diff(digits(n)))))

Attempt This Online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.