Computing the First 100 Numbers in the Fibonacci Sequence in a Function (Swift)
My homework exercise for week 4 of the Devslopes Academy was to write a function in Swift that would calculate the first 100 numbers in the Fibonacci Sequence.
I’m not going to lie, I read the assignment and felt immediately overwhelmed. I’m not great at math and I had to look up what the Fibonacci Sequence was.
After doing a little reading I had flashbacks of drawing classes in college. The Fibonacci Sequence is everywhere in nature and I remember using it when drawing organic things like plants to help make the drawings more realistic.
Example: A flower with 5 petals vs. 6 is more true to nature and will look a little more natural to the eye.
After wrapping my head around what the Fibonacci Sequence was and watching the SciShow YouTube video on the topic, I felt pumped enough to start working on my solution in the Xcode Playground.
I had to do a couple of revisions because my first solution printed the number with scientific notation.
To convert the number out of scientific notation I used
print(String(format: “%.0f”, fibonacciNumber))
The “%f” format string means “a floating point number,” and “%.0f” would mean “a floating-point number with no digits after the decimal point”.
I found an article on Hacking With Swift that was extremely useful in explaining the concept of how to specify floating-point precision in a string should scientific notation ever creep into my dev life again.
A good two hours or so later, I feel okayish enough about my solution to submit it for the course. Fingers crossed!
Thanks for following along on my learning journey. :)