# Challenge 2 - Work out the Angle

**URL:** <https://forum.kx.com/t/challenge-2-work-out-the-angle/13817>\
**Category:** Community Support\
**Tags:** imported, kdb-and-q\
**Created:** [February 20, 2023, 12:00am UTC](https://forum.kx.com/t/challenge-2-work-out-the-angle/13817 "2023-02-20T00:00:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmcparland](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/mmcparland/32/54_2.png) [@mmcparland](https://forum.kx.com/u/mmcparland)\
**Post date:** [February 20, 2023, 12:00am UTC](https://forum.kx.com/t/challenge-2-work-out-the-angle/13817/1 "2023-02-20T00:00:00Z")

</div>

[https://learninghub.kx.com/forums/topic/challenge-2-work-out-the-angle](https://learninghub.kx.com/forums/topic/challenge-2-work-out-the-angle)

Thank you to everyone who interacted with my last challenge! The replies were greatly appreciated.

My next challenge is for the math lovers. The triangle below, ABC is a right-angled triangle.

M is the mid-point of the hypotenuse AC.

You are given the lengths AB and BC (which are integers).

The task is to work out the angle MBC.

&nbsp;

This is my solution:

```
// reading in 2 sides 
ab: "I"$read0 0 
bc: "I"$read0 0 

// finding the distance 
ac: sqrt((ab*ab)+(bc*bc)) 
bm: ac % 2.0 
mc: bm 

// equalizing the sides 
b: mc 
c: bm 
a: bc 

// where b=c 
// finding the angle in radian 
angle_b_radian: acos(a % (2*b)) 

// converting the radian to degree 
round:{"F"$.Q.f[y]x} 
angle_b_degree: round[((180 * angle_b_radian) % 3.141593);2] 
answer: "i"$angle_b_degree 
show angle_b_degree
```

I would love to see other approaches to this so if anyone has any comments or suggestions please let me know

---

<div class="post-metadata">

**Author:** ![roryk](https://avatars.discourse-cdn.com/v4/letter/r/ed655f/32.png) [@roryk](https://forum.kx.com/u/roryk)\
**Post date:** [March 19, 2024, 11:00am UTC](https://forum.kx.com/t/challenge-2-work-out-the-angle/13817/2 "2024-03-19T11:00:57Z")

</div>

You don’t need any of the midpoint calculation, the answer in radians is atan ab%bc

---

<div class="post-metadata">

**Author:** ![mmcparland](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/mmcparland/32/54_2.png) [@mmcparland](https://forum.kx.com/u/mmcparland)\
**Post date:** [March 19, 2024, 11:00am UTC](https://forum.kx.com/t/challenge-2-work-out-the-angle/13817/3 "2024-03-19T11:00:57Z")

</div>

&nbsp;

Along with 's help on how to convert to degrees, thanks for showing me a quicker way of solving it!

```
q)ab: 10 
q)bc: 10 
q)atan ab%bc 
0.7853982 
q)atan[ab%bc]*180%acos -1 
45f
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![mmcparland](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/mmcparland/32/54_2.png) [@mmcparland](https://forum.kx.com/u/mmcparland)\
**Post date:** [March 19, 2024, 11:00am UTC](https://forum.kx.com/t/challenge-2-work-out-the-angle/13817/4 "2024-03-19T11:00:57Z")

</div>

thanks so much for all the advice ! Now looking back at my own solution and your examples, I should have took advantage of using a function. I can see how using a lamba is more effective than reading in the values. I will most definitely take this on board in the future as I understand the fewer the lines of code the better debugged.
