Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Comprehensive Ruby Programming

You're reading from   Comprehensive Ruby Programming From beginner to confident programmer

Arrow left icon
Product type Paperback
Published in Jun 2017
Publisher Packt
ISBN-13 9781787280649
Length 330 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Jordan Hudgens Jordan Hudgens
Author Profile Icon Jordan Hudgens
Jordan Hudgens
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Introduction to the Ruby Programming Language 2. Ruby Variables FREE CHAPTER 3. Ruby Strings 4. Working with Numbers in Ruby 5. Ruby Methods 6. Ruby Iterators and Loops 7. Ruby Collections 8. Ruby Conditionals 9. Object-Oriented Programming in Ruby 10. Working with the Filesystem in Ruby 11. Error Handling in Ruby 12. Regular Expressions in Ruby 13. Searching with grep in Ruby 14. Ruby Gems 15. Ruby Metaprogramming 16. Ruby Web Frameworks 17. Working with APIs in Ruby 18. Ruby Algorithms 19. Machine Learning

How to code a Fibonacci digit counter

In this section, we are going to solve another fun math problem that asks us to solve this problem: what is the index of the first term in the Fibonacci sequence to contain 1,000 digits?

In case your college algebra is a little rusty, the Fibonacci sequence is a series where you add the next number to the previous number in that series. These numbers can get massive quickly because the addition to the previous value creates a mathematical snowball effect.

Though the problem sounds daunting, it can be solved easily in Ruby.

We are going to start by creating a method called fibonacci_digit_counter and define some variables:

def fibonacci_digit_counter
num1, num2, i = -1, 0, 1
end

Next, we will create a while loop inside of the fibonacci_digit_counter method and iterate over the digit value of i:

while i.to_s.length < 1000
num1 += 1
i,...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime