“Presto” math trick in Java

June 23rd, 2010

According to Futility Closet, if you start with a 3 digit and place it next to the same number to form a 6 digit number, you can divide the 6 digit number by 7, 11, and then 13 and you will end up with the original 3 digit number and no remainders.  For example, by taking the number 412 and making it 412412 and then doing the divisions, you will end up with 412.  I wrote a small program in Java to test it.

The Code

  1. public class Main
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         for(int i = 100; i <= 999; i++)
  6.         {
  7.             //get the number, and "double" it
  8.             int number = Integer.parseInt(Integer.toString(i) + Integer.toString(i));
  9.  
  10.             //successively divided by 7, 11, then 13
  11.             System.out.print((i) + "\t");
  12.             System.out.print((number) + "\t");
  13.             System.out.print((number /= 7) + "\t");
  14.             System.out.print((number /= 11) + "\t");
  15.             System.out.print(number /= 13);
  16.  
  17.             //is the result what we expect? (input == output)
  18.             if(i == number)
  19.             {
  20.                 System.out.print("\tCorrect\n");
  21.             }
  22.             else
  23.             {
  24.                 System.out.print("\tIncorrect");
  25.                 break;
  26.             }
  27.         }
  28.     }
  29. }

In this snippet, we loop through all 3 digit numbers (100 to 999) and output the results of each operation in a tab separated column. If the result (output) is the same as the input, we print correct and continue with the loop. Otherwise, we print incorrect and break. Give it a try!

Leave a Reply