Created
July 15, 2013 18:38
-
-
Save DiegoSalazar/c34bd6a4c138e02a0589 to your computer and use it in GitHub Desktop.
exact squares
This file contains 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
# A program that takes any single number (max) and returns all the numbers below (max) that is an exact square | |
def exact_squares(max) | |
s = [] | |
while max > 0 | |
s << max if is_exact_square? max | |
max -= 1 | |
end | |
s | |
end | |
def is_exact_square?(max) | |
sqr = Math.sqrt max | |
sqr == sqr.to_i | |
end | |
p exact_squares(200) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
def prog(max)
results = .each do |x|
results << x if is_valid?(x)
end
results
end
def is_valid?(num)
((num % 5) == 0 || (num % 2) == 0) && ((num % 5) != 0 || (num % 2) != 0)
end