---
title: "Tech Tip #72: Memoise expensive recursion"
description: "Naive recursion can repeat the same calculation millions of times."
url: https://greencode.ai/tech-tips/72-memoise-expensive-recursion/
---

# Tech Tip #72: Memoise expensive recursion

**Naive recursion can repeat the same calculation millions of times.**

### The problem

A recursive function that calls itself on overlapping sub-problems recomputes the same answers again and again. The textbook example is Fibonacci; real code does it with tree walks, pricing rules and path searches.

### The fix you can use today

- Cache results of pure functions: functools.lru\_cache in Python, a map elsewhere.
- Or turn the recursion into a bottom-up loop.
- Only memoise pure functions whose inputs repeat.

A study found that storing results of repeated calls can cut energy by up to 74%. GreenCode's energy rules for AI coding agents cover this too.

### Tools that help

- [GreenCode Constitution skill](https://greencode-constitution.org/?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode) (GreenCode researchers): open-source energy principles and a catalogue of 37 energy anti-patterns that an AI coding agent can follow to find and fix waste like this
- [green-languages](https://github.com/PLEnergyDev/green-languages?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode) (GreenCode researchers): a command-line tool that measures the energy of whole programs or marked sections of code, across languages

### How GreenCode helps

GreenCode helps tackle this by refactoring flagged code one element at a time: it writes any missing tests and documentation first, and returns every change as a pull request for a person to approve. [More on this](https://greencode.ai/feature/automated-software-maintenance/).

### Tags

#algorithms#caching#python

### Sources

- [GreenCode Constitution skill, energy anti-patterns: C6 unmemoized expensive recursion (Tier 1)](https://greencode-constitution.org/skill.md?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode)
- [GreenCode State of the Art Review, D2.1 (2025), PDF p. 119: memoisation showed energy reductions of up to 74% in certain cases (Wysocki et al., Electronics 14(7), 2025)](https://greencode.ai/publications/d2-1-state-of-the-art-review/)

[All GreenCode Tech Tips](https://greencode.ai/tech-tips/)

#### Key Facts

- Tip **#72**
- For **Developers**
- Category **Code efficiency**
- Format **short video**
![GreenCode](https://greencode.ai/wp-content/uploads/2024/12/greencode-logo-website-small-colours-2.webp)

Want this checked on your own system? Talk to the GreenCode team.

[Get in touch](https://greencode.ai/contact/)
