The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Math::Cryptarithm - Solving simple cryptarithm.

VERSION

Version 0.20.2

DESCRIPTION

A primitive cryptarithm (also known as verbal arithmetic) solver.

See English Wikipedia: Verbal arithmetic.

SYNOPSIS

    use Math::Cryptarithm;
    use Data::Dumper;

    my $abc5 = ["A + B = C5 ", "A % 2 = 0"];

    my $abc5_ans_in_eqs = Math::Cryptarithm->new($abc5)->solve_ans_in_equations();

    for my $set ($abc5_ans_in_eqs->@*) { 
        print join "\n", @{$set};
        print "\n\n"
    }

    # 2 + 3 = 05 
    # 2 % 2 = 0
    # 
    # 4 + 1 = 05 
    # 4 % 2 = 0
    #
    # 8 + 7 = 15 
    # 8 % 2 = 0
    #
    # 6 + 9 = 15 
    # 6 % 2 = 0


    my $abcd = [
        "ABA * ABA = CCDCC", 
        "ABA * A = CAC", 
        "ABA * B = ABA"
    ];

    my $abcd_ans = Math::Cryptarithm->new($abcd)->solve_alphabetvalue_hash();

    say scalar $abcd_ans->@*;             # 1
    say $abcd_ans->[0]->{"A"};            # 2
    say $abcd_ans->[0]->{"B"};            # 1
    say $abcd_ans->[0]->{"C"};            # 4
    say $abcd_ans->[0]->{"D"};            # 9 


    my $magical_seven = ["ABCDEF * 6 = DEFABC"];
    my $magical_seven_ans = Math::Cryptarithm->new($magical_seven)->solve_alphabetvalue_hash();

    print Dumper($magical_seven_ans);

    # $VAR1 = [ { 'A' => 1, 'F' => 7, 'E' => 5, 'B' => 4,'D' => 8, 'C' => 2 ];

METHODS

solve_alphabetvalue_hash()

Return a list object of hashes with all possible solutions. Different letters represent different digits.

solve_ans_in_equations()

Return the possible solutions in "decrypted equations" form. See the section Synopsis.

TODOS

Improve the Module by Backtracking instead of Permutations

Currently the module runs slowly when the number of variables is equal to or more than 6. Using a backtracking as the algorithm should improve the performance of the module.

"?"

Allow "?" to represent arbitary digits. See the related Wikipedia entry (zh or ja) using "square box" representing any possible digits.

setLeadingZeros($symbol)

To determine whether allow zeros as possible values as the leading part of a number. 1 is allowed. 0 means not allowed. Default should be 1.

AUTHOR

Cheok-Yin Fung, <fungcheokyin at gmail.com>

REPOSITORY

https://github.com/E7-87-83/Math-Cryptarithm.

COPYRIGHT & LICENSE

Copyright 2021 FUNG CHEOK YIN, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0).