1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Math Brain Teaser Question and attempted solution with Java - need help.

  1. Jul 21, 2009 #1
    1. The problem statement, all variables and given/known data

    The question is thus:

    e = even digit
    o = odd digit

    eo
    x ee
    -----
    eoe
    +eoe
    -----
    oooe


    Which is just the expanded multiplication of the numbers eo and ee, with eoe and eoe being the partial products and oooe being the product. The question is to find the numbers which fit the digits.


    3. The attempt at a solution

    Here is the code I wrote to find the solution:

    Code (Text):
    public class EvensOdds
    {
        public static void main(String[ ] args)
        {
           
            // num = AB, CD, EFGH
            double a, b, c, d, e, f, g, h;
            double fir, sec;
            double fira, firb, firc;
            double seca, secb, secc;
            double product;
           
            for(int i = 9; i < 100; i++){
                b = Math.floor(i % 10);
                a = Math.floor((i % 100)/10);
                if(a % 2 == 0 && b % 2 != 0){
                    for(int x = 9; x < 100; x++){
                        d = Math.floor(x % 10);
                        c = Math.floor((x % 100)/10);
                        if(c % 2 == 0 && d % 2 ==0){
                            fir = d * i;
                                firc = Math.floor(fir % 10);
                                firb = Math.floor((fir % 100)/10);
                                fira = Math.floor((fir % 1000) / 100);
                            sec = c * i * 10;
                                secc = Math.floor(sec % 10);
                                secb = Math.floor((sec % 100)/10);
                                seca = Math.floor((sec % 1000) / 100);
                            if(firc%2==0 && firb%2!=0 && fira%2==0 && secc%2==0 && secb%2!=0 && seca%2==0 && sec < 1000){
                            product = i * x;
                            if(product > 999 && product < 10000){
                                h = Math.floor(product % 10);
                                g = Math.floor((product % 100)/10);
                                f = Math.floor((product % 1000) / 100);
                                e = Math.floor((product % 10000) / 1000);
                                if(e%2!=0 && f%2!=0 && g%2!=0 && h%2==0){
                                    System.out.println(i+", "+x+", "+fir+", "+sec+", "+product);}}}}}}}
        }
    }
    The code compiles without error, and, to my understanding, should produce the correct answer. But, when I run the program, no solution is printed. If you see an error with my code, or another method to solve the problem please let me know!
     
    Last edited: Jul 21, 2009
  2. jcsd
  3. Jul 21, 2009 #2
    Ok, so I've been going over the problem theoretically, and it seems impossible. Tell me if this makes sense:

    eo
    x ee
    -----
    eoe
    +eoe <- this first e must be 0 since its the partial product of the tens digit
    -----
    oooe <- so this e must be equal to the e in the ones digit of the first partial product

    since any 1 digit number + 0 will always remain a 1 digit number, we know that no numbers are carried and that the o + o of the tens digit must result in an odd number. When you add two, single digit, odd numbers together you will always get an even number, therefore not an "o" and the problem has no solution.

    Unfortunately, this problem comes from a reputable source, and it must have some solution.
     
  4. Jul 21, 2009 #3
    can you give an example ? i didn't understand precisely what is the task. But maybe i can help.
     
  5. Jul 21, 2009 #4
    judging by your code, which is nasty, i think your task is
    number1 has to be EO
    number2 has to be EE
    product has to be OOOE
    Here is some code i wrote that does that.
     
  6. Jul 21, 2009 #5
    judging by your code, which is nasty, i think your task is
    number1 has to be EO
    number2 has to be EE
    product has to be OOOE
    Here is some code i wrote that does that.

    Code (Text):
    public class EvensOdds {
        public static void main (String[] args) {
            for (int num1 = 10; num1 < 100; num1++) {
                if (!testEvenOdd(num1))
                    continue;
                for (int num2 = 10; num2 < 100; num2++) {
                    if (!testEvenEven(num2))
                        continue;
                    if (EvensOdds.test(num1, num2))
                        System.out.println (num1 + " " + num2 + " match OOOE " +  num1 * num2);
                }
            }
        }

        public static double getDigit (int number, int digitNumber) {
            double power = Math.pow(10, digitNumber);
            double powerCutter = Math.pow (10, digitNumber -1);
            double target = number % (power);
            target = target / powerCutter;
            return Math.floor(target);
        }
       
        public static boolean testEvenOdd (int num) {
            if ((getDigit(num, 1) % 2 != 0) && (getDigit(num, 2) % 2 == 0))
                return true;
            return false;
        }
        public static boolean testEvenEven (int num) {
            if ((getDigit(num, 1) % 2 == 0) && (getDigit(num, 2) % 2 == 0))
                return true;
            return false;
        }

        public static boolean test (int num1, int num2) {
            boolean go=true;
           
            int product = num1 * num2;
            if ((getDigit(product, 1) % 2 == 0) && //even
                (getDigit(product, 2) % 2 != 0) && //odd
                (getDigit(product, 3) % 2 != 0) && //odd
                (getDigit(product, 4) % 2 != 0)) //odd
                go = true;
            else
                go = false;
            return go;
        }
    }
     
     
  7. Jul 21, 2009 #6
    eo
    x ee
    -----
    eoe
    +eoe
    -----
    oooe

    The question clearly has no solution. Why?

    The ten's place of the "ee", when multiplied by the one's place of the "eo" must yield a number (of one or two digits) the rightmost digit of which is e. This should be the middle character of the second line, which is in fact eoe. Because e x o = ?e, and that e is what is supposed to be in the middle position for the second summand, the answer has no solution (as you have presented it).
     
  8. Jul 21, 2009 #7

    Dick

    User Avatar
    Science Advisor
    Homework Helper

    eo=69, ee=46. Use that to debug your codes and proofs.
     
  9. Jul 21, 2009 #8
    Thanks for the help guys. Yea, Tigor thats basically what I was asking, except the partial products are also included and tested in my code, which I bet is nasty! I've never taken a real programming class.

    Dick, I think thats the "correct" solution, though the partial product of 69 and the 4 is actually 2760 (eoee). But, I think the way the problem is given, you are supposed to assume a zero already and go for the other eoe digits like you did. Modifying the code to fit that scenario is easy.

    Here's my finished code:

    Code (Text):
    public class EvensOdds
    {
        public static void main(String[ ] args)
        {
           
            // num = AB, CD, EFGH
            int a, b, c, d, e, f, g, h;
            int fir, sec;
            double fira, firb, firc;
            double seca, secb, secc;
            int product;
           
            for(int i = 9; i < 100; i++){
                b = (int)Math.floor(i % 10);
                a = (int)Math.floor((i % 100)/10);
                if(a % 2 == 0 && b % 2 != 0){
                    for(int x = 9; x < 100; x++){
                        d = (int)Math.floor(x % 10);
                        c = (int)Math.floor((x % 100)/10);
                        if(c % 2 == 0 && d % 2 ==0){
                            fir = d * i;
                                firc = Math.floor(fir % 10);
                                firb = Math.floor((fir % 100)/10);
                                fira = Math.floor((fir % 1000) / 100);
                            sec = c * i;
                                secc = Math.floor(sec % 10);
                                secb = Math.floor((sec % 100)/10);
                                seca = Math.floor((sec % 1000) / 100);
                                if(firc%2==0 && firb%2!=0 && fira%2==0 && secc%2==0 && secb%2!=0 && seca%2==0 && sec < 1000 && sec > 99){
                                    product = i * x;
                                    if(product > 999 && product < 10000){
                                        h = (int)Math.floor(product % 10);
                                        g = (int)Math.floor((product % 100)/10);
                                        f = (int)Math.floor((product % 1000) / 100);
                                        e = (int)Math.floor((product % 10000) / 1000);
                                        if(e%2!=0 && f%2!=0 && g%2!=0 && h%2==0){
                                            System.out.println(i+", "+x+", "+fir+", "+sec+", "+product);}}}}}}}
        }
    }
     
  10. Jul 21, 2009 #9

    Dick

    User Avatar
    Science Advisor
    Homework Helper

    Here's the python code I used, if you are curious. It can be written pretty simply.

    Code (Text):

    evens=['0','2','4','6','8']
    odds=['1','3','5','7','9']

    def test(s,eo):
      for i in range(len(s)):
        if (eo[i]=='e'):
          if (s[i] in odds):
            return(False)
        if (eo[i]=='o'):
          if (s[i] in evens):
            return(False)
      return(True)

    for i1 in evens:
      for i2 in odds:
        for i3 in evens:
          for i4 in evens:
            n1=int(i1+i2)
            n2=int(i3+i4)
            p1=`int(i4)*n1`
            p2=`int(i3)*n1`
            tot=`n1*n2`
            if (len(p1)!=3 or len(p2)!=3 or len(tot)!=4):
              continue
            if (test(p1,'eoe') and test(p2,'eoe') and test(tot,'oooe')):
              print n1,n2,p1,p2,tot
     
     
  11. Jul 22, 2009 #10
    Dick, i loved your algorithm - very cool approach !!:cool:
     
  12. Jul 22, 2009 #11

    Dick

    User Avatar
    Science Advisor
    Homework Helper

    Thanks. I credit python with having the sort of idioms that encourage you to think that way.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Math Brain Teaser Question and attempted solution with Java - need help.
  1. Java Help (Replies: 6)

  2. JAVA ArrayList help (Replies: 5)

Loading...