Code Logo

Fern Spiral Growth

Published atDate not available
Easy 0 views
Like0

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
Recommendation Algorithm Flow for Fern Spiral Growth

Best Answers

java
class Solution {
    public int calculate_fern_height(int n) {
        if (n == 0) return 0;
        if (n == 1) return 1;
        int[] dp = new int[n + 1];
        dp[1] = 1;
        for (int i = 2; i <= n; i++) {
            dp[i] = i + dp[i-2];
        }
        return dp[n];
    }
}