Skip to content

Instantly share code, notes, and snippets.

@ugofred
Last active October 1, 2016 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ugofred/e54e86662b446b32c8c9d92c5be9021e to your computer and use it in GitHub Desktop.
Save ugofred/e54e86662b446b32c8c9d92c5be9021e to your computer and use it in GitHub Desktop.
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