Skip to content

xtuc/Fibonacci-forth

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 

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.
: 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.

result

About

The Fibonacci sequence using Forth language with only two words

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages