Waste less time on Facebook — follow Brilliant.
×

Programming Challenge: Trigonometry

Hi! I have a challenge for the programmers out there. Using only sine, cosine, their addition and subtraction formulas, and the values \(\sin\left(0^\circ\right),\) \(\sin\left(30^\circ\right),\) \(\sin\left(90^\circ\right),\) \(\cos\left(0^\circ\right),\) \(\cos\left(60^\circ\right),\) and \(\cos\left(90^\circ\right),\) write a program that will find the value of all six of the trigonometric functions (where defined) to three decimal places for all \(\theta\in[0^\circ,360^\circ).\) Use any language you want, and post your code below. Good luck!

Note by Trevor B.
2 years, 10 months ago

No vote yet
1 vote

Comments

Sort by:

Top Newest

Nice problem! Here's my solution:

Language used: Python

Core formula used: sin((A+B)/2) = sin(A/2)cos(B/2) + cos(A/2)sin(B/2)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def sinToCos(sinTheta):
    '''
    sinTheta: sin(theta)
    Returns cos(theta) given sin(theta)
    (Assumes 0 <= theta <= 90)
    '''
    return (1 - sinTheta**2)**.5

def findSin(sinA, sinB):
    '''
    sinA: sin(A)
    sinB: sing(B)
    Returns value of sin((A+B)/2)
    given the value of sin(A) and sin(B)
    '''
    cosA, cosB = sinToCos(sinA), sinToCos(sinB)
    return ((((1-cosA)/2)**.5) * (((1+cosB)/2)**.5)) \
            + ((((1+cosA)/2)**.5) * (((1-cosB)/2)**.5))

sinDict = {0.0: 0.0, 30.0: 0.5, 90.0:1.0}
accuracy = 0.001
def sin(theta):
    '''
    Returns the desired value of sin(theta)
    within some accuracy: accuracy
    '''
    try:
        return sinDict[theta] # See's if theta is in sinDict

    except KeyError:
        if theta - 270 >= 0: 
            return -sinToCos(sin(theta - 270))
        elif theta - 180 >= 0:
            return -sin(theta - 180)
        elif theta - 90 >= 0:
            return sinToCos(sin(theta - 90))

        else:
            startAngle = 0 if theta < 30 else 30 # theta could be > 0 or > 30
            stopAngle = 30 if theta < 30 else 90 # theta could be < 30 or < 90
            curAngle = (startAngle + stopAngle)/2
            curAngle = round(curAngle, 5)
            sinDict[curAngle] = findSin(sinDict[startAngle], sinDict[stopAngle])

            while True:
                if abs(curAngle-theta) <= accuracy:
                    return sinDict[curAngle] # Returns sin(curAngle) if curAngle is close enought to theta
                else: # Tries to find another curAngle which is close enought to theta
                    if theta > curAngle:
                        startAngle = curAngle
                    else:
                        stopAngle = curAngle

                    curAngle = (startAngle + stopAngle)/2
                    curAngle = round(curAngle, 5)
                    sinDict[curAngle] = findSin(sinDict[startAngle], sinDict[stopAngle])
Lokesh Sharma · 2 years, 10 months ago

Log in to reply

@Lokesh Sharma Note: I have just included the implementation of sin(theta) as other 5 trigonometric functions can be evaluated given we have sin(theta). Lokesh Sharma · 2 years, 10 months ago

Log in to reply

I'm definitely doing this! Just kidding, I don't even know what a computer is. :D Finn Hulse · 2 years, 10 months ago

Log in to reply

Did you already create one , it seems that you already know it it. Can you post your code after some replies of codes from problems.May I ask you how are good at computer science while you said you started recently, what resources do you use.@Trevor B. Mardokay Mosazghi · 2 years, 10 months ago

Log in to reply

@Mardokay Mosazghi I might be able to do this if I tried, but I haven't yet. I'm way too busy nowadays, and this would take me a couple hours to do.

Codecademy and lots of Google searches is how I learned programming.

This is actually an area programming I haven't had experience with before, generating a bunch of data instead of finding one concrete answer. Additionally, I didn't learn how to make the box come up to input values and have the program pick a specific value stored. Trevor B. · 2 years, 10 months ago

Log in to reply

@Trevor B. Hi,

Although I actually don't know anything about programming till date. But I have become all the way curious about learning. Could you pls suggest me where to start from. ( literally from the basics) Thanks....! Anyways I have signed up for Codeacademy... Vishal Sharma · 2 years, 10 months ago

Log in to reply

@Trevor B. Thanks didn't know about codeacademy really helpful Mardokay Mosazghi · 2 years, 10 months ago

Log in to reply

It is not the matter of challenging. once i am calculating some value of trigonometry function oo calculator but suddenly i find this that sum of two angle that is sin or cos whose sum is 90 and diference is 90.are equal to 1 ( one). I want your comments can it be prove by right triangle method. All the best good night. Amar Nath · 2 years, 10 months ago

Log in to reply

You can easily achieve a logarithmic number of operations and constant memory: Consider the binary representation of \(\frac{\theta}{30}\). This uses the half angle formula, \(\tan\frac\theta2=\frac{\sin\theta}{\cos\theta+1}\), though. (Which is easily derivable from the basics). Only using addition and substraction in the code will never be useful enough, since we could ony represent linear intger combinations of the angles we are given, that is: Angles multiples of \(30^{\circ}\) . Luis Rivera · 2 years, 10 months ago

Log in to reply

python languagei have not learn. but the formula used is notcosine formula.i am sure. o. k Amar Nath · 2 years, 10 months ago

Log in to reply

×

Problem Loading...

Note Loading...

Set Loading...