| 1 | package Acme::Goedelize; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Math::BigInt; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 1 | require Exporter; | |||||||||
| 10 | |||||||||||
| 11 | 1 | our @ISA = qw(Exporter); | |||||||||
| 12 | |||||||||||
| 13 | 1 | our $VERSION = '0.02'; | |||||||||
| 14 | |||||||||||
| 15 | |||||||||||
| 16 | # Alpha-to-number | ||||||||||
| 17 | 1 | my %a_to_n = ( ' ' => '0', | |||||||||
| 18 | 'a' => '1', | ||||||||||
| 19 | 'b' => '2', | ||||||||||
| 20 | 'c' => '3', | ||||||||||
| 21 | 'd' => '4', | ||||||||||
| 22 | 'e' => '5', | ||||||||||
| 23 | 'f' => '6', | ||||||||||
| 24 | 'g' => '7', | ||||||||||
| 25 | 'h' => '8', | ||||||||||
| 26 | 'i' => '9', | ||||||||||
| 27 | 'j' => '10', | ||||||||||
| 28 | 'k' => '11', | ||||||||||
| 29 | 'l' => '12', | ||||||||||
| 30 | 'm' => '13', | ||||||||||
| 31 | 'n' => '14', | ||||||||||
| 32 | 'o' => '15', | ||||||||||
| 33 | 'p' => '16', | ||||||||||
| 34 | 'q' => '17', | ||||||||||
| 35 | 'r' => '18', | ||||||||||
| 36 | 's' => '19', | ||||||||||
| 37 | 't' => '20', | ||||||||||
| 38 | 'u' => '21', | ||||||||||
| 39 | 'v' => '22', | ||||||||||
| 40 | 'w' => '23', | ||||||||||
| 41 | 'x' => '24', | ||||||||||
| 42 | 'y' => '25', | ||||||||||
| 43 | 'z' => '26', | ||||||||||
| 44 | '.' => '27' | ||||||||||
| 45 | ); | ||||||||||
| 46 | |||||||||||
| 47 | ### Methods | ||||||||||
| 48 | |||||||||||
| 49 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 50 | 1 | bless {}, shift; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 1 | ▶ | sub to_number { | ||||||||
|
Subroutine
to_number: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 54 | 1 | my ($self, $text) = @_; | |||||||||
| 55 | |||||||||||
| 56 | 1 | my %link = %a_to_n; | |||||||||
| 57 | |||||||||||
| 58 | ### Check the text | ||||||||||
| 59 | 1 | ▶ | croak "The string must contain only alpha chars and spaces" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | if ($text !~ /^[a-zA-Z\s]*$/); | ||||||||||
| 61 | |||||||||||
| 62 | ### Append dot | ||||||||||
| 63 | 1 | ▶ | if ($text !~ /\.$/) { $text .= "."; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | |||||||||||
| 65 | 1 | my @txt = split(//, $text); | |||||||||
| 66 | |||||||||||
| 67 | 1 | my $current_prime = 2; | |||||||||
| 68 | 1 | my $result = Math::BigInt->new(1); | |||||||||
| 69 | |||||||||||
| 70 | 1 | foreach my $char ( @txt ) { | |||||||||
| 71 | 4 | my $prime = get_next_prime($current_prime); | |||||||||
| 72 | 4 | my $current = Math::BigInt->new($prime); | |||||||||
| 73 | 4 | $result *= ($current ** $link{$char}); | |||||||||
| 74 | 4 | $current_prime = $prime + 1; | |||||||||
| 75 | } | ||||||||||
| 76 | 1 | return $result; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | |||||||||||
| 80 | 1 | ▶ | sub to_text { | ||||||||
|
Subroutine
to_text: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 81 | 1 | my ($self, $number) = @_; | |||||||||
| 82 | |||||||||||
| 83 | 1 | my %link = reverse %a_to_n; | |||||||||
| 84 | |||||||||||
| 85 | ### Check the text | ||||||||||
| 86 | 1 | ▶ | croak "The string must be number\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | if $number !~ /^[0-9]*$/; | ||||||||||
| 88 | |||||||||||
| 89 | 1 | my $goedel = Math::BigInt->new($number); | |||||||||
| 90 | |||||||||||
| 91 | 1 | my $current_prime = 2; | |||||||||
| 92 | 1 | my $result; | |||||||||
| 93 | |||||||||||
| 94 | 1 | PROCESS: while (1) { | |||||||||
| 95 | 4 | my $prime = get_next_prime($current_prime); | |||||||||
| 96 | |||||||||||
| 97 | 4 | my $times = 0; | |||||||||
| 98 | 4 | my $tmp = $goedel; | |||||||||
| 99 | 4 | DIVISION: while (1) { | |||||||||
| 100 | 37 | my $num = ($tmp/$prime); | |||||||||
| 101 | 37 | ▶ | last DIVISION if ( ($tmp % $prime) > 0 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 33 | $times++; | |||||||||
| 103 | 33 | $tmp = $num; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 4 | ▶ | last PROCESS if $link{$times} eq "."; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 3 | $result .= $link{$times}; | |||||||||
| 108 | 3 | $current_prime = $prime + 1; | |||||||||
| 109 | } | ||||||||||
| 110 | 1 | return $result; | |||||||||
| 111 | } | ||||||||||
| 112 | |||||||||||
| 113 | 8 | ▶ | sub get_next_prime { | ||||||||
|
Subroutine
get_next_prime: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 114 | 8 | my $current = shift; | |||||||||
| 115 | 8 | GUESS: for (my $guess = $current; ; $guess++) | |||||||||
| 116 | { | ||||||||||
| 117 | 12 | ▶ | for (my $divisor = 2; $divisor < $guess; $divisor++) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | { | ||||||||||
| 119 | 22 | ▶ | next GUESS unless $guess % $divisor; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | } | ||||||||||
| 121 | 8 | return $guess; | |||||||||
| 122 | } | ||||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | |||||||||||
| 126 | 1 | 1; | |||||||||
| 127 | __END__ | ||||||||||