22
\$\begingroup\$

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 rules. You should assume natural numbers include 0. You may assume they don't, but it won't do you any help.

This is so the goal is to minimize the size of your source code as measured in bytes.

\$\endgroup\$
2
  • \$\begingroup\$ is the sequence 1,2,2,3,3,3,4,4,4,4,5,5,5,5,5... valid? every number n just appearing in order n times? \$\endgroup\$
    – TKoL
    Commented Feb 3, 2023 at 16:47
  • 4
    \$\begingroup\$ @TKoL but 3 occurs next to 3 two different times \$\endgroup\$
    – l4m2
    Commented Feb 3, 2023 at 16:52

17 Answers 17

10
\$\begingroup\$

JavaScript (V8), 37 bytes

for(i=0;;print(i**.5>>i%2))print(++i)

Try it online!

JavaScript (V8), 41 40 bytes

for(i=0;;i%2||print(i**.5>>1))print(++i)

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

\$\endgroup\$
2
  • \$\begingroup\$ "I don't know how it works" How did you arrive at it? \$\endgroup\$
    – Jonah
    Commented Feb 3, 2023 at 17:35
  • 3
    \$\begingroup\$ @Jonah Trying OP's seq and something wrong lead to this \$\endgroup\$
    – l4m2
    Commented Feb 3, 2023 at 18:29
9
\$\begingroup\$

Vyxal, 8 5 3 bytes

₅[L

Try it Online!

Modified Kevin Cruijssen's 05AB1E answer. Equivalent python code: lambda n: n if n%5 != 0 else len(str(n))

\$\endgroup\$
2
  • 1
    \$\begingroup\$ An actual use for that FizzBuzz builtin! \$\endgroup\$
    – Neil
    Commented Feb 7, 2023 at 9:59
  • \$\begingroup\$ noob here sorry, when i click the link, it just outputs 1. how do i see more of the sequence? \$\endgroup\$ Commented Sep 14, 2023 at 5:08
7
\$\begingroup\$

Brachylog (v2), 4 bytes

√ℕl|

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 . (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 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).

\$\endgroup\$
5
\$\begingroup\$

x86 32-bit machine code, 9 bytes

A8 03 75 04 0F BD C0 48 C3

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 2n-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.
\$\endgroup\$
5
\$\begingroup\$

Retina 0.8.2, 59 8 bytes

.*0$
$.&

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.

\$\endgroup\$
2
  • \$\begingroup\$ I think this is invalid because 1 and 3 are consecutive twice (and may other pairs also are). \$\endgroup\$
    – m90
    Commented Feb 4, 2023 at 16:23
  • \$\begingroup\$ @m90 Worse still, I hadn't even golfed it properly! Better now? \$\endgroup\$
    – Neil
    Commented Feb 4, 2023 at 18:41
4
\$\begingroup\$

Vyxal, 14 bytes

ɾ:ẋfṖλ2lvsÞu;c

Try it Online!

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 
\$\endgroup\$
1
  • 1
    \$\begingroup\$ So is your first output 3(⟨ 3 | 1 | 2 | 2 | 3 | 3 ⟩ with input 3) or 2? \$\endgroup\$
    – l4m2
    Commented Feb 3, 2023 at 17:22
4
\$\begingroup\$

Python, 52 46 bytes

i=2
while[*map(print,(i,i+1,int(i**.4)))]:i+=2

Attempt This Online!

  • -6 thanks to loopy walt
\$\endgroup\$
1
3
\$\begingroup\$

JavaScript (V8), 30 bytes

for(n=.3;;)print(++n**(n%4)|0)

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.

\$\endgroup\$
3
\$\begingroup\$

bc, 28 bytes

while(1){++x;++x;sqrt(x)-1}

Try it online! ~ Corrected per comments.

Try it online! Note, this calls bc via Zsh. TIO's bc interpreter seems to be having problems.

\$\endgroup\$
2
  • \$\begingroup\$ I think this is invalid because 1 and 2 are consecutive twice. \$\endgroup\$
    – m90
    Commented Feb 4, 2023 at 16:19
  • 1
    \$\begingroup\$ TIO's bc works fine, but bc's syntax requires a newline to execute a statement, see info '(bc)Statements'. So your program is 26 bytes \$\endgroup\$
    – c--
    Commented Feb 4, 2023 at 18:00
2
\$\begingroup\$

Charcoal, 8 bytes

⎇﹪N⁹θILθ

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:

  N         First input as a number
 ﹪          Modulo
   ⁹        Literal integer `9`
⎇           If not divisible then
    θ       First input as a string else
       θ    First input as a string
      L     Take the length
     I      Cast to string
            Implicitly print
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I think this is invalid because 1 and 2 are consecutive twice. \$\endgroup\$
    – m90
    Commented Feb 4, 2023 at 16:22
  • \$\begingroup\$ @m90 Whoops, I forgot that they can't be consecutive in reverse order either. But that's easily fixed. Better now? \$\endgroup\$
    – Neil
    Commented Feb 4, 2023 at 18:34
2
\$\begingroup\$

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.


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.

\$\endgroup\$
2
\$\begingroup\$

JavaScript (V8), 32 bytes

for(n=0;;)print(n++%3?n:n**.4|0)

Try it online!

How?

This is similar (but not identical) to the sequence used by The Thonnu.

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
\$\endgroup\$
5
  • \$\begingroup\$ 30 using more opportunistic changes to the sequence produced. (Pretty sure it's correct,but better double check.) \$\endgroup\$
    – loopy walt
    Commented Feb 5, 2023 at 0:05
  • \$\begingroup\$ @loopywalt This is probably rather easy to fix but \$1\$ and \$2\$ both appears twice. \$\endgroup\$
    – Arnauld
    Commented Feb 5, 2023 at 0:43
  • \$\begingroup\$ Oops. sorry. Good job I said double check... Changing the starting value to something smaller like .3 seems to fix that. \$\endgroup\$
    – loopy walt
    Commented Feb 5, 2023 at 0:53
  • \$\begingroup\$ @loopywalt Cool. Maybe you should post it as a new answer? \$\endgroup\$
    – Arnauld
    Commented Feb 5, 2023 at 1:03
  • \$\begingroup\$ OK, so will post an actual JavaScript answer. Makes me feel like a con man. \$\endgroup\$
    – loopy walt
    Commented Feb 5, 2023 at 7:33
2
\$\begingroup\$

Zsh, 39 bytes

while echo $[++i] $[++i] $[(i**.5-1)^0]

Try it online!   42 bytes

Similar to the @l4m2 solution. Thanks to @pxeger for -2 bytes. Corrected per comment from @m90.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can just write while print ... for -2 bytes \$\endgroup\$
    – pxeger
    Commented Feb 4, 2023 at 8:35
  • \$\begingroup\$ I think this is invalid because 1 and 2 are consecutive twice. \$\endgroup\$
    – m90
    Commented Feb 4, 2023 at 16:21
1
\$\begingroup\$

Jelly, 5 bytes

A port of ais523's Brachylog answer - 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!

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)
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 7 6 5 bytes

∞TÅ€g

Slightly modified port of @Neil's Retina and Charcoal 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.

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)
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 9 bytes

∞Ðtï.ι¦.ι

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
\$\endgroup\$
0
\$\begingroup\$

Scala, 97 bytes

Golfed version. Try it online!

object M extends App{Stream from 0 foreach(i=>{println(math.pow(i,.5).toInt>>i%2);println(i+1)})}
\$\endgroup\$

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