Digit Sum Integer Sequence (working title)
Inspired by this challenge. If anyone has any suggestions for a song title I could use for the challenge title then please leave a comment.
Definition
We define f(x)
for a given integer as the sum of:
-
x
itself, -
s
, the smallest digit in the decimal representation ofx
, and, -
l
, the largest digit in the decimal representation ofx
Examples
x |
s |
m |
f(x) |
---|---|---|---|
2 | 2 | 2 | 6 |
53 | 3 | 5 | 61 |
77 | 7 | 7 | 91 |
123 | 1 | 3 | 127 |
8022 | 0 | 8 | 8030 |
Challenge
You will be building a sequence by repeatedly applying f(x)
to a starting value n>0
taken as input, with the first term in the sequence being n
itself.
For example, given a starting value of n=1
, the first few terms go as follows:
1, 3, 9, 27, 36, 45, 54, 63, ...
You will also take an integer i
as input and then either:
- Output the
i
th term (0-indexed or 1-indexed, but please specify) of then
-based sequence, - Output the the first
i
terms of that sequence, or, - Forego taking
i
as input and output then
-based sequence infinitely.
Rules
- I/O can be by any reasonable, convenient means.
- This is code-golf so lowest byte count in each language wins.
Test Cases
Here are the first 25 terms of each sequence from n=1
to n=10
:
1, 3, 9, 27, 36, 45, 54, 63, 72, 81, 90, 99, 117, 125, 131, 135, 141, 146, 153, 159, 169, 179, 189, 199, 209
2, 6, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 117, 125, 131, 135, 141, 146, 153, 159, 169, 179, 189, 199, 209
3, 9, 27, 36, 45, 54, 63, 72, 81, 90, 99, 117, 125, 131, 135, 141, 146, 153, 159, 169, 179, 189, 199, 209, 218
4, 12, 15, 21, 24, 30, 33, 39, 51, 57, 69, 84, 96, 111, 113, 117, 125, 131, 135, 141, 146, 153, 159, 169, 179
5, 15, 21, 24, 30, 33, 39, 51, 57, 69, 84, 96, 111, 113, 117, 125, 131, 135, 141, 146, 153, 159, 169, 179, 189
6, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 117, 125, 131, 135, 141, 146, 153, 159, 169, 179, 189, 199, 209, 218
7, 21, 24, 30, 33, 39, 51, 57, 69, 84, 96, 111, 113, 117, 125, 131, 135, 141, 146, 153, 159, 169, 179, 189, 199
8, 24, 30, 33, 39, 51, 57, 69, 84, 96, 111, 113, 117, 125, 131, 135, 141, 146, 153, 159, 169, 179, 189, 199, 209
9, 27, 36, 45, 54, 63, 72, 81, 90, 99, 117, 125, 131, 135, 141, 146, 153, 159, 169, 179, 189, 199, 209, 218, 227
10, 11, 13, 17, 25, 32, 37, 47, 58, 71, 79, 95, 109, 118, 127, 135, 141, 146, 153, 159, 169, 179, 189, 199, 209
More test cases can be generated using this ungolfed Japt implementation, where the first integer in the header is n
and the second is i
.
[Haskell], 56 bytes …
3y ago
[Python 3], 68 bytes ``` …
1y ago
[jq], 38 bytes while(1; …
3y ago
[AWK], 82 bytes {split( …
6mo ago
Dyalog APL, 23 bytes ```apl …
1y ago
Ruby, 36 bytes Infinite ver …
3y ago
[Python 3], 183 181 175 bytes …
3y ago
7 answers
jq, 38 bytes
while(1;.+("\(.)"|explode|max+min-96))
prints the infinite sequence starting with $n$.
0 comment threads
Python 3, 68 bytes
def g(n):
while True:print(n);d=[*map(int,str(n))];n+=min(d)+max(d)
Outputs the n
-based sequence indefinitely, starting with n
.
AWK, 82 bytes
{split($1,d,"");i=a=d[1];for(x in d){d[x]>a?a=d[x]:0;d[x]<i?i=d[x]:0}print a+i+$1}
Just pass your input, e.g.:
echo "123" | awk '...'
0 comment threads
Ruby, 36 bytes
Infinite version:
n=1;0while n+=p(n).digits.minmax.sum
Try this online!
(program is interrupted after it reaches the 128KiB limit of output)
0 comment threads
Python 3, 183 181 175 bytes
def f(a,b):
c=a;a=str(a)
for i in range(b):
y=0;z=9
for j in range(len(a)):
if y<int(a[j]):y=int(a[j])
if z>int(a[j]):z=int(a[j])
c=int(a)+y+z;a=str(c)
print(c)
I decided to go for outputting the i
th value of the sequence, 0-indexed. a
is the number to be sequenced and b
is the index of the non-existent list--since no list exists--to be printed.
0 comment threads
Dyalog APL, 23 bytes
{⍵+(⌈/+⌊/)10⊥⍣¯1⊢⍵}⍣⎕⊢1⎕
Takes an input n and outputs the 0-indexed nth item of the sequence.
{⍵+(⌈/+⌊/)10⊥⍣¯1⊢⍵}⍣⎕⊢⎕
⊢⎕ ⍝ Starting from <input>,
⍣⎕ ⍝ repeat <input> times
⍵+ ⍝ the number plus
(⌈/+⌊/) ⍝ the minimum plus the maximum of
10⊥⍣¯1 ⍝ the base-10 representation
⊢⍵ ⍝ of the number.
💎
Created with the help of Luminespire.
1 comment thread