Created
September 29, 2015 03:58
-
-
Save jubishop/d8fe7bd6118c6f630c7c to your computer and use it in GitHub Desktop.
Factors Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This program is a demo of how to find factors for numbers | |
# But it's highly inefficient. Can you think of how to make it faster? | |
# Print message to ask the user for a number | |
puts 'What number shall we find factors for? ' | |
# Get input from user and store in a variable we've decided | |
# to name number_to_factor. | |
# 'gets' is the name of the function that takes input from the user. | |
# By default 'gets' returns a variable that's called a "string" which means its just text | |
# But we actually need our variable to be a number for calculations, so adding | |
# '.to_i' instructs the program convert the input to an Integer. | |
# Integer is a fancy term for a "whole" number. That is a number with no decimals. | |
# If the user entered anything other than an Integer this program would break. | |
# A more advanced program would double check to make sure the input was correct... | |
number_to_factor = gets.to_i | |
# By putting number_to_factor inside a #{} we output it in our sentence below. | |
puts "Factors for #{number_to_factor} are:" | |
# now we will check every number from 1 up to number_to_factor to see if its a factor | |
1.upto(number_to_factor) { |number_to_test| | |
# everything inside this "block" between the opening { above and the closing } belowe will | |
# execute over and over, with the value in number_to_test changing each time | |
# the first time number_to_test will be 1, then 2, etc. all the way up to number_to_test | |
# the "%" operator tells you what the remainder would be if you divided two numbers. | |
# so 10 % 3 would be 1 | |
# or 17 % 5 would be 2 | |
# or 27 % 11 would be 5 | |
# so we know if the "%" of two numbers is 0, then the one divides into the other perfectly | |
# and we have a factor | |
if (number_to_factor % number_to_test == 0) | |
# == 0 is how we test if the result is 0 like we want, and if so we get inside this block | |
# if not, this code won't execute | |
# So since we now know number_to_test in this loop is a factor, we output it | |
puts "#{number_to_test}" | |
end | |
} | |
# That's it! | |
# This program works perfectly and will always give you the right answer. | |
# But it is extremely inefficient. | |
# Trying to make things more efficient is often an important element of Software Engineering, | |
# because you may sometimes have to deal with very large numbers or piles of data that even a | |
# computer could take days or months to fully analyze. | |
# Remember that the 1.upto(number_to_factor) loop above will check EVERY SINGLE NUMBER | |
# between 1 and the number the user input. So if the user enters | |
# 10 billion, the loop will run 10 billion times! | |
# Can you think of how to make it faster? | |
# Does the challenge of making this faster sound interesting to you? | |
# If so, I definitely recommend you consider software engineering. | |
# I won't reveal any answer here, but email me if you want to discuss it. | |
# [email protected] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment