Last active
October 1, 2016 13:57
-
-
Save ugofred/e54e86662b446b32c8c9d92c5be9021e to your computer and use it in GitHub Desktop.
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
def prime?(x) | |
[9] pry(main)* root = square(x, 0.0001) | |
[9] pry(main)* a = 2..(root) | |
[9] pry(main)* a.each do |i| | |
[9] pry(main)* return false if x % i == 0 | |
[9] pry(main)* end | |
[9] pry(main)* true | |
[9] pry(main)* end | |
=> :prime? | |
[10] pry(main)> prime?(5) | |
=> true | |
#question 2 | |
def pro_array(arr) | |
pro_a = arr.inject(1){ |product,i| i == 0 ? product * 1 : product * i } | |
new_arr = [] | |
if arr.count(0) == 1 | |
arr.each {|i| i == 0 ? new_arr.push(pro_a): new_arr.push(0)} | |
elsif arr.count(0) > 1 | |
arr.each {|i| new_arr.push(0)} | |
else | |
arr.each { |i| new_arr.push( pro_a / i) | |
} | |
end | |
new_arr | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment