MohammadAbusaleh
MohammadAbusaleh

Reputation: 118

Integer results is different from calculation

So if found the link for this article: http://www.theguardian.com/science/alexs-adventures-in-numberland/2015/may/21/how-to-solve-the-maths-puzzle-for-vietnamese-eight-year-olds-that-stumped-parents-and-teachers

so i decided to use a java code to solve it.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Quiz {

    public static void main (String [] args){
        Double result = 0.0;
        boolean test = false;

        List<Integer> dataList = new ArrayList<Integer>();
        for (int i = 1; i < 10; i++) {
            dataList.add(i);
        }

        int[] num = new int[dataList.size()];

        while (test == false) {
            Collections.shuffle(dataList);
            for (int i = 0; i < dataList.size(); i++) {
                num[i] = dataList.get(i);
            }

            if ((num[0] + 13 * num[1] / num[2] + num[3] + 12 * num[4] - num[5] - 11 + num[6] * num[7] / num[8] - 10) == (3 + 13 * 2 / 1 + 5 + 12 * 4 - 7 - 11 + 9 * 8 / 6 - 10)){
                test = true;
            }
        }

        for (int i = 0; i < num.length; i++) {
            System.out.println(num[i]);
        }
        System.out.println(test);
    }
}

The code checks both equations and compares them and gives me true, but when i calculate them with a calculator, the result is different.

What could be wrong ?

Upvotes: 0

Views: 99

Answers (1)

Yogesh
Yogesh

Reputation: 1585

it's because type of result depends on type of it's operands. In your case since all operands are of type int hence final result will also be an int

Upvotes: 3

Related Questions