# Generate an aperiodic integer sequence

Your task is to build a piece of code that generates an aperiodic sequence of integers, either by outputting one every time it iterates, or by taking an integer n and returning the nth number in that sequence.

Now, the sequence 1, 2, 3, 4, 5, 6, ... is itself aperiodic, so I'm going to introduce a few limitations to the definition of "aperiodic" used in this problem to weed out the trivial cases:

• The sequence must not be periodic modulo any integer m > 1. So a sequence like the powers of 2 would repeat 2, 4, 8, 6 all the time modulo 10, and therefore not count. (It would also repeat 0 modulo 2.)

• The sequence must not become periodic after a certain number of operations - e.g. 1 2 4 8 6 2 4 8 6 ... is still considered periodic even though 1 never appears again.

• The generation code must only use integers. So a piece of code that calculated the nth digit of pi (without calculating it in some integer form first) would not be acceptable.

Now, obviously every sequence performed with limited computer memory will eventually either be periodic or run out of memory, but if you can prove that your function, given infinite memory and time, will never loop back to a previous state, then it is acceptable.

The shortest code that does the above wins.

• What about strings? Can we use strings or arrays of numbers? Sep 10, 2013 at 4:04
• Btw you can calculate the nth digit of pi using only integers. Sep 10, 2013 at 5:39
• I doubt anyone can beat my 2-character golfscript solution. Anyone fluent in APL? Sep 10, 2013 at 6:34
• @JanDvorak Burlesque - 2 bytes: gl. Does the same thing as your GolfScript solution. Sep 10, 2013 at 7:25
• You might be interested in my work on BiEntropy, which evaluates the order and disorder of a finite binary sequence. The sequence 1,2,3,4,5,6... is in fact periodic (ie the first differences 0,0,0,0,0....of the sequence are perfectly periodic). arxiv.org/abs/1305.0954 The paper includes an evaluation of the disorder of the prime number sequence and the digits of pi and e etc. Sep 21, 2013 at 21:10

# Golfscript, 2 characters

,


(stringify () the number and count (,) the digits)

# J, 3 characters

#q:


is the count (#) of the prime factors (q:) of its input.

The vector form involves more bookkeeping, but that's not our contestant:

#@q:L:0&.<


the first 40 terms are: 0 1 1 2 1 2 1 3 2 2 1 3 1 2 2 4 1 3 1 3 2 2 1 4 2 2 3 3 1 3 1 5 2 2 2 4 1 2 2 4

see more at OEIS

The sequence is 1 at every prime position, 2 at every prime multiple of a prime, 3 at every prime multiple of a product of two primes...

If you are not convinced this is aperiodic, here are plenty of other sequences:

##:


(the number of bits = floor of the base-2 logarithm)

+/#:


(count of 1-bits in the binary representation of the number). This (reduced under any modulus)

#b#:
+/b#:


(the same in base-b)

<.^.
>.^.
<.b^.
>.b^.


(floor or ceiling of the (natural) log - this time without stringification)

*./#:
+./b#:
*./b#:


(GCD or LCM of all base-b digits (except GCD of bits won't work))

1{#:!
9{#:!
({#:@!)


(second/tenth/nth highest bit of the factorial of n)

(=<.&.%:)


(Is the number equal its floor under the square root operation? Is the number equal the square of the floor of its square root? Is the number a square?)

• I don't think anybody here needs convincing that the pattern of primes is aperiodic. Sep 10, 2013 at 6:30
• @breadbox the pattern of primes is aperiodic, but what about the pattern of numbers that have an odd number of prime power divisors? Sep 10, 2013 at 6:30
• For the J part of the solution, why not just use p: (the nth prime)? After all, you said yourself that the pattern of primes is aperiodic. Sep 10, 2013 at 7:21
• @Volatility the sequence of primes is periodic modulo 2 as all primes are odd. If rules 1+2 are not meant to be taken together, then a simple factorial will do (as noted by Peter Taylor). -:<:p: would likely be a valid sequence, but it's a bit longer. Sep 10, 2013 at 7:25
• If rules 1 and 2 aren't taken together then the primes do work, because the first one is even. Even if they are, the prime indicator function works, although that seems to be 4 characters in J: 1 p:. Sep 10, 2013 at 8:27

# Python, 48 25 bytes

Volatility's version, using string operations:

f=lambda a:int(2**a)


Numerical version (48 bytes):

def f(a):
m=2**a
while m>9:
m/=10
return m


Sequence it returns (first 100 terms):

1 2 4 8 1 3 6 1 2 5 1 2 4 8 1 3 6 1 2 5 1 2 4 8 1 3 6 1 2 5 1 2 4 8 1 3 6 1 2 5
1 2 4 8 1 3 7 1 2 5 1 2 4 9 1 3 7 1 2 5 1 2 4 9 1 3 7 1 2 5 1 2 4 9 1 3 7 1 3 6
1 2 4 9 1 3 7 1 3 6 1 2 4 9 1 3 7 1 3 6


This returns the first digit of the ath power of 2, which is aperiodic because log10 2 is irrational, and no multiple of it besides 0 will be an integer. (So while it may look periodic for a long time, eventually there will always be something to break the period.)

I could probably golf for whitespace, but I'm not experienced with that.

• Why not just f=lambda a:int(2**a)? Sep 10, 2013 at 4:53
• @Volatility Good question. I didn't think of using string operations. Sep 10, 2013 at 4:54
• Why do people assign their lambdas to variables? Mar 9, 2016 at 19:34
• @CatsAreFluffy Unnamed lambdas weren't always allowed.
– user45941
Mar 10, 2016 at 7:52

# Python, 14

int.bit_length


This generates the following sequence:

>>> f=int.bit_length
>>> [f(i) for i in range(20)]
[0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5]

• lambda x:x Mar 9, 2016 at 20:54

# RETURN, 9 bytes (noncompetitive)

0[' ,1+\$¦{\3}§.1!]!


Try it here.

Make sure to run using "timed run," because this is an infinite stream. Also replace \3 with character U+0003.

# Explanation

Basically just outputs each number reversed. I couldn't find any periodicity, but please tell me if you do.