Code Logo

Fern Spiral Growth

Published at05 Jan 2026
Array Manipulation Easy 4 views
Like10

Imagine a tiny fern that grows in a curling spiral. At the start, it has just one little frond. Then each new turn of growth makes the shape bigger by building from what was already there.

Your job is to figure out how many glowing fern pieces exist after a certain number of turns. This is a recursive pattern problem, which means each new step depends on the earlier one. Instead of starting from nothing every time, the fern keeps using its old shape and growing from it.

For example, when turns = 0, the answer is 1 because only the center frond exists. When turns = 2, the answer is 7. By the time you reach turns = 4, the fern has grown a lot more and reaches 31.

This problem is really about noticing how the pattern grows from one step to the next. Once you understand how one turn leads to the next turn, the numbers stop feeling random and start feeling like a growing story.

Example Input & Output

Example 1
Input
turns = 2
Output
7
Explanation

The second turn adds one frond and mirrors the earlier growth twice.

Example 2
Input
turns = 0
Output
1
Explanation

Only the central frond has opened.

Example 3
Input
turns = 4
Output
31
Explanation

Four turns preserve the rule, leaving thirty-one fronds glowing in moonlight.

Algorithm Flow

Recommendation Algorithm Flow for Fern Spiral Growth

Solution Approach

This problem follows a doubling growth pattern where each turn builds on the previous fern shape. The totals grow by powers of two, which lets us solve it with a simple closed-form formula.

The example values 1, 3, 7, 15, 31 for turns 0 through 4 match the pattern 2^(n + 1) - 1. This is the sum of the geometric series 1 + 2 + 4 + ... + 2^n.

Here is the implementation:

function calculate_fern_height(turns) {
    return Math.pow(2, turns + 1) - 1;
}

The formula computes 2^(turns + 1) - 1. Because the sum 1 + 2 + 4 + ... + 2^turns equals 2^(turns + 1) - 1, this gives the total number of glowing fern pieces directly.

Let us verify with turns = 2. The formula gives 2^3 - 1 = 7, matching the expected answer. For turns = 4, we get 2^5 - 1 = 31. And for turns = 0, we get 2 - 1 = 1, the single center frond.

Because it is a single expression, the time complexity is O(1) and the space complexity is O(1).

Best Answers

java
class Solution {
    public int calculate_fern_height(int n) {
        return (1 << (n + 1)) - 1;
    }
}