Math::Cryptarithm - Solving simple cryptarithm.
Version 0.20.2
A primitive cryptarithm (also known as verbal arithmetic) solver.
See English Wikipedia: Verbal arithmetic.
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 ];
Return a list object of hashes with all possible solutions. Different letters represent different digits.
Return the possible solutions in "decrypted equations" form. See the section Synopsis.
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.
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.
Cheok-Yin Fung, <fungcheokyin at gmail.com>
https://github.com/E7-87-83/Math-Cryptarithm.
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).
To install Math::Cryptarithm, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Math::Cryptarithm
CPAN shell
perl -MCPAN -e shell install Math::Cryptarithm
For more information on module installation, please visit the detailed CPAN module installation guide.