[Question]
  [
In this challenge you will be tasked with implementing a sequence of natural numbers such that:
* Each number appears a natural number of times
* No two numbers appear the same number of times
* No two numbers appear in the sequence next to each other more than once.
For some examples, the sequence
```
1,2,3,1,4,5,1,6,7,1,8,9,1,10,11,1,...
```
Is not valid because `1` appears an infinite number of times.
The sequence:
```
1,2,3,4,5,6,7,8,9,10,11,12,13,14,...
```
Is also not valid because every number appears exactly once, but different numbers must appear a distinct number of times.
The sequence:
```
1,1,2,2,3,2,4,3,5,3,6,3,7,4,8,4,9,4,10,4,11,5,...
```
meets the first two criteria but `2` occurs next to `3` two different times (`2,3,2`).
One sequence that meets all the criteria is:
```
1,1,2,2,3,4,2,5,6,3,7,8,3,9,10,3,11,12,4,13,14,4,15,16,4,17,18,4,19,20,5,21,22,5,23,24,5,25,26,5,...
```
You may use any option available in the default [sequence](/questions/tagged/sequence "show questions tagged 'sequence'") rules. You should assume natural numbers include 0. You may assume they don't, but it won't do you any help.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so the goal is to minimize the size of your source code as measured in bytes.
  
[Answer]
# [JavaScript (V8)](https://v8.dev/), 37 bytes
```
for(i=0;;print(i**.5>>i%2))print(++i)
```
[Try it online!](https://tio.run/##y0osSyxOLsosKNEts/j/Py2/SCPT1sDauqAoM69EI1NLS8/Uzi5T1UhTEyKirZ2p@f8/AA "JavaScript (V8) – Try It Online")
# [JavaScript (V8)](https://v8.dev/), ~~41~~ 40 bytes
```
for(i=0;;i%2||print(i**.5>>1))print(++i)
```
[Try it online!](https://tio.run/##y0osSyxOLsosKNEts/j/Py2/SCPT1sDaOlPVqKamoCgzr0QjU0tLz9TOzlBTE8LX1s7U/P8fAA "JavaScript (V8) – Try It Online")
I don't know how it works but it seems fine and \$i\$ appears \$4i+3\$ times except 0 which appears 1 time
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), ~~8~~ ~~5~~ 3 bytes
```
₅[L
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCLigoVbTCIsIiIsIiJd)
Modified Kevin Cruijssen's 05AB1E answer. Equivalent python code: `lambda n: n if n%5 != 0 else len(str(n))`
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog) (v2), 4 bytes
```
√ℕl|
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/pvaGBg8Gj@skdNjY/aNgDJh1snlP9/1DHrUcvUnJr//wE "Brachylog – Try It Online")
A function submission, which takes the 1-based index of the position within the sequence as an argument, and returns the value of the sequence at that index, one of the allowed default I/O formats for [sequence](/questions/tagged/sequence "show questions tagged 'sequence'"). (The header in the TIO link calls the function with all indexes from 1 to 1000, printing the results, and therefore displays the first 1000 elements of the sequence.)
## Algorithm
This solution's algorithm is pretty similar to the other short entries: if the input is not a square number, it's returned unchanged; if it is a square number, the program returns the number of digits in its square root. As such, each natural number \$n\$ appears \$9\times 10^n\$ times, plus one time more if the number isn't square, meaning that there are no duplicate numbers-of-occurrences; and because the difference between any two positive square numbers is at least 3, there can't be any repeated pairs.
## Explanation
```
√ℕl|
√     {if} the square root of {the input}
 ℕ      is a natural number
  l   {then} {return} {the square root's} length {in digits}
   |  otherwise, {return the input unchanged}
```
(In explanations, I use the `{}` notation for things that are implied by rather than explicitly stated in the program, such as tacit variables and default behaviours of the language – I can't remember ever having needed to use it quite so much before!)
Contrary to what might be expected (given that `√` is normally a floating-point operation), `√ℕ` [does correctly work on arbitrarily large integers](https://tio.run/##SypKTM6ozMlPN/r/39DI2MTUzNzC0gA/y/Hw9EdtMx0f7up81NT0qGPWo5apEPb//wA) without having to worry about floating-point overflow and without having to worry about non-integral floats being approximated to integers (there's a special case in the Brachylog interpreter).
[Answer]
# x86 32-bit machine code, 9 bytes
```
A8 03 75 04 0F BD C0 48 C3
```
[Try it online!](https://tio.run/##XZDBTsMwDIbPyVOYIrRk7dDYBIdt5cKZCyckhqosTdugNpvSFFKmvTrFGQMBFyf@HX/@YzkppRyGc21k3eUKVq3L9fayuqV/pFpv/mtWmzJoNMuEw2zTOZVljFlV7oRt2BXnHLRxULAQBV9S0TbA4CFitFgQp1pU6wTmlLyYd1CUbFoLSvgkBEpyJY8XtSBWOcojQAQNrEZo8wW1pUxAVsLCeIzJK6d7SkLFQwrTBPpwLCl5q3StgMWxh1UKs@sbTgnZ4RdcwaILDVGCNj3HAT/q2twLWWmjQG5ztYhCzZ9w@Rb23@/gYjp7xP4@ZawzrS6Nyo@WxrzgTz6On/nyACcDPZwhwd/Nf08Chg42PS6Erw2S/LHYuQBho7UZhRxX0FkTph/oMHzIohZlO0ya@QwDLjZFlqo/AQ "C (gcc) – Try It Online")
Following the `regparm(1)` calling convention, this takes a 1-index in EAX and returns a number in EAX.
This produces the sequence 1, 2, 3, **1**, 5, 6, 7, **2**, 9, 10, 11, **2**, 13, 14, 15, **3**, 17, 18, 19, **3**, 21, 22, 23, **3**, 25, 26, 27, **3**, ..., with each *n* appearing 2*n*-1 times among the bolded terms, and if not divisible by 4, appearing one more time among the unbolded terms.
In assembly:
```
f:  test al, 3      # Check the low two bits of the number.
    jnz e           # If they're not both zero, jump to the end to return the number unchanged.
    bsr eax, eax    # Find the highest position of a 1 bit in the number...
    dec eax         # and decrease that by 1.
e:  ret             # Return.
```
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), ~~59~~ 8 bytes
```
.*0$
$.&
```
[Try it online!](https://tio.run/##FcsxEkAwFEXR/q4DYxSZ/34SkRXYBoVCozD2H5z@3MdzXnvrx3VrYbKOLgytCSeSyMwUFioyJOQoooQymlFBC6q44d9xPOIJz9S/fMf0Ag "Retina 0.8.2 – Try It Online") Link includes test cases. Outputs the nᵗʰ term of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 21, 22, 23, 24, 25, 26, 27, 28, 29, 2... where n appears ⌊9∙10ⁿ⁻²⌋ times if n is a multiple of 10 and an additional time if it is not. Explanation:
```
.*0$
```
Match any multiple of 10.
```
$.&
```
Replace it with its length.
It might even be possible to remove the `$` but then I'm not sure what behaviour the sequence would have.
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 14 bytes
```
ɾ:ẋfṖλ2lvsÞu;c
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCLJvjrhuotm4bmWzrsybHZzw551O2MiLCIiLCIzIl0=)
Takes an integer as input. Takes a long time to calculate for larger integers. (Each element \$x\$ appears \$x\$ times.)
## Explanation
```
ɾ               # Make range from input [1,2,...,r]
 :ẋ             # Repeat each element x times [[1],[2,2],[3,3,3],...]
   f            # Flatten [1,2,2,3,3,3,...,r]
    Ṗ           # Get all permutations of input
     λ          # Opening a lambda function
      2l        # Get all adjacent pairs of items
        vs      # Sort each pair (so, e.g. (1,2) and (2,1) become the same)
          Þu    # Check if there are any duplicates
            ;c  # Take first item from list of permutations which satisfies the lambda above 
```
[Answer]
# [Python](https://www.python.org), ~~52~~ 46 bytes
```
i=2
while[*map(print,(i,i+1,int(i**.4)))]:i+=2
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72soLIkIz9vwYKlpSVpuhY39TJtjbjKMzJzUqO1chMLNAqKMvNKdDQydTK1DXWATI1MLS09E01NzVirTG1bI4guqGaYIQA)
* -6 thanks to loopy walt
[Answer]
# [JavaScript (V8)](https://v8.dev/), 30 bytes
```
for(n=.3;;)print(++n**(n%4)|0)
```
[Try it online!](https://tio.run/##y0osSyxOLsosKNEts/j/Py2/SCPPVs/Y2lqzoCgzr0RDWztPS0sjT9VEs8ZA8/9/AA "JavaScript (V8) – Try It Online")
A messier but also shorter version of @Arnauld's answer. Was meant as a golf but they suggested I post it separately.
[Answer]
# [bc](https://www.gnu.org/software/bc/), 28 bytes
```
while(1){++x;++x;sqrt(x)-1}
```
[Try it online!](https://tio.run/##S0r@/788IzMnVcNQs1pbu8IahIsLi0o0KjR1DWu5/v8HAA) ~ Corrected per comments.
~~[Try it online!](https://tio.run/##qyrO@J@UrKCrW5ZaVJyZn8eVmpyRr6D0vzwjMydVw1CzWlu7whqEiwuLSjQqNGv/K9UkJddkpCamKOhaWlr@BwA) Note, this calls `bc` via Zsh. TIO's `bc` interpreter seems to be having problems.~~
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 8 bytes
```
⎇﹪Ｎ⁹θＩＬθ
```
[Try it online!](https://tio.run/##S85ILErOT8z5/z@gKDOvRCMktSgvsahSwzc/pTQnX8Mzr6C0xK80Nym1SENTR8ESiAt1FJwTi0s0fFLz0ksyNAo1gcD6/39DY9P/umU5AA "Charcoal – Try It Online") Link is to verbose version of code. Outputs the nᵗʰ term of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 1, 10, 11, 12, 13, 14, 15, 16, 17, 2, 19, 20, 21, 22, 23, 24, 25, 26, 2, 28, 29, 30... where n appears 10ⁿ⁻¹ times if n is a multiple of 9 and an additional time if it is not. Explanation:
```
  Ｎ         First input as a number
 ﹪          Modulo
   ⁹        Literal integer `9`
⎇           If not divisible then
    θ       First input as a string else
       θ    First input as a string
      Ｌ     Take the length
     Ｉ      Cast to string
            Implicitly print
```
[Answer]
# TI-Basic, 25 bytes
```
Input N
N-int(N/3
If not(fPart(N/3
int(.5√(Ans
Disp Ans
```
Outputs the nth term (1-indexed) of the sequence used in [l4m2's JavaScript answer](https://codegolf.stackexchange.com/a/257407).
---
### 64 bytes
```
{3,1
While 1
Disp 1+2Ans(1),2+2Ans(1
For(I,1,dim(Ans
Disp Ans(I
End
Ans+1
If 1+2dim(Ans)=Ans(dim(Ans
augment(Ans,{dim(Ans
End
```
Outputs indefinitely. This is definitely not the simplest approach, but it uses an interesting sequence:
```
7, 8, 3, 1, 9, 10, 4, 2, 11, 12, 5, 3, 13, 14, 6, 4, 15, 16, 7, 5, 2, 17, 18, 8, 6, 3, 19, 20, 9, 7, 4...
```
When arranged like this, the pattern becomes apparent:
```
 7  8 |  3  1
 9 10 |  4  2
11 12 |  5  3
13 14 |  6  4
15 16 |  7  5  2
17 18 |  8  6  3
19 20 |  9  7  4
21 22 | 10  8  5
23 24 | 11  9  6
25 26 | 12 10  7  3
27 28 | 13 11  8  4
29 30 | 14 12  9  5
31 32 | 15 13 10  6
33 34 | 16 14 11  7
35 36 | 17 15 12  8
37 38 | 18 16 13  9  4
39 40 | 19 17 14 10  5
41 42 | 20 18 15 11  6
43 44 | 21 19 16 12  7
45 46 | 22 20 17 13  8
47 48 | 23 21 18 14  9
49 50 | 24 22 19 15 10
51 52 | 25 23 20 16 11  5
53 54 | 26 24 21 17 12  6
55 56 | 27 25 22 18 13  7
57 58 | 28 26 23 19 14  8
59 60 | 29 27 24 20 15  9
61 62 | 30 28 25 21 16 10
63 64 | 31 29 26 22 17 11
65 66 | 32 30 27 23 18 12...
```
For all natural numbers \$n\$, \$n\$ appears \$n\$ times if \$n < 3\$, \$n+1\$ times if \$2 < n < 7\$, and otherwise \$n+2\$ times.
[Answer]
# [JavaScript (V8)](https://v8.dev/), 32 bytes
```
for(n=0;;)print(n++%3?n:n**.4|0)
```
[Try it online!](https://tio.run/##y0osSyxOLsosKNEts/j/Py2/SCPP1sDaWrOgKDOvRCNPW1vV2D7PKk9LS8@kxkDz/38A "JavaScript (V8) – Try It Online")
### How?
This is similar (but not identical) to the sequence [used by The Thonnu](https://codegolf.stackexchange.com/a/257408/58563).
Two distinct sequences are interleaved. For any \$n\ge0\$, we use the sequence \$s\_0(n)=n+1\$ if \$n\not\equiv 0 \pmod 3\$ and the sequence \$s\_1(n)=\lfloor (n+1)^{2/5}\rfloor\$ if \$n\equiv 0 \pmod 3\$.
| \$n\$ | \$s\_0(n)\$ | \$s\_1(n)\$ |
| --- | --- | --- |
| 0 |  | 1 |
| 1 | 2 |  |
| 2 | 3 |  |
| 3 |  | 1 |
| 4 | 5 |  |
| 5 | 6 |  |
| 6 |  | 2 |
| 7 | 8 |  |
| 8 | 9 |  |
| 9 |  | 2 |
| 10 | 11 |  |
| 11 | 12 |  |
| 12 |  | 2 |
| 13 | 14 |  |
| 14 | 15 |  |
| 15 |  | 3 |
| ⋮ | ⋮ | ⋮ |
 
[Answer]
# [Zsh](https://www.zsh.org/), 39 bytes
```
while echo $[++i] $[++i] $[(i**.5-1)^0]
```
[Try it online!](https://tio.run/##qyrO@K@hWf2/PCMzJ1UhNTkjX0ElWls7MxZBaWRqaemZ6hpqxhnE/q@tKSlSiInJU4hR@A8A)
  ~~[42 bytes](https://tio.run/##qyrO@P@/PCMzJ1WhoCgzr0RBN0dBJVpbOzMWQWlkamkZ6JlqxhnE/v8PAA "Zsh – Try It Online")~~
Similar to the [@l4m2](https://codegolf.stackexchange.com/a/257407/15940) solution. Thanks to @pxeger for -2 bytes. Corrected per comment from @m90.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 5 bytes
A port of [ais523's Brachylog answer](https://codegolf.stackexchange.com/a/257442/53748) - do upvote that.
```
ẈÆ²¡Ṫ
```
A monadic Link that accepts \$n\$ and yields:
$$a(n) =
\begin{cases}
\lfloor n \log\_{10} \rfloor + 1 & \text{if $n$ is a perfect square} \\
n & \text{otherwise}
\end{cases}$$
**[Try it online!](https://tio.run/##AR8A4P9qZWxsef//4bqIw4bCssKh4bmq/8OH4oKs//8xMDI1//8xMDI1 "Jelly – Try It Online")**
### How?
A byte saved over the naive `DL$Æ²¡`.
```
ẈÆ²¡Ṫ - Link: integer, n
   ¡  - repeat...
 Æ²   - ...times: is square? (using integer only arithmetic) 
Ẉ     - ...action: length of each (implicit range [1..n], implicit cast to digits)
    Ṫ - tail (for the list this is the length of digits of n, otherwise it's just n)
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~7~~ ~~6~~ 5 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
∞TÅ€g
```
Slightly modified port of *@Neil*'s [Retina](https://codegolf.stackexchange.com/a/257420/52210) and [Charcoal](https://codegolf.stackexchange.com/a/257432/52210) answers, so make sure to upvote him as well!
Outputs the sequence `1,2,3,4,5,6,7,8,9,10,2,12,13,14,15,16,17,18,19,20,2,22,...`. A.k.a.:
$$a(n) =
\begin{cases}
\text{length}(n) & \text{if $(n-1)$ is divisible by 10} \\
n & \text{otherwise}
\end{cases}$$
[Try it online.](https://tio.run/##yy9OTMpM/f//Uce8kMOtj5rWpP//DwA)
**Explanation:**
```
∞      # Push the infinite positive sequence: [1,2,3,...]
 TÅ€   # Map every 10th item to:
       # (where the 0-based index is visible by 10)
    g  #  The length of the current integer
       # (after which the infinite sequence is output implicitly as result)
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 9 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
∞Ðtï.ι¦.ι
```
[Try it online!](https://tio.run/##yy9OTMpM/f//Uce8wxNKDq/XO7fz0DIg8f8/AA "05AB1E – Try It Online")
Port of AndrovT's Vyxal answer.
#### Explanation
```
∞          # Infinite list of positive integers
 Ð         # Triplicate
  tï       # Square root and floor
    .ι     # Interleave
      ¦    # Remove first item
       .ι  # Interleave
```
[Answer]
# [Scala](http://www.scala-lang.org/), 97 bytes
Golfed version. [Try it online!](https://tio.run/##K05OzEn8/z8/KSs1uUTBVyG1oiQ1L6VYwbGgoDq4pCg1MVchrSg/V8FAIS0fyEvO0Mi0tasuKMrMK8nJ08hNLMnQK8gv18jU0TPV1CvJ98wrsbPLVDXStIYpydQ21KzVrP3/HwA)
```
object M extends App{Stream from 0 foreach(i=>{println(math.pow(i,.5).toInt>>i%2);println(i+1)})}
```
]