Wednesday 3 June 2015

Cryptarithmetic problem

Verbal arithmetic, also known as alphameticscryptarithmeticcrypt-arithmeticcryptarithm or word addition, is a type of mathematical game consisting of a mathematical equation among unknown numbers, whose digits are represented by letters. The goal is to identify the value of each letter. The name can be extended to puzzles that use non-alphabetic symbols instead of letters.
following java code implements cryptarithmetic problem.
    T O O
+  T O O
    T O O
 ----------------
    G O O D
 ----------------
T=1, O=2, G=3, D=4


import java.util.Scanner;

public class cryptoarithmatic
{
  public static void main(String args[])
  {
        for (int T = 0; T <= 9; T++)
        {
                for (int O= 0; O <= 9; O++)
                {
                        for (int G= 0; G <= 9; G++)
                        {
                                for (int D= 0; D <= 9; D++)
                                {
                                      
                                         if ((T != O) && (T != G) && (T != D) && (O != G) && (O!= D) && (G != D)){
                                                int too = T* 100 + O * 10 + O, good = G * 1000 + O * 100 + O* 10 + D;
                                                if (good == 4 *too)
                                                {
                                                        System.out.println( " T = " +T);
                                                        System.out.println(" O= " +O);
                                                        System.out.println("O = " +O);
                                                        System.out.println("G = " +G);
                                                        System.out.println("D = " +D);
                                                       
                                                        System.out.println("TOO = "+  too );
                                                        System.out.println( "GOOD = " +good );
                                                }
                                        }
                                }
        }
        }
        }
  }

}

No comments:

Post a Comment