DEV Community

Cover image for Viral Math Problems: 6 / 2 * (1 + 2)
Tony
Tony

Posted on

Viral Math Problems: 6 / 2 * (1 + 2)

Viral Math Problems: 6 / 2 * (1 + 2)

How do computers solve - 6 / 2 * (1 + 2) ?

The answer is 9.


Code & Repo for current solutions

C answer - 9

code:

#include "stdio.h"

int main() {
    int a = 6 / 2 * ( 1 + 2);
    printf("C answer - %d\n", a);

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Golang answer - 9

code:

package main

import "fmt"

func main() {
    a := 6 / 2 * (1 + 2)

    fmt.Printf("Golang answer - %d\n", a)
}

Enter fullscreen mode Exit fullscreen mode

Node.JS answer - 9

code:

const a = 6 / 2 * (1 + 2)
console.log(`Node.JS answer - ${a}`)
Enter fullscreen mode Exit fullscreen mode

Deno answer - 9

code:

const a = 6 / 2 * (1 + 2)
console.log(`Deno answer - ${a}`)
Enter fullscreen mode Exit fullscreen mode

Python2 answer - 9

code:

a = 6 / 2 * ( 1 + 2)

print("Python2 answer - " + str(a))
Enter fullscreen mode Exit fullscreen mode

Python3 answer - 9.0

code:

a = 6 / 2 * ( 1 + 2 )

print("Python3 answer - " + str(a))
Enter fullscreen mode Exit fullscreen mode

Ruby answer - 9

code:

a = 6 / 2 * (1 + 2)

puts "Ruby answer - #{a}"
Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
rsa profile image
Ranieri Althoff

I don't see the point of this. The whole problem was with the interpretation of A / B (C), if one should prioritize (B*C) or not. The way you wrote it, there was never any doubt that the result is 9.

Collapse
 
immannino profile image
Tony • Edited

I understand where you are coming from, but at the same time I am expressing that there is no interpretation of that priority.

Based on the order of operations, once we have the 6 / 2 (3), this translates to 6 / 2 * 3 (removing the parens), and thus the Order of Operations states we evaluate left to right. Within those rules, there is only 1 correct interpretation.


So I just googled to better understand my reasoning and came across this video with a good explaination. It talks about the historical references to treating the division symbol as 6 / ( 2 * 3) as some people are evaluating. But that interpretation is historical and stems off a different way of evaluation not common today (or at least the video states).

Collapse
 
_ypbr_ profile image
ypbr

If operand has same precedence it is calculated from left to right.

6/2*3 = 3*3 =9..

Collapse
 
filatovv profile image
Yuri Filatov

Cool!

Collapse
 
mafee6 profile image
Mafee7

woow