> ## Documentation Index
> Fetch the complete documentation index at: https://docs.rhymeq.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Fibonacci Series: Marking Fibonacci Values with Phase

In the previous tutorial, we marked the values that belong to the **Cantor Set** using a phase change.

This time, let's do something similar with **Fibonacci numbers**.

Instead of building a pattern, we will start with all the values and mark the ones that are Fibonacci numbers.

We will use the same idea as before: a **phase change**.

## Finding the Fibonacci values

Let's first think about the Fibonacci sequence.

Each number is made by adding the two previous numbers.

Here, we start with `2` and `3`:

```text theme={null}
2, 3, 5, 8, 13, 21, ...
```

So we need a small function that can answer one question: *Is this value a Fibonacci number?*

We can write this function as:

```python theme={null}
def is_fib(x):
    a, b = 2, 3

    while a <= x:
        if a == x:
            return True
        a, b = b, a + b

    return False
```

Let's see what happens inside it.

We start with:

```text theme={null}
a = 2
b = 3
```

We compare `a` with `x`. If they are equal, we have found the Fibonacci number we are looking for, so we return `True`.

Otherwise, this line moves us to the next pair of Fibonacci numbers:

```python theme={null}
a, b = b, a + b
```

So we move through the sequence:

```text theme={null}
2, 3
3, 5
5, 8
8, 13
...
```

Each new number comes from adding the previous two.

We keep going until `a` is equal to `x` or becomes larger than it. If it becomes larger without finding `x`, we return `False`.

For example:

```text theme={null}
is_fib(8)   → True
is_fib(10)  → False
is_fib(13)  → True
```

Now we have a simple way to recognize the Fibonacci values.

## Marking the Fibonacci values

Next, we need to decide which phase each value should receive.

We can write another small Python function for it:

```python theme={null}
def mark_fib(x):
    return math.pi if is_fib(x) else 0.0
```

This gives us a very simple rule:

```text theme={null}
Fibonacci value  →  phase π

Other value      →  phase 0
```

Why use `π`? Because a phase change of `π` reverses the direction of an amplitude.

So just like in the Cantor Set example, we can think of it as putting a small **minus sign** on the values we want to mark.

We are not removing the other values. We are simply giving the Fibonacci values a different phase.

## Starting with the values

Now let's create our values.

We use:

```python theme={null}
u = quint.all(size=4)
```

A `quint` with `size=4` gives us:

```text theme={null}
0, 1, 2, ..., 15
```

All the values in `u` have equal amplitudes, as shown in the debugger:

![Debugger showing the values before applying the phase change](https://mintcdn.com/rhyme-q/JlURJlNdnA85HOaP/images/basic-tutorials/fibonacci_part1.png?fit=max\&auto=format\&n=JlURJlNdnA85HOaP\&q=85\&s=96bb37f89164f3c21d8e3f60c1c865c4)

So our Fibonacci values in this range are:

```text theme={null}
2, 3, 5, 8, 13
```

Now we have all the values we need.

We just need to mark the Fibonacci ones.

## Applying the phase

Let's apply the phase to the Fibonacci values by giving our `mark_fib()` function to `add_phase()`:

```python theme={null}
u.add_phase(mark_fib)
```

This tells Rhyme to use `mark_fib()` as the phase function.

For each value, the function checks whether it is a Fibonacci number.

If it is a Fibonacci number, the value gets phase `π`. Otherwise, it gets phase `0`.

So the idea is:

```text theme={null}
Start with all values
        ↓
Check each value
        ↓
Is it a Fibonacci number?
     ↓             ↓
    yes            no
     ↓             ↓
 phase π         phase 0
```

For our `quint` with `size=4`, the result is shown in the debugger:

![Debugger showing the values after applying the phase change](https://mintcdn.com/rhyme-q/JlURJlNdnA85HOaP/images/basic-tutorials/fibonacci_phase.png?fit=max\&auto=format\&n=JlURJlNdnA85HOaP\&q=85\&s=cbdd9426e7afacf49ac25a35e13378b4)

The Fibonacci values are now **marked by phase**.

## What changed?

We started with all the values:

```text theme={null}
0, 1, 2, 3, ..., 15
```

Then `is_fib()` told us which ones are Fibonacci numbers.

`mark_fib()` turned that answer into a phase:

```text theme={null}
Fibonacci  →  π
Other      →  0
```

Finally, `add_phase()` applied that rule.

We did not build the Fibonacci sequence inside the `quint`.

Instead, we used the sequence to **recognize and mark** particular values.

And just like with the Cantor Set, the important part is the phase change.

**That's Fibonacci marking with phase in Rhyme!**


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.