-11
Long AAA = 42L;
Long BBB = -37L;
Long TTT = 17206538691L;

problem is don't know value that X should be for equation:

AAA + BBB * X == TTT

this should find the value for X but it's off

Long X = TTT / BBB - AAA; 

This keep returning false

Boolean Matched = AAA + BBB * X == TTT;
Long Result = AAA + BBB * X;
System.out.println(X.toString()+" Matched "+Matched.toString()+" Result "+Result.toString());
Matt
  • 74,352
  • 26
  • 153
  • 180
  • 4
    Shouldn't your equation be: `X = (TTT - AAA) / BBB` ? – Frakcool Jun 16 '17 at 13:52
  • 1
    Are you asking a basic math question? – OH GOD SPIDERS Jun 16 '17 at 13:52
  • 1
    1) Your formula is wrong. `AAA + BBB * X == TTT` means `X = (TTT - AAA) / BBB`. --- 2) Result of that is `-465041585.10810810810810810810811`, i.e. not an *integer* value. --- 3) Don't use boxed objects. Change `Long` to `long`. Or rather, to `double` (see #2). --- For all the people voting to close as "math-only" problem: You're wrong. True, question has bad math, but it is also a programming problem with using integer math for a non-integer result. – Andreas Jun 16 '17 at 13:56
  • @OHGODSPIDERS `X = (TTT - AAA) / BBB` with `AAA = 42`, `BBB = -37`, and `TTT = 17206538691` gives `X = (17206538691 - 42) / -37 = 17206538649 / -37 = -465041585.10810810810810810810811`, according to my handy Windows Calculator app. – Andreas Jun 16 '17 at 14:03
  • @Andreas yes, realized my mistake a few seconds after posting and deleted the comment. Guess that happens when you use a cheap calculator that just rounds stuff without telling you. – OH GOD SPIDERS Jun 16 '17 at 14:09
  • @Martyn if you are doing maths, don't use wrapped primitive types (e.g. `Long`), use primitive types (e.g. `long`). Also, you don't need `.toString()` in your string concatenation: in `a + b`, if either `a` or `b` is a string, the other one is automagically converted to a string (e.g. `0 + "a"` produces `"0a"`, no need to change the `0` to a string yourself). – Andy Turner Jun 16 '17 at 14:13
  • @All-those-who-only-see-maths-problem If it were just a maths problem then the answer would be simple and not worthy of any ones time. All those that claimed it is a maths problem have clearly demonstrated a lack of understanding how Java operates by assuming the equation is solved left to right, and that a Long can be a Decimal. – Martyn Kemp Jun 17 '17 at 12:37
  • When an equation is performed without parenthesis, then Java goes in priority of the Operator, hence multiplications are performed before additions and subtractions. This is a very relevant question about programming Java, because we are getting into the behaviour of this language and how it handles certain situations. – Martyn Kemp Jun 17 '17 at 12:43

1 Answers1

4

It's not completely clear what you are looking for, but note that java long arithmetic is effectively done mod 264. You can investigate modular inverses and the extended euclidean algorithm yourself, as well as how java handles integer overflow. The BigInteger class makes doing these experiments relatively easily, as this example shows.

public class Main {
    static long AAA = 42L;
    static long BBB = -37L;
    static long TTT = 17206538691L;

    private static long solve() {
        // compute x = inverse(BBB, 1<<64) * (TTT - AAA)

        BigInteger two_64 = BigInteger.ONE.shiftLeft(64);
        BigInteger BBB_inverse = BigInteger.valueOf(BBB).modInverse(two_64);
        return BBB_inverse.multiply(BigInteger.valueOf(TTT - AAA)).longValue();
    }

    public static void main(String[] args) {
        System.out.println(solve());
    }
}

which shows that the answer is -5982727808154625893L.

This only works if BBB is an odd integer.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125