Last active
October 1, 2016 00:07
-
-
Save itzbernoulli/20017b78ab4166cf09bb989af2ef1626 to your computer and use it in GitHub Desktop.
Prime number, array_multiplication and Cartesian planes
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
a = [2,4,5,6] | |
#product increment | |
times= 1 | |
#starting value | |
start = 0 | |
result = [] | |
#loop through the array to get the product | |
a.each{|i| times = times * i} | |
#loop through the array and divide the product by the elements of the array | |
a.each{|i| result<<times/i} | |
#output the result | |
puts result |
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
#This program takes in two arrays containing | |
#the x and y coordinates, the breadth i.e x axis and | |
#the length i.e y-axis | |
a=[2,1,3,3] | |
b=[3,3,4,3] | |
def array_intersection(a,b) | |
r1x1 = a[0] | |
r1y1 = a[1] | |
r1x2 = a[0] | |
r1y2= a[1] + a[2] | |
r1x3 = a[0] + a[3] | |
r1y3 = a[1] + a[2] | |
r1x4 = a[0] + a[3] | |
r1y4 = a[1] | |
puts "coordinates of First Rectangle" | |
puts "x1: #{r1x1} y1: #{r1y1}" | |
puts "x2: #{r1x2} y2: #{r1y2}" | |
puts "x3: #{r1x3} y3: #{r1y3}" | |
puts "x4: #{r1x4} y4: #{r1y4}" | |
r2x1 = b[0] | |
r2y1= b[1] | |
r2x2 = b[0] | |
r2y2 = b[1] + b[2] | |
r2x3 = b[0] + b[3] | |
r2y3= b[1] + b[2] | |
r2x4 = b[0] + b[3] | |
r2y4 = b[1] | |
puts "coordinates of Second Rectangle" | |
puts "x1: #{r2x1} y1: #{r2y1}" | |
puts "x2: #{r2x2} y2: #{r2y2}" | |
puts "x3: #{r2x3} y3: #{r2y3}" | |
puts "x4: #{r2x4} y4: #{r2y4}" | |
x_points = [r1x1,r1x4,r2x1,r2x4].sort! | |
y_points = [r1y1,r1y3,r2y1,r2y2].sort! | |
x= x_points - x_points.minmax | |
y= y_points - y_points.minmax | |
puts '' | |
puts "Intersection Points" | |
puts "x1: #{x[0]} , y1: #{y[0]}" | |
puts "x2: #{x[0]} , y2: #{y[1]}" | |
puts "x3: #{x[1]} , y3: #{y[0]}" | |
puts "x4: #{x[1]} , y4: #{y[1]}" | |
end | |
array_intersection(a,b) |
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
#this method calculates the mid-poit of the | |
#two arguments | |
def mid(x,y) | |
midpoint = (x + y)/2.0 | |
end | |
#this method uses the square-root method to get | |
#ths square-root of a number and loops through | |
#from 2 to the square-root to verify if it is a | |
#prime number or not | |
def prime(value,n) | |
check = true | |
#get the square-root | |
num = sqrt(value,n) | |
#roundup the number to an integer | |
n = num.round | |
start = 2 | |
#loop through and test if it is prime or not | |
while start <=n | |
if value % start == 0 | |
check = false | |
end | |
start =start + 1 | |
end | |
check | |
end | |
#this function gives thw square-root of a number | |
#using the trial method | |
def sqrt(n,d) | |
lower_range = 0 | |
higher_range = n | |
div = mid(higher_range,lower_range) | |
until (div * div) - n <= d and (div * div) -n >= 0 do | |
higher_range = div if (div * div) - n > d | |
lower_range = div if (div * div) - n < 0 | |
div =mid(lower_range,higher_range) | |
end | |
div | |
end | |
puts prime(7,0.0000001) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment