Exemplo n.º 1
0
def is_hexagonal_number(n):
    '''
    tests whether number is a hexagonal number using quadratic formula
    problem 45
    '''
    roots = solve_quadratic(2,-1,-n)
    return True if [r for r in roots if r > 0 and r == int(r)] else False
Exemplo n.º 2
0
def is_triangle_number(n):
    '''
    tests whether number is a triangle number using quadratic formula
    problem 45
    '''
    roots = solve_quadratic(1,1,-2*n)
    return True if [r for r in roots if r > 0 and r == int(r)] else False
Exemplo n.º 3
0
 def has_triple(p):
     m_max = int(p**.5)
     m_min = int(max(solve_quadratic(2,-1,-p)))
     for m in range(m_min,m_max+1):
         for n in range(1 if m % 2 == 0 else 2,m,2):
             if gcd(m,n) > 1: continue
             if m*(m+n) == p: return 1
     return 0
Exemplo n.º 4
0
    def intersect(self, ray_direction, ray_origin):
        '''Contruct and solve a quadratic equation to find the intersection of the given ray and this sphere'''
        a = ray_direction.length**2

        to_sphere = ray_origin - self.position
        b = (ray_direction*to_sphere).product*2

        c = (to_sphere.length**2) - (self.radius**2)

        ts = solve_quadratic(a, b, c)
        
        return ts
Exemplo n.º 5
0
def points_of_reflection(a_x = 0,a_y = 10.1,b_x=1.4,b_y=-9.6):
    '''
    problem 144
    '''
    points = []
    while b_x < -0.01 or b_x > 0.01 or b_y < 0:
        ab_slope = (b_y - a_y)/(b_x - a_x) #slope of incoming line AB
        points.append((b_x,b_y)) #current reflection at bx,by
        tang_slope = -4*b_x/b_y #slope of tang at bx,by

        #find bc slope (angle of reflection)
        trig_ident = (ab_slope - tang_slope)/(1+ab_slope*tang_slope)
        bc_slope = (tang_slope - trig_ident) / (1+trig_ident*tang_slope) #trig identities (slope = tan) gives us outgoing bc_slope

        #use bc_slope to find new points cx,cy
        bc_intercept = b_y-(bc_slope*b_x) #use known points on BC (bx,by) and slope BC to solve b = y-mx
        new_xs = solve_quadratic(4+bc_slope*bc_slope,2*bc_intercept*bc_slope,bc_intercept*bc_intercept-100) #find intersection by plugging values into equation for ellipse
        c_x = new_xs[0] if abs(b_x-new_xs[0]) > abs(b_x-new_xs[1]) else new_xs[1] #pick value which is not current one

        #move everything along (b_y = mx+b or b_slope*c_x+intercept)
        a_x,a_y,b_x,b_y = b_x,b_y,c_x,bc_slope*c_x + bc_intercept
    return points