File: fn_phi

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 (22 lines) | stat: -rwxr-xr-x 460 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
#!/usr/bin/perl
#
# Euler's totient function
# If N=p1^e1...pn^en then phi(N)=N/(1-1/p1)...(1-1/pn).

die "Usage: $0 number\n" if ($#ARGV != 0);
$N = shift;
$N =~ s/^[+-]//;  # Remove the sign
if ($N eq "0" || $N eq "1") {
	print "$N\n";
	exit 0;
}
open(STDOUT, "|samuel -bZ");
print "$N";
chop ($factors = `factorint $N`);
foreach $p (split(/\./,$factors)) {
	$p =~ s/\^\d+//;  # Remove the exponent
	print "/$p.($p-1)";
}	
print ";\n";
close STDOUT;
exit 0;