Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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.

share|improve this question
    
What about strings? Can we use strings or arrays of numbers? –  Jan Dvorak Sep 10 '13 at 4:04
    
I assume we cannot use randomness lest it would be too easy? –  Jan Dvorak Sep 10 '13 at 4:26
7  
Btw you can calculate the nth digit of pi using only integers. –  Howard Sep 10 '13 at 5:39
1  
I doubt anyone can beat my 2-character golfscript solution. Anyone fluent in APL? –  Jan Dvorak Sep 10 '13 at 6:34
2  
@JanDvorak Burlesque - 2 bytes: gl. Does the same thing as your GolfScript solution. –  primo Sep 10 '13 at 7:25

6 Answers 6

up vote 16 down vote accepted

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

share|improve this answer
    
I don't think anybody here needs convincing that the pattern of primes is aperiodic. –  breadbox Sep 10 '13 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? –  Jan Dvorak Sep 10 '13 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. –  Volatility Sep 10 '13 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. –  Jan Dvorak Sep 10 '13 at 7:25
2  
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:. –  Peter Taylor Sep 10 '13 at 8:27

Perl -p, 6 characters

$_.=/1/

Takes an integer n on the standard input and outputs the nth number in the following sequence:

11 2 3 4 5 6 7 8 9 101 111 121 131 141 151 161 171 181 191 20 211 22 23 24 25 26...
share|improve this answer

Sage, 7

Sage has the integer-part-of-the-square-root function built-in :

f=isqrt

[f(n) for n in [0..20]]
->  [0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4]

A number of OEIS sequences are also Sage built-ins named using the OEIS index, and would score a length of 16; e.g.,

f=sloane.A000120

f;[f(n) for n in [0..20]
->  1's-counting sequence: number of 1's in binary expansion of n.
    [0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2]
share|improve this answer

Python, 16

f=int.bit_length

This generates the following sequence:

>>> [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]
share|improve this answer

Perl, 19 18 characters

Simplicity itself:

sub f{"@_"=~/(.)/}

Given an integer n, the function f returns the nth number in the sequence.

For those not conversant in Perl, f returns the leading decimal digit in n. As the sequence continues, you get progressively longer runs of the current number. Thus the sequence is transparently aperiodic, and avoids devolving into any of the trivial cases listed above.

share|improve this answer
    
+1 for simplicity. $_[0] may be replaced with "@_" for one byte. –  primo Sep 10 '13 at 7:13
    
Good one, thanks. –  breadbox Sep 10 '13 at 8:05

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.

share|improve this answer
    
Why not just f=lambda a:int(`2**a`[0])? –  Volatility Sep 10 '13 at 4:53
    
@Volatility Good question. I didn't think of using string operations. –  Joe Z. Sep 10 '13 at 4:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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