https://github.com/xtuc/fibonacci-forth
The Fibonacci sequence using Forth language with only two words
https://github.com/xtuc/fibonacci-forth
Last synced: 6 months ago
JSON representation
The Fibonacci sequence using Forth language with only two words
- Host: GitHub
- URL: https://github.com/xtuc/fibonacci-forth
- Owner: xtuc
- License: mit
- Created: 2016-10-06T06:00:33.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-11-01T20:04:58.000Z (almost 10 years ago)
- Last Synced: 2025-10-08T00:38:20.880Z (10 months ago)
- Language: Forth
- Homepage:
- Size: 25.4 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Fibonacci-forth
The Fibonacci sequence using Forth language
I used GNU Forth (https://www.gnu.org/software/gforth).
## Explanations
Forth is a stack based language. All functions are called words.
* `2dup` word duplicates `n` and `n-1` elements and put it on the top of the stack
* `+` word takes `n` and `n-1` elements from the stack and sum them. The result goes on top of the stack.
```forth
: fib ( x1 x2 -- x1 x2 x3 )
2dup
+
;
```
The `fib` word sums the two last elements and put the result on the top of the stack, which is the Fibonacci sequence.
## Result
First number is the occurrence of the sequence and the second its result.
