Blue host

Math in computer science


WHY MATH?


Learning those formulae and proofing of rules should not end in university. I mean math in computer science. Seldom have queried what these theories are all about when I was a naive learner. And now I understand why just because I had the chance to practice them. You also have the chance. Use it.






This is not about if Math demanded or its important in programming or coding or let's say in Artificial Intelligent? however it is about how significant do we need Math in coding/programming as computer science students. Below I had a Math puzzle to solve.


Math in computer science



In the first scene, the task appears simple, true? Most of you will assume that it is, yet to me, it wasn’t. My first method was to think in a logical clarification (at least I’d like to think that it was as that is part of science), then I assume that if I needed to know if the kangaroos will ever land on the same location at the same time, I would have to move the kangaroos until they were both on the same location



I designed a loop that would move the kangaroos and check if they are in the same position.



Forgetting other validations, I only wrote the necessary code for the case:



for i:=0 ; i<10000; i++ {
    kOnePosition = moveKangoro(x1, v1)
    kTwoPosition = moveKangoro(x2, v2)
    if kOnePosition == kTwoPosition {
        fmt.Println(YES)
        break;
    }
}


I wrote variants of this code and other ones, but not any of them passed the tests. After 10 or 20 minutes I imagined in a math solution as a computer science student.

The equation to verify if in n jumps the kangaroos are going to be on the same location is this:

x1 + (v1 * n) = x2 + (v2 * n) -> where n is of num of jumps.

So, we are going to resolve this equation:

(v1 * n) — (v2 * n) = x2 — x1

n(v1 — v2) = x2 — x1

n = (x2 — x1) / (v1 — v2)

We know that the value of n needs to be an integer, so we’re going to replace the division by the modular division and check if the operation leaves a remainder of 0

(x2 — x1) % (v1 — v2) == 0


This means that for a set of initial positions and meters per jump, we can know that the kangaroos are going to be in the same position at the same time only if the remainder value is 0.
Again, I’m omitting other validations, I only wrote the basic code for the case:

for i:=0 ; i<10000; i++ {
    kOnePosition = moveKangoro(x1, v1)
    kTwoPosition = moveKangoro(x2, v2)
    if kOnePosition == kTwoPosition {
        fmt.Println(YES)
        break;
    }
}



I wrote variants of this code and other ones, but neither of them passed the analyses. After 10 or 20 minutes I imagined in a math solution.



The equation to prove if in n jumps the kangaroos are going to be on the same location is this:



x1 + (v1 * n) = x2 + (v2 * n) -> where n is of num of jumps.



So, we will resolve this equation:



(v1 * n) — (v2 * n) = x2 — x1



n(v1 — v2) = x2 — x1


n = (x2 — x1) / (v1 — v2)

We understand that the value of n needs to be an integer, so will replace the division by the modular division(%) and verify if the operation leaves a remainder of 0

(x2 — x1) % (v1 — v2) == 0

This indicates that for a set of initial positions and meters per jump, we can apprehend that the kangaroos are going to be in the same position at the same time only if the remainder value is 0. Math is funny.

Again, I’m omitting other validations, I only wrote the basic code for the case:


if x1 == x2 && v1 == v2 {
       fmt.Println("YES")
   } else if x1 == x2 && v1 != v2{
       fmt.Println("NO")
   } else {
       if v1 > v2 && ((x2-x1)%(v1-v2)) == 0 {
           fmt.Println("YES")
       } else {
           fmt.Println("NO")
       }
   }



This algorithm passed all the tests and we could only do this efficiently with math.



Seldom I hear developers state “Math is not really relevant” or phrases like “A qualified coder is bad at math” and I hold that they are mistaken because as we can observe in this case, maths help us to solve a puzzle in an effective way.



I have got a lot to explore and It’s interesting to discover certain problems to develop each day.

Now you know what Math is in computer scienceBy the way, if you like this article, then please share it, subscribe to our mail and leave your valuable comment.

Post a Comment

0 Comments