Compute OEIS A005434 The task is to compute OEIS A005434 as quickly as possible.

Consider a binary string S of length n.  Indexing from 1, we can determine if S[1..i+1] matches S[n-i..n] exactly for all i in order from 0 to n-1.      For example,

S = 01010


gives 

[Y, N, Y, N, Y].


This is because 0 matches 0, 01 does not match 10, 010 matches 010, 0101 does not match 1010  and finally 01010 matches itself.

Define f(n) to be the number of distinct arrays of Ys and Ns one gets when iterating over all 2^n different possible bit strings S of length n.

The observant will notice this question is a simpler variant of another recent question of mine. However, I expect that clever tricks can make this much faster and easier.

Task

For increasing n starting at 1, your code should output n, f(n).

Example answers

For n = 1..24, the correct answers are:

1, 2, 3, 4, 6, 8, 10, 13, 17, 21, 27, 30, 37, 47, 57, 62, 75, 87, 102, 116, 135, 155, 180, 194


Scoring

Your code should iterate up from n = 1 giving the answer for each n in turn. I will time the entire run, killing it after two  minutes.  

Your score is the highest n you get to in that time.

In the case of a tie, the first answer wins.  

Where will my code be tested?

I will run your code under Virtualbox in a Lubuntu guest VM (on my Windows 7 host).

My laptop has 8GB of RAM and an Intel i7 5600U@2.6 GHz (Broadwell) CPU with 2 cores and 4 threads. The instruction set includes SSE4.2, AVX, AVX2, FMA3 and TSX.

Leading entries per language


n = 599 in Rust bu Anders Kaseorg.
n= 30 in C by Grimy. Parallel version gets to 32 when run natively  in cygwin.
Rust, n ≈ 660



use std::collections::HashMap;
use std::iter::once;
use std::rc::Rc;

type Memo = HashMap&lt;(u32, u32, Rc&lt;Vec&lt;u32&gt;&gt;), u64&gt;;

fn f(memo: &amp;mut Memo, mut n: u32, p: u32, mut s: Rc&lt;Vec&lt;u32&gt;&gt;) -&gt; u64 {
    debug_assert!(p != 0);
    let d = n / p;
    debug_assert!(d &gt;= 1);
    let r = n - p * if d &gt;= 2 { d - 1 } else { 1 };

    let k = s.binary_search(&amp;(n - r + 1)).unwrap_or_else(|i| i);
    for &amp;i in &amp;s[..k] {
        if i % p != 0 {
            return 0;
        }
    }

    if d &gt;= 3 {
        let o = n - (p + r);
        n = p + r;
        s = Rc::new(s[k..].iter().map(|i| i - o).collect());
    } else if n == p {
        return 1;
    } else if k != 0 {
        s = Rc::new(s[k..].to_vec());
    }

    let query = (n, p, s);
    if let Some(&amp;c) = memo.get(&amp;query) {
        return c;
    }
    let (n, p, s) = query;

    let t = Rc::new(s.iter().map(|i| i - p).collect::&lt;Vec&lt;_&gt;&gt;());
    let c = if d &lt; 2 {
        (1..r + 1).map(|q| f(memo, r, q, t.clone())).sum()
    } else if r == p {
        (1..p + 1)
            .filter(|&amp;q| p % q != 0 || q == p)
            .map(|q| f(memo, r, q, t.clone()))
            .sum()
    } else {
        let t = match t.binary_search(&amp;p) {
            Ok(_) =&gt; t,
            Err(k) =&gt; {
                Rc::new(t[..k]
                            .iter()
                            .cloned()
                            .chain(once(p))
                            .chain(t[k..].iter().cloned())
                            .collect::&lt;Vec&lt;_&gt;&gt;())
            }
        };
        (1..t.first().unwrap() + 1)
            .filter(|&amp;q| p % q != 0 || q == p)
            .map(|q| f(memo, r, q, t.clone()))
            .sum()
    };
    memo.insert((n, p, s), c);
    c
}

fn main() {
    let mut memo = HashMap::new();
    let s = Rc::new(Vec::new());
    for n in 1.. {
        println!("{} {}",
                 n,
                 (1..n + 1)
                     .map(|p| f(&amp;mut memo, n, p, s.clone()))
                     .sum::&lt;u64&gt;());
    }
}


Try it online!

How it works

This is a memoized implementation of the recursive predicate Ξ given in Leo Guibas, “Periods in strings” (1981).  The function f(memo, n, p, s) finds the number of correlations of length n with smallest period p and also each of the periods in the set s.