A sequence of positive rational numbers comes from an infinite complete binary tree whose nodes are labeled by positive rationals.
- The root is labeled 1/1.
- The left child of a node labeled p/q is labeled p/(p+q).
- The right child of a node labeled p/q is labeled (p+q)/q.
The top of the tree looks like this:

The sequence F comes from a level order (breadth first) traversal of the tree, drawn as the light dashed line in the figure. That gives F(1)=1/1, F(2)=1/2, F(3)=2/1, F(4)=1/3, F(5)=3/2, F(6)=2/3, and so on.
Given p and q, find the value of n for which F(n)=p/q.