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!

#Geometry #Trigonometry #ComputerScience #Triangles #Programming

Note by Trevor B.
7 years, 1 month ago

No vote yet
1 vote

  Easy Math Editor

This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.

When posting on Brilliant:

  • Use the emojis to react to an explanation, whether you're congratulating a job well done , or just really confused .
  • Ask specific questions about the challenge or the steps in somebody's explanation. Well-posed questions can add a lot to the discussion, but posting "I don't understand!" doesn't help anyone.
  • Try to contribute something new to the discussion, whether it is an extension, generalization or other idea related to the challenge.
  • Stay on topic — we're all here to learn more about math and science, not to hear about your favorite get-rich-quick scheme or current world events.

MarkdownAppears as
*italics* or _italics_ italics
**bold** or __bold__ bold

- bulleted
- list

  • bulleted
  • list

1. numbered
2. list

  1. numbered
  2. list
Note: you must add a full line of space before and after lists for them to show up correctly
paragraph 1

paragraph 2

paragraph 1

paragraph 2

[example link](https://brilliant.org)example link
> This is a quote
This is a quote
    # I indented these lines
    # 4 spaces, and now they show
    # up as a code block.

    print "hello world"
# I indented these lines
# 4 spaces, and now they show
# up as a code block.

print "hello world"
MathAppears as
Remember to wrap math in \( ... \) or \[ ... \] to ensure proper formatting.
2 \times 3 2×3 2 \times 3
2^{34} 234 2^{34}
a_{i-1} ai1 a_{i-1}
\frac{2}{3} 23 \frac{2}{3}
\sqrt{2} 2 \sqrt{2}
\sum_{i=1}^3 i=13 \sum_{i=1}^3
\sin \theta sinθ \sin \theta
\boxed{123} 123 \boxed{123}

Comments

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 - 7 years, 1 month ago

Log in to reply

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 - 7 years, 1 month ago

I'm definitely doing this! Just kidding, I don't even know what a computer is. :D

Finn Hulse - 7 years, 1 month ago

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 - 7 years, 1 month ago

Log in to reply

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. - 7 years, 1 month ago

Log in to reply

Thanks didn't know about codeacademy really helpful

Mardokay Mosazghi - 7 years, 1 month ago

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 - 7 years, 1 month ago

python languagei have not learn. but the formula used is notcosine formula.i am sure. o. k

amar nath - 7 years, 1 month ago

You can easily achieve a logarithmic number of operations and constant memory: Consider the binary representation of θ30\frac{\theta}{30}. This uses the half angle formula, tanθ2=sinθcosθ+1\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 3030^{\circ} .

Luis Rivera - 7 years, 1 month ago

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 - 7 years ago

I think there is a numerical algorithm that calculators use to do what you just said.

Krishna Karthik - 2 years, 7 months ago
×

Problem Loading...

Note Loading...

Set Loading...