Last active
September 29, 2015 04:34
-
-
Save jubishop/6eadbc52107a92f51394 to your computer and use it in GitHub Desktop.
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 | |
# Print message to ask the user for a number | |
# "puts" is shorthand for "put string". | |
# It's how we output text to the user. | |
puts "What number shall we find factors for? " | |
# We want to get input from the user and then store that input | |
# in a "variable" so that we can access it again later. | |
# We've decided to name our variable "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 it's just text. | |
# But we actually need our variable to be a number for calculations, so adding | |
# '.to_i' instructs the program to 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 crash. | |
# A more advanced program would double check to make sure the input was an Integer | |
# so that it wouldn't crash unexpectedly. | |
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 } below will | |
# execute over and over. | |
# The variable inside the ||, which we've named "number_to_test", will change each time. | |
# the first time "number_to_test" will be 1, then 2, etc. all the way up to "number_to_factor" | |
# 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. | |
# The code from here until "end" won't execute unless our "if" statement is true. | |
# So we know this will only happen when number_to_test is a true factor. | |
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