File: primes_aux.pl

package info (click to toggle)
libgraphviz-perl 2.16-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 940 kB
  • ctags: 67
  • sloc: perl: 2,059; makefile: 2
file content (42 lines) | stat: -rwxr-xr-x 632 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 -w
# badly written program in the hopes it would sound nice
# - Greg McCarroll
#
# This was used to test Devel::GraphVizProf, providing
# primes.dot and thus primes.png

@known=qw(2 3 5 7);

for (1..100) {
    if (check_prime($_)) {
	warn "$_ is prime\n";
    }
}

sub check_prime {
    my ($n)=@_;
    if ($n < 2) {
	return 0;
    }

    for (1..scalar(@known)) {
	if ($n==$known[$_-1]) {
	    return 1;
	}
	if (($n/$known[$_-1]) == int($n/$known[$_-1])) {
	    return 0;
	}
    }


    for ($known[-1]..int(sqrt($n))) {
	if (($n/$_) == int($n/$_)) {
	    return 0;
	}
    }
    push(@known,$n);
    return 1;
}