-3

I have a very challenging problem here today. I cannot think of a way to solve it.

Given 6 numbers as input: a1, a2, a3, b1, b2, b3, find 2 numbers X and Y such that a1 * x^2 + a2 ^ x + a3 = b1 * y^2 + b2 * y + b3. X and Y must be between 10 and 15000 inclusive.

What I have tried:

I have tried all X values from 10-15000 and all Y values from 10-15000, and checked if they satisfied the equation. However, this method is extremely slow. Does anyone have a faster solution? Thanks.

My Bad Code:

for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    for (int i = 10; i <= 15000; i++) {
        for (int j = 10; j <= 15000; j++) {
            if (conv(a, i) == conv(b, j)) {
                cout << i << " " << j << endl;
                j = 20000;
                i = 20000;
            }
        }
    }
}
long long conv(int x, int b) {
long long ans = 0;
int count = 0;
while (x) {
    int y = x % 10;
    ans += y * poww(b, count);
    count++;
    x /= 10;
}
return ans;
}
long long poww(int x, int y) {
long long ans = 1;
while (y != 0) {
    ans *= x;
    y--;
}
return ans;
}
Andy
  • 49,085
  • 60
  • 166
  • 233
Bob Billy
  • 285
  • 1
  • 6
  • 5
    We will not do your homework for you. – Cory Kramer Jan 17 '15 at 19:01
  • 1
    Apparently your fast solution is to go to SO ? What did you try ? – quantdev Jan 17 '15 at 19:01
  • 1
    Should we show as much effort as you have shown? – Drew Dormann Jan 17 '15 at 19:02
  • I have been thinking for an hour and I only have thought of one solution. – Bob Billy Jan 17 '15 at 19:06
  • 1
    Just FYI, the "base 10" isn't really part of the problem. What you're looking for is, given six numbers as input (a1, a2, a3, b1, b2, b3), is to find positive integers X and Y such that a1*X^2 + a2*X + a3 = b1*Y^2 + b2*Y + b3. I don't know if that helps--I'm not seeing an obvious approach. – ajb Jan 17 '15 at 19:09
  • Thanks ajb, I changed my problem so it's less confusing. – Bob Billy Jan 17 '15 at 19:13
  • Can you show us the "extremely slow" code that you wrote, so we may find opportunity to improve it? – Drew Dormann Jan 17 '15 at 19:15
  • @DrewDormann Yes, I have posted my code on there. I am sure it's the wrong approach though. Also, the problem states that X and Y must be above 10. – Bob Billy Jan 17 '15 at 19:19
  • Hang on, I am in the process of adding code. – Bob Billy Jan 17 '15 at 19:21
  • Voting to reopen. There is actual code to be seen. – Drew Dormann Jan 17 '15 at 19:27
  • OK, I thought of something. For each X, compute the value of what the number would be in that base. If the value is V, then you must have b1*Y^2 + b2*Y + b3 = V, and you can use the quadratic formula to see if there are any integer solutions > 10. Since this is only a single loop and not a double loop, it will be much faster. – ajb Jan 17 '15 at 20:25
  • Thank you everyone, I actually managed to solve this problem before someone posted an answer, but thanks! I love all of your replies and thank you for spending time to help me. If you're interested, my solution was the same with the double for loops, but the inner for loop went from 0 until i, instead of 0 to 15000. – Bob Billy Jan 17 '15 at 22:29

1 Answers1

0

I thought this might be a good occassion to exercise writing some Java code and came up with the following solution. On my system it gives the solution to the two numbers 419 and 792 (as you wrote in an earlier edit of your question the result should be Base X: 47 Base Y: 35) in 1 ms.

The code just uses some smart brute force :).

See it running online.

public class TwoBases {
    public static void main(String[] args) {
        long beg = System.nanoTime();
        solve(419, 792);
        System.out.println("Time needed to calculate: "+(System.nanoTime()-beg)/1000000.0 + "ms");
    }

    public static void solve(int a, int b) {
        int[] aDigits = new int[3];
        int[] bDigits = new int[3];
        for (int i = 0; i < 3; i++) {
            aDigits[2 - i] = (a / (int) Math.pow(10, i)) % 10;
            bDigits[2 - i] = (b / (int) Math.pow(10, i)) % 10;
        }
        for (int x = 10; x <= 15000; x++) {
            int numBaseX = digitsToBase10(aDigits, x);
            int y = 10;
            while (y <= 15000) {
                int numBaseY = digitsToBase10(bDigits, y);
                if (numBaseX == numBaseY) {
                    System.out.println("Base X: " + x + " Base Y: " + y);
                    return;
                } else if (numBaseY > numBaseX) {
                    break;
                } else {
                    y++;
                }
            }
        }
        System.out.println("Nothing found");
    }

    public static int digitsToBase10(int[] digits, int b) {
        int res = 0;
        for (int i = 0; i < digits.length; i++) {
            res += digits[i] * (int) Math.pow(b, digits.length - 1 - i);
        }
        return res;
    }
}
halex
  • 16,253
  • 5
  • 58
  • 67