Created
July 30, 2013 18:41
-
-
Save rkjha/6115607 to your computer and use it in GitHub Desktop.
silly_methods
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
# silly_sum | |
def silly_sum numbers | |
sum = 0 | |
numbers.each_with_index {|number, index| | |
sum = sum + number*index | |
} | |
puts "sum = #{sum}" | |
end | |
puts "### silly_sum ###" | |
silly_sum([2]) | |
silly_sum([2, 3]) | |
silly_sum([2, 3, 5]) | |
silly_sum([2, 3, 5, 7]) | |
#num_squares | |
def num_squares max | |
number_of_squares = 0 | |
for num in 1..max-1 | |
square_root = Math::sqrt(num) | |
if(num == square_root.floor**2) | |
number_of_squares += 1 | |
end | |
end | |
puts "number of square below #{max} = #{number_of_squares}" | |
end | |
puts "### num squares ###" | |
num_squares(5) | |
num_squares(10) | |
num_squares(25) | |
#silly_nums | |
def silly_nums max | |
num_array = [] | |
for num in 1..max-1 | |
if num%3 == 0 | |
num_array.push(num) unless num%5 == 0 | |
elsif num%5 == 0 | |
num_array.push(num) | |
end | |
end | |
puts num_array.inspect | |
end | |
puts "### silly nums ###" | |
silly_nums(3) | |
silly_nums(10) | |
silly_nums(20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment