5 kyu

Hofstadter's Figure-Figure sequence

Description:

In his book, Gödel, Escher, Bach, Douglas Hofstadter describes a sequence of numbers which begins:

1, 3, 7, 12, 18, 26, 35, 45, 56, 69, 83, 98 ..  (sequence A)

It has the following property:

The sequence, together with its sequence of first differences (the differences between successive terms), lists all positive integers exactly once.

To illustrate this property, write down the first differences of the sequence A:

2, 4, 5, 6, 8, 9, 10, 11, 13, 14, 15 .. (sequence B)

and note that together, the sequences A and B list all positive integers.

To calculate more terms we can first extend the sequence B:

2, 4, 5, 6, 8, 9, 10, 11, 13, 14, 15, 16, 17, 19 ..

(We omit 18 as it occurs already in the sequence A). Then we can extend the sequence A by using the rule that A(n) = A(n-1) + B(n-1):

1, 3, 7, 12, 18, 26, 35, 45, 56, 69, 83, 98, (98+16) ..

Write a function which will return the nth term of the sequence A.

For example:

hof 0 = 1
hof 1 = 3
hof 2 = 7

etc.

In the tests, values of n up to

100 000
1 000 000
1 000 000
1 000
1_000_000

will be tested.

Lists
Algorithms
Recursion
Performance

Stats:

CreatedNov 9, 2022
PublishedNov 9, 2022
Warriors Trained678
Total Skips25
Total Code Submissions1041
Total Times Completed106
Haskell Completions19
JavaScript Completions40
C# Completions21
λ Calculus Completions3
Python Completions36
Total Stars25
% of votes with a positive feedback rating95% of 31
Total "Very Satisfied" Votes28
Total "Somewhat Satisfied" Votes3
Total "Not Satisfied" Votes0
Total Rank Assessments7
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • Paul Robertson Avatar
  • JohanWiltink Avatar
  • hobovsky Avatar
  • mauro-1 Avatar
  • Kacarott Avatar
  • dfhwze Avatar
Ad