| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
################################################################################################### |
|
2
|
|
|
|
|
|
|
# Copyright 2013 by Marcel Greter |
|
3
|
|
|
|
|
|
|
# This file is part of Webmerge (GPL3) |
|
4
|
|
|
|
|
|
|
#################################################################################################### |
|
5
|
|
|
|
|
|
|
# static helper functions for canvas |
|
6
|
|
|
|
|
|
|
#################################################################################################### |
|
7
|
|
|
|
|
|
|
package OCBNET::WebSprite::Math; |
|
8
|
|
|
|
|
|
|
#################################################################################################### |
|
9
|
|
|
|
|
|
|
our $VERSION = '1.0.0'; |
|
10
|
|
|
|
|
|
|
#################################################################################################### |
|
11
|
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
21823
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
39
|
|
|
13
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
38
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
################################################################################################### |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# load exporter and inherit from it |
|
18
|
1
|
|
|
1
|
|
6
|
BEGIN { use Exporter qw(); our @ISA = qw(Exporter); } |
|
|
1
|
|
|
1
|
|
2
|
|
|
|
1
|
|
|
|
|
42
|
|
|
|
1
|
|
|
|
|
57
|
|
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# define our functions to be exported |
|
21
|
1
|
|
|
1
|
|
407
|
BEGIN { our @EXPORT_OK = qw(lcm gcf snap factors); } |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
#################################################################################################### |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# try to optimize slow functions |
|
26
|
|
|
|
|
|
|
# with memoize oly if available |
|
27
|
|
|
|
|
|
|
# eval |
|
28
|
|
|
|
|
|
|
# { |
|
29
|
|
|
|
|
|
|
# # try to load |
|
30
|
|
|
|
|
|
|
# use Memoize qw(memoize); |
|
31
|
|
|
|
|
|
|
# # memoize functions |
|
32
|
|
|
|
|
|
|
# memoize('gcf', LIST_CACHE => 'MERGE'); |
|
33
|
|
|
|
|
|
|
# memoize('lcm', LIST_CACHE => 'MERGE'); |
|
34
|
|
|
|
|
|
|
# memoize('multigcf', LIST_CACHE => 'MERGE'); |
|
35
|
|
|
|
|
|
|
# memoize('multilcm', LIST_CACHE => 'MERGE'); |
|
36
|
|
|
|
|
|
|
# }; |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
#################################################################################################### |
|
39
|
|
|
|
|
|
|
# stolen from http://www.perlmonks.org/?node_id=56906 |
|
40
|
|
|
|
|
|
|
#################################################################################################### |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# greatest common factor |
|
43
|
|
|
|
|
|
|
sub _gcf($$) |
|
44
|
|
|
|
|
|
|
{ |
|
45
|
10
|
|
|
10
|
|
16
|
my ($x, $y) = @_; |
|
46
|
10
|
|
|
|
|
54
|
($x, $y) = ($y, $x % $y) while $y; |
|
47
|
10
|
|
|
|
|
34
|
return $x; |
|
48
|
|
|
|
|
|
|
} |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# least common multiple |
|
51
|
|
|
|
|
|
|
sub _lcm($$) |
|
52
|
|
|
|
|
|
|
{ |
|
53
|
4
|
|
|
4
|
|
15
|
return $_[0] * $_[1] / _gcf($_[0], $_[1]); |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# greatest common factor |
|
57
|
|
|
|
|
|
|
sub gcf(@) |
|
58
|
|
|
|
|
|
|
{ |
|
59
|
4
|
|
|
4
|
0
|
10
|
my $x = shift; |
|
60
|
4
|
|
|
|
|
12
|
$x = _gcf($x, shift) while @_; |
|
61
|
4
|
|
|
|
|
19
|
return $x; |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# least common multiple |
|
65
|
|
|
|
|
|
|
sub lcm(@) |
|
66
|
|
|
|
|
|
|
{ |
|
67
|
3
|
|
|
3
|
0
|
8
|
my $x = shift; |
|
68
|
3
|
|
|
|
|
21
|
$x = _lcm($x, shift) while @_; |
|
69
|
3
|
|
|
|
|
50
|
return $x; |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
#################################################################################################### |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# snap value to given multiplier |
|
75
|
|
|
|
|
|
|
# ****************************************************************************** |
|
76
|
|
|
|
|
|
|
sub snap |
|
77
|
|
|
|
|
|
|
{ |
|
78
|
|
|
|
|
|
|
# get rest by modulo divide |
|
79
|
0
|
|
|
0
|
0
|
0
|
my $rest = $_[0] % $_[1]; |
|
80
|
|
|
|
|
|
|
# add rest to fill up to multipler |
|
81
|
0
|
0
|
|
|
|
0
|
$_[0] += $rest ? $_[1] - $rest : 0; |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
# EO sub snap |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
#################################################################################################### |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# private helper function |
|
88
|
|
|
|
|
|
|
# returns all prime factors |
|
89
|
|
|
|
|
|
|
# we shouldn't need many! |
|
90
|
|
|
|
|
|
|
# ****************************************************************************** |
|
91
|
|
|
|
|
|
|
sub factors |
|
92
|
|
|
|
|
|
|
{ |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# hold all factors |
|
95
|
2
|
|
|
2
|
0
|
10
|
my @primes; |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# get number to factorize |
|
98
|
2
|
|
|
|
|
5
|
my ($number) = @_; |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# loop from 2 up to number |
|
101
|
2
|
|
|
|
|
9
|
for ( my $y = 2; $y <= $number; $y ++ ) |
|
102
|
|
|
|
|
|
|
{ |
|
103
|
|
|
|
|
|
|
# skip if not a factor |
|
104
|
11
|
100
|
|
|
|
29
|
next if $number % $y; |
|
105
|
|
|
|
|
|
|
# divide by factor found |
|
106
|
5
|
|
|
|
|
8
|
$number /= $y; |
|
107
|
|
|
|
|
|
|
# store found factor |
|
108
|
5
|
|
|
|
|
7
|
push(@primes, $y); |
|
109
|
|
|
|
|
|
|
# restart from 2 |
|
110
|
5
|
|
|
|
|
5
|
redo; |
|
111
|
|
|
|
|
|
|
} |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# sort the prime factors |
|
114
|
2
|
|
|
|
|
25
|
return sort @primes; |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
}; |
|
117
|
|
|
|
|
|
|
# EO sub factors |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
#################################################################################################### |
|
120
|
|
|
|
|
|
|
#################################################################################################### |
|
121
|
|
|
|
|
|
|
1; |