Last active
October 2, 2016 07:27
-
-
Save johnotu/7305df363c42f003f8be1234f1ddb16d 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
# Determine if a number is a prime number | |
def square_root(n,d) | |
g1 = (n * 1.0) / 2 | |
g2 = (g1 + (n / g1)) / 2 | |
while (g1 - g2).abs >= d | |
g1 = g2 | |
g2 = (g1 + (n / g1)) / 2 | |
end | |
g2 | |
end | |
def is_prime? n | |
a = square_root n, 0.000001 | |
(2..a).each do |x| | |
return false if n % x == 0 | |
end | |
true | |
end | |
# Return an array of products exclusive of element | |
def check_zeroes arr | |
count = 0 | |
arr.each do |x| | |
count += 1 if x == 0 | |
end | |
count | |
end | |
def array_product arr | |
prd = 1 | |
arr.each do |x| | |
prd = prd * x if x != 0 | |
end | |
prd | |
end | |
def new_array_of_products arr | |
a = array_product arr | |
b = check_zeroes arr | |
new_array = [] | |
if b > 1 | |
arr.each {|x| new_array.push(0)} | |
else | |
arr.each do |x| | |
if x == 0 | |
new_array.push(a) | |
else | |
new_array.push(a/x) | |
end | |
end | |
end | |
puts new_array | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment