Generate an aperiodic integer sequence

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?)


Python, 48 25 bytes

Volatility's version, using string operations:

f=lambda a:int(`2**a`[0])

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.


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]

Tags:

Math

Code Golf