| 1 | ############################################################################# | ||||||||||||||||||||||||||||
| 2 | # Math/Big/Factors.pm -- factor big numbers into prime factors | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Math::Big::Factors; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | require 5.006002; # requires this Perl version or later | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Math::BigInt; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Math::BigFloat; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Math::Big; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | our $VERSION = '1.16'; | |||||||||||||||||||||||||||
| 17 | 2 | our @ISA = qw( Exporter ); | |||||||||||||||||||||||||||
| 18 | 2 | our @EXPORT_OK = qw( wheel factors_wheel | |||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub wheel | ||||||||||||||||||||||||||||
| 22 | 12 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
wheel: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | # calculate a prime-wheel of order $o | ||||||||||||||||||||||||||||
| 24 | 12 | ▶ | my $o = abs(shift || 1); # >= 1 | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 1
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # some primitive wheels as shortcut: | ||||||||||||||||||||||||||||
| 27 | 12 | ▶ | return [ Math::BigInt->new(2), Math::BigInt->new(1) ] if $o == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 9 | my @primes = Math::Big::primes($o*5); # initial primes, get some more | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 9 | my $mul = Math::BigInt->new(1); my @wheel; | |||||||||||||||||||||||||||
| 32 | 9 | ▶ | for (my $i = 0; $i < $o; $i++) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | { | ||||||||||||||||||||||||||||
| 34 | #print "$primes[$i]\n"; | ||||||||||||||||||||||||||||
| 35 | 27 | $mul *= $primes[$i]; push @wheel,$primes[$i]; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | #print "Mul $mul\n"; | ||||||||||||||||||||||||||||
| 38 | 9 | my $last = $wheel[-1]; # get biggest initial | |||||||||||||||||||||||||||
| 39 | #print "last is $last\n"; | ||||||||||||||||||||||||||||
| 40 | # now sieve any number that is a multiply of one of the inital ones | ||||||||||||||||||||||||||||
| 41 | 9 | @primes = (); # undef => leftover | |||||||||||||||||||||||||||
| 42 | 9 | foreach (@wheel) | |||||||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||||||||
| 44 | 27 | ▶ | next if $_ == 2; # dont mark these, we skip 'em | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 18 | my $i = $_; my $add = $i; | |||||||||||||||||||||||||||
| 46 | 18 | ▶ | while ($i < $mul) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||
| 48 | 462 | $primes[$i] = 1; $i += $add; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 9 | push @wheel, Math::BigInt->new(1); | |||||||||||||||||||||||||||
| 52 | 9 | my $i = $last; | |||||||||||||||||||||||||||
| 53 | 9 | ▶ | while ($i < $mul) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | { | ||||||||||||||||||||||||||||
| 55 | 351 | ▶ | push @wheel,$i if !defined $primes[$i]; $i += 2; # skip even ones | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 9 | \@wheel; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub _transform_wheel | ||||||||||||||||||||||||||||
| 61 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_transform_wheel: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 62 | # from a given prime-wheel, calculate a increment table that can be used | ||||||||||||||||||||||||||||
| 63 | # to step trough numbers | ||||||||||||||||||||||||||||
| 64 | # input: ref to array with prime wheel | ||||||||||||||||||||||||||||
| 65 | # output: ($restart,$ref_to_add_table); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 8 | my (@wheel); | |||||||||||||||||||||||||||
| 68 | 8 | my $add = shift; shift @$add; # remove the first 2 from wheel | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 8 | ▶ | if (@$add == 1) # order 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | { | ||||||||||||||||||||||||||||
| 72 | 2 | my $two = Math::BigInt->new(2); | |||||||||||||||||||||||||||
| 73 | # (2,2) or (2,2,2,2,2,2) etc would do, too | ||||||||||||||||||||||||||||
| 74 | 2 | @wheel = ($two->copy(),$two->copy(),$two->copy(),$two->copy()); | |||||||||||||||||||||||||||
| 75 | 2 | return (1,\@wheel); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | # from the list of divisors above create a add-table which we can take to | ||||||||||||||||||||||||||||
| 78 | # increment from 3 onwards. The tabe consists of two parts, the second part | ||||||||||||||||||||||||||||
| 79 | # will be repeatedly used | ||||||||||||||||||||||||||||
| 80 | 6 | my $last = -1; my $mod = 2; my $i = 0; | |||||||||||||||||||||||||||
| 81 | # create the increment part for the initial primes (3,5, or 3,5,7 etc) | ||||||||||||||||||||||||||||
| 82 | 6 | ▶ | while ($add->[$i] != 1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | { | ||||||||||||||||||||||||||||
| 84 | 12 | $mod *= $add->[$i]; | |||||||||||||||||||||||||||
| 85 | 12 | ▶ | push @wheel, $add->[$i] - $last if $last != -1; # skip the first | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | #print $wheel[-1],"\n" if $last != -1; | ||||||||||||||||||||||||||||
| 87 | 12 | $last = $add->[$i]; $i++; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | #print "mod $mod\n"; | ||||||||||||||||||||||||||||
| 90 | 6 | my $border = $i-1; # account for ++ | |||||||||||||||||||||||||||
| 91 | 6 | my $length = scalar @$add-$i; | |||||||||||||||||||||||||||
| 92 | 6 | my $ws = $border+$length; # remember this | |||||||||||||||||||||||||||
| 93 | #print "border: $border length $length $mod\n"; | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # now we add two arrays in a row, both are equal except the first element | ||||||||||||||||||||||||||||
| 96 | # which is in case A a step from the last inital prime to the second in list | ||||||||||||||||||||||||||||
| 97 | # and in case B a step from '1' to the second in list | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | #print "add[border+1]: ",$add->[$border+1]," add[border] $add->[$border]\n"; | ||||||||||||||||||||||||||||
| 100 | 6 | $wheel[$border] = $add->[$border+2]-$add->[$border]; | |||||||||||||||||||||||||||
| 101 | 6 | $wheel[$border+$length] = $add->[$border+2]-1; | |||||||||||||||||||||||||||
| 102 | # and last add a wrap-around around $mod | ||||||||||||||||||||||||||||
| 103 | #print "last: ",$add->[-1],"\n"; | ||||||||||||||||||||||||||||
| 104 | 6 | $wheel[$border+$length-1] = 1+$mod-$add->[-1]; | |||||||||||||||||||||||||||
| 105 | 6 | $wheel[$border+$length*2-1] = $wheel[$border+$length-1]; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 6 | $i = $border + 1; | |||||||||||||||||||||||||||
| 108 | # now fill in the rest | ||||||||||||||||||||||||||||
| 109 | 6 | ▶ | while ($i < $length+$border-1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | { | ||||||||||||||||||||||||||||
| 111 | 104 | $wheel[$i] = $add->[$i+2]-$add->[$i+1]; | |||||||||||||||||||||||||||
| 112 | 104 | $wheel[$i+$length] = $wheel[$i]; | |||||||||||||||||||||||||||
| 113 | 104 | $i++; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 6 | ($ws,\@wheel); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | sub factors_wheel | ||||||||||||||||||||||||||||
| 119 | 24 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
factors_wheel: called CC=12 SCAR=26.3
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 24 | my $n = shift; | |||||||||||||||||||||||||||
| 121 | 24 | ▶ | my $o = abs(shift || 1); | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 1
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 24 | ▶ | $n = Math::BigInt->new($n) unless ref $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 24 | my $two = Math::BigInt->new(2); | |||||||||||||||||||||||||||
| 125 | 24 | my $three = Math::BigInt->new(3); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 24 | my @factors = (); | |||||||||||||||||||||||||||
| 128 | 24 | my $x = $n->copy(); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 24 | ▶ | return ($x) if $x < 4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 8 | my ($i,$y,$w,$div,$rem); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | #print "Using a wheel of order $o, length "; | ||||||||||||||||||||||||||||
| 134 | 8 | my $wheel = wheel($o); | |||||||||||||||||||||||||||
| 135 | #print scalar @$wheel,":\n"; | ||||||||||||||||||||||||||||
| 136 | 8 | my ($ws,$add) = _transform_wheel($wheel); undef $wheel; | |||||||||||||||||||||||||||
| 137 | 8 | my $we = scalar @$add - 1; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # reduce to odd number (after that, no odd left-over divisior will ocur) | ||||||||||||||||||||||||||||
| 140 | 8 | ▶ | while (($x->is_even) && (!$x->is_zero)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$x->is_even and not $x->is_zero
MC/DC50%1/2
$x->is_even and not $x->is_zero
$x->is_even
not $x->is_zero
Truth table66%2/3
A and not(B)
A= $x->is_evenB= not $x->is_zero
| |||||||||||||||||||||||||||||
| 141 | { | ||||||||||||||||||||||||||||
| 142 | 4 | push @factors, $two->copy(); | |||||||||||||||||||||||||||
| 143 | #print "factoring $x (",$x->length(),")\n"; | ||||||||||||||||||||||||||||
| 144 | #print "2\n"; | ||||||||||||||||||||||||||||
| 145 | 4 | $x /= $two; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | # 8 => 6 => 3, 7, 6 => 3, 5, 4 => 2 => 1, 3, 2 => 1, are all prime | ||||||||||||||||||||||||||||
| 148 | # so the first number interesting for us is 9 | ||||||||||||||||||||||||||||
| 149 | #my $op = 0; | ||||||||||||||||||||||||||||
| 150 | OUTER: | ||||||||||||||||||||||||||||
| 151 | 8 | ▶ | while ($x > 8) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | { | ||||||||||||||||||||||||||||
| 153 | #print "factoring $x (",$x->length(),")\n"; | ||||||||||||||||||||||||||||
| 154 | 8 | $i = $three; $w = 0; | |||||||||||||||||||||||||||
| 155 | 8 | ▶ | while ($i < $x) # should be sqrt() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | { | ||||||||||||||||||||||||||||
| 157 | # $steps++; | ||||||||||||||||||||||||||||
| 158 | # $op = 0, print "$i\r" if $op++ == 1024; | ||||||||||||||||||||||||||||
| 159 | 8 | $y = $x->copy(); | |||||||||||||||||||||||||||
| 160 | 8 | ($div,$rem) = $y->bdiv($i); | |||||||||||||||||||||||||||
| 161 | 8 | ▶ | if ($rem == 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | { | ||||||||||||||||||||||||||||
| 163 | #print "$i\n"; | ||||||||||||||||||||||||||||
| 164 | 8 | push @factors,$i; | |||||||||||||||||||||||||||
| 165 | 8 | $x = $div; next OUTER; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | #print "$i + ",$add->[$w]," ($w)\n"; | ||||||||||||||||||||||||||||
| 168 | #$i += 2; # trial div by odd numbers | ||||||||||||||||||||||||||||
| 169 | 0 | $i += $add->[$w]; | |||||||||||||||||||||||||||
| 170 | #print "restart $w $ws\n" if $w == $we; # wheel of 2,3,5,7... | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | $w = $ws if $w++ == $we; # wheel of 2,3,5,7... | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | #exit if $i > 100000; | ||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | 0 | last; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 8 | ▶ | push @factors,$x if $x != 1 || $n == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$x != 1 or $n == 1
MC/DC0%0/2
$x != 1 or $n == 1
$x != 1
$n == 1
Truth table33%1/3
A or B
A= $x != 1B= $n == 1
| |||||||||||||||||||||||||||||
| 177 | 8 | @factors; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | sub _factor | ||||||||||||||||||||||||||||
| 181 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_factor: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 182 | # later: factor ( n => $n, algorithmn => 'wheel', order => 3 ); | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 2 | 1; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | __END__ | ||||||||||||||||||||||||||||