File: fn_modulo

package info (click to toggle)
saml 970418-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,188 kB
  • ctags: 1,703
  • sloc: ansic: 17,186; sh: 2,573; yacc: 497; perl: 264; makefile: 242; python: 242
file content (42 lines) | stat: -rwxr-xr-x 835 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/perl
#
# Computes $1 modulo $2 with respect to the literal in $3
#
$Usage = "Usage: $0 <expr> <modulus> <literal>\n";
die $Usage unless $#ARGV == 2;

$_ = $ARGV[2];
$literal = $1 if /^\+?(\w+)$/;
die $Usage unless $literal;

$expr = $ARGV[0];
$modu = $ARGV[1];

# Find the term of highest degree in $modu
$_ = " $modu";
s/\b$literal\b/$literal\^1/g;
s/\^1\^/\^/g;
s/([+-])/ \1/g;
@terms = split(' ');
$degree = 0;
foreach $term (@terms) {
	if ($term =~ /\b$literal\^(\d+)/)
		{ $degree = $1 if $degree < $1; }
}
if ($degree == 0) {
	print "0\n";
	exit 0;
}
@high_terms = grep(/\b$literal\^$degree/, @terms);
$high_terms = join("", @high_terms);

open(STDOUT, "| samuel -b") || die;
print <<"__end"
  E = $expr;
  M = $modu;
  S = $literal^$degree;
  C = ($high_terms) / ?S;
  (?E, ?S >> ?S - ?M / ?C);
__end
;
close STDOUT;