Abstract
Executing computer programs described in natural language has long been a pursuit of computer science. With the advent of enhanced natural language understanding capabilities exhibited by large language models (LLMs), the path toward this goal has been illuminated. In this paper, we seek to examine the capacity of present-day LLMs to comprehend and execute algorithms outlined in natural language. We established an algorithm test set sourced from Introduction to Algorithms, a well-known textbook that contains many representative widely-used algorithms. To systematically assess LLMs’ code execution abilities, we selected 30 algorithms, generated 300 random-sampled instances in total, and evaluated whether popular LLMs can understand and execute these algorithms. Our findings reveal that LLMs, notably GPT-4, can effectively execute programs described in natural language, as long as no heavy numeric computation is involved. We believe our findings contribute to evaluating LLMs’ code execution abilities and would encourage further investigation and application for the computation power of LLMs.
Introduction
For decades, computer science has aimed to create machines capable of executing programs described in plain human language. While Large Language Models (LLMs) have demonstrated significant proficiency in code generation and reasoning, a systematic measurement of their ability to execute algorithms step-by-step—mimicking a CPU—has been lacking. To execute an arbitrary algorithm, a system must rigorously support fundamental control flows: sequence, selection (if/else), and iteration (loops). This investigation explores whether current LLMs can serve as effective "interpreters" for natural language programs, establishing a standardized benchmark to quantify this capability.
Methodology: Algorithm Prompting
The researchers developed a technique termed Algorithm Prompting. Rather than providing high-level instructions (e.g., "sort this list"), they supplied rigorous, unambiguous natural language descriptions of the algorithm's internal logic. Key components of this prompting strategy include:
- Reasoning-First Approach: Models are prompted to perform intermediate calculations and comparisons before stating a result (e.g., "Calculate 8 - 3 = 5, which is positive, so proceed...").
- Explicit State Management: Abstract variables are replaced with concrete values (e.g., using "i < 4" instead of "i < n") to minimize the cognitive load of memory lookups.
- Control Flow Triggers: "Goto" statements are used to signal loops clearly, and sequential indices act as delimiters for each computational step.
- Stopwords: The term "halt" is utilized to prevent models from hallucinating additional output following an error.
Experimental Setup
The study utilizes two primary evaluation sets derived from the classic textbook Introduction to Algorithms:
- CLRS-mini: Comprising 26 representative algorithms across sorting, searching, string matching, greedy strategies, dynamic programming, and graph theory.
- CLRS-Numeric: Featuring 4 algorithms intensive in floating-point mathematics, such as Discrete Fourier Transform and Least Square Regression.
Key Findings
LLMs as Virtual Machines
The most significant discovery is that GPT-4 can consistently maintain and update variable values through text output, effectively mimicking the core functions of a Von-Neumann machine: calculation, flow control, and variable storage.
Natural Language vs. Code Execution
Surprisingly, models performed significantly better when provided with detailed natural language instructions compared to equivalent Python code. As shown in the results, GPT-4's average accuracy dropped from 100% to 65.4% when transitioning from natural language prompts to raw code. This suggests that "uninterpreted" code may require an internal translation step that introduces errors.
The Arithmetic Bottleneck
While models excel at logic, they struggle with arithmetic. A 0% success rate on the CLRS-Numeric set indicates that LLMs currently lack a reliable "floating-point unit."
Detailed Results
Quantitative analysis highlights a substantial leap in capability from GPT-3.5 to GPT-4. While GPT-4 achieved 100% accuracy across the CLRS-mini suite, GPT-3.5-Turbo averaged only 35%, primarily due to context length limitations and failures in complex graph logic.
The authors also evaluated "Process Accuracy"—the frequency with which the model correctly executes intermediate steps before an error occurs. GPT-4 maintained 100% process accuracy for the tested subset, confirming its performance is based on systematic execution rather than heuristic guessing.
Limitations and Future Directions
Two primary hurdles remain:
- Context Window Limits: Complex algorithms or larger input sizes require a number of tokens for step-by-step execution that can exceed current context windows.
- Numerical Precision: Floating-point miscalculations can lead to "cascading errors," where a single incorrect digit compromises the entire remaining execution.
Future research may explore "hybrid" execution models where an LLM manages high-level logic and control flow while offloading numerical tasks to an external interpreter. Additionally, LLMs could be used to automatically compile high-level code into optimized natural language instructions.
Summary updated