| 1 | package Math::BigSimple; | ||||||||||||||||||||||||||||
| 2 | 3 | require Exporter; | |||||||||||||||||||||||||||
| 3 | 3 | ▶ | use Math::BigInt; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 5 | 3 | @EXPORT = qw(new); | |||||||||||||||||||||||||||
| 6 | 3 | @EXPORT_OK = qw(is_simple make_simple); | |||||||||||||||||||||||||||
| 7 | 3 | $VERSION = "1.1a"; | |||||||||||||||||||||||||||
| 8 | 3 | $_DEFAULT_CHECKS = 4; | |||||||||||||||||||||||||||
| 9 | 147 | ▶ | $_DEFAULT_RAND = sub { rand() }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | sub new | ||||||||||||||||||||||||||||
| 11 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 13 | 2 | my($LENGTH, $CHECKS, $RAND); | |||||||||||||||||||||||||||
| 14 | 2 | ▶ | my %param = @_ if(@_ % 2 == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | if(exists $param{Length}) # Compability mode (with 1.0 version). | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | { | ||||||||||||||||||||||||||||
| 17 | 1 | $CHECKS = $param{'Checks'}; | |||||||||||||||||||||||||||
| 18 | 1 | $LENGTH = $param{'Length'}; | |||||||||||||||||||||||||||
| 19 | 1 | $RAND = $param{'Random'}; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | else | ||||||||||||||||||||||||||||
| 22 | { | ||||||||||||||||||||||||||||
| 23 | 1 | ($LENGTH, $CHECKS, $RAND) = @_; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 2 | ▶ | if((!$LENGTH) || (int($LENGTH) != $LENGTH)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $LENGTH or int $LENGTH != $LENGTH
MC/DC0%0/2
not $LENGTH or int $LENGTH != $LENGTH
not $LENGTH
int $LENGTH != $LENGTH
Truth table33%1/3
not(A) or B
A= not $LENGTHB= int $LENGTH != $LENGTH
| |||||||||||||||||||||||||||||
| 26 | { | ||||||||||||||||||||||||||||
| 27 | 0 | die "[error] " . __PACKAGE__ . " $VERSION : number length not specified."; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | 2 | ▶ | $CHECKS = $Math::BigSimple::_DEFAULT_CHECKS if((!$CHECKS) || (int($CHECKS) != $CHECKS)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $CHECKS or int $CHECKS != $CHECKS
MC/DC50%1/2
not $CHECKS or int $CHECKS != $CHECKS
not $CHECKS
int $CHECKS != $CHECKS
Truth table66%2/3
not(A) or B
A= not $CHECKSB= int $CHECKS != $CHECKS
| |||||||||||||||||||||||||||||
| 30 | 2 | ▶ | $RAND = $Math::BigSimple::_DEFAULT_RAND if((!$RAND) || (ref($RAND) != 'CODE')); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $RAND or ref $RAND != "CODE"
MC/DC0%0/2
not $RAND or ref $RAND != "CODE"
not $RAND
ref $RAND != "CODE"
Truth table33%1/3
not(A) or B
A= not $RANDB= ref $RAND != "CODE"
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 2 | my $ref = [$LENGTH, $CHECKS, $RAND]; # [$CHECKS, $LENGTH, $RAND] in 1.0. | |||||||||||||||||||||||||||
| 33 | 2 | bless $ref, $class; | |||||||||||||||||||||||||||
| 34 | 2 | return $ref; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | sub make | ||||||||||||||||||||||||||||
| 37 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
make: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 2 | my $ref = shift; | |||||||||||||||||||||||||||
| 39 | 2 | my($LENGTH, $CHECKS, $RAND) = @$ref; | |||||||||||||||||||||||||||
| 40 | 2 | while(1) | |||||||||||||||||||||||||||
| 41 | { | ||||||||||||||||||||||||||||
| 42 | 17 | my $p = &$RAND(); | |||||||||||||||||||||||||||
| 43 | 17 | ▶ | if(int($p * 10) == 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | { | ||||||||||||||||||||||||||||
| 45 | 3 | my $repl = 0; | |||||||||||||||||||||||||||
| 46 | 3 | ▶ | while(!$repl) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||
| 48 | 3 | $repl = int(rand() * 10); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 3 | $p += $repl / 10; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 17 | $p = int($p * (10 ** $LENGTH)); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 17 | ▶ | return $p if(Math::BigSimple::is_simple($p) == 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | sub is_simple | ||||||||||||||||||||||||||||
| 58 | 68 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
is_simple: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 68 | my($number, $CHECKS) = @_; | |||||||||||||||||||||||||||
| 60 | 68 | ▶ | return -1 if((!$number) || (int($number) != $number)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $number or int $number != $number
MC/DC0%0/2
not $number or int $number != $number
not $number
int $number != $number
Truth table33%1/3
not(A) or B
A= not $numberB= int $number != $number
| |||||||||||||||||||||||||||||
| 61 | 68 | ▶ | $CHECKS = $Math::BigSimple::_DEFAULT_CHECKS if(!$CHECKS); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 68 | my $_2 = Math::BigInt->new(2); | |||||||||||||||||||||||||||
| 64 | 68 | my $coof = $number << 4; | |||||||||||||||||||||||||||
| 65 | 68 | my $simple = 1; | |||||||||||||||||||||||||||
| 66 | 68 | my $_p = Math::BigInt->new($number); | |||||||||||||||||||||||||||
| 67 | 68 | my $_i1 = Math::BigInt->new($number-1); | |||||||||||||||||||||||||||
| 68 | 68 | my $_i2 = $_i1->bdiv($_2); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 68 | ▶ | for(my $count = 0; $count < $CHECKS; $count++) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | { | ||||||||||||||||||||||||||||
| 72 | 164 | $simple = 0; | |||||||||||||||||||||||||||
| 73 | 164 | ▶ | last if($number % 2 == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 130 | my $x = Math::BigInt->new(int(&$Math::BigSimple::_DEFAULT_RAND() * $coof) % $number); | |||||||||||||||||||||||||||
| 75 | 130 | ▶ | next if($x->is_zero()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 130 | my $func = $x->bmodpow($_i1, $_p); | |||||||||||||||||||||||||||
| 78 | 130 | $func = $func->bstr(); | |||||||||||||||||||||||||||
| 79 | 130 | ▶ | last if(($func != 1) && ($func != $number-1)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$func != 1 and $func != $number - 1
MC/DC100%2/2
$func != 1 and $func != $number - 1
$func != 1
$func != $number - 1
Truth table100%3/3
A and B
A= $func != 1B= $func != $number - 1
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 108 | $simple = 1; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 68 | ▶ | if($simple == 1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||
| 85 | 12 | return 1; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | else | ||||||||||||||||||||||||||||
| 88 | { | ||||||||||||||||||||||||||||
| 89 | 56 | return 0; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | sub make_simple | ||||||||||||||||||||||||||||
| 93 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
make_simple: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 1 | my $g = Math::BigSimple->new(shift); | |||||||||||||||||||||||||||
| 95 | 1 | return $g->make(); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | __END__ | ||||||||||||||||||||||||||||