exploring fibonacci in swift

Warren HansenalgorithmsLeave a Comment

I’ve been interested in Fibonacci ever since I became involved in the world of financial trading. I’ve written a few algos before that look for cycles in financial instruments but never applied fibs to them. Also known as the golden ratio this formula of repeating numbers has shown up in many different disciplines. Fibonacci examples are plentiful in geometry…

and music.

Fibs show up in too many places to mention here. In this edition of my blog,  I’m going to write a function that calculates all the fibs in a range of numbers in swift. In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-2

Concept: Create a list of Fibonacci numbers
Test: Find all of the Fibonacci numbers less than 100.

A solution I came up with in swift simple repeats the sequence of adding the last 2 numbers together.

Console Output: Fibs under 100 found are [1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.