primes.pl
144 lines of code
1
#!/usr/local/bin/perl
2
3
binmode(STDIN, ":utf8");
4
binmode(STDOUT, ":utf8");
5
6
# must have's!
7
use strict;
8
use warnings;
9
use CGI::Carp qw(fatalsToBrowser);
10
use DBI;
11
use URI::Escape;
12
13
use lib "/var/www/html/Pm";
14
15
use Html qw(pre_html_header);
16
use Bc_chef qw(cookie_get);
17
use Bc_misc qw(get_param referrer);
18
use Bc_sql qw(
19
              get_constant
20
              sql_execute
21
              user_exists
22
              $QUERY_PAGE
23
              $QUERY_UID
24
              $LOGGEDIN
25
26
              $DB
27
             );
28
29
use Security qw(banned);
30
31
my $DEBUG = 0;
32
33
my $output;
34
35
  ############################################################
36
37
  ### YOUR CONTENT HERE
38
39
  my $byPerl = get_param("byPerl");
40
  my $floor = get_param("floor");
41
  my $ceiling = get_param("ceiling");
42
  if (not $floor) { $floor = 2; }
43
  if (not $ceiling) { $ceiling = 12345; }
44
45
  sub remainder($$) {
46
    my ($a, $b) = @_;
47
48
    return $a % $b;
49
  }
50
51
  if ($byPerl) {
52
    my $counter = 1;
53
54
    $output .= pre_html_header();
55
56
    while ($counter <= $ceiling) {
57
      my $isPrime = 1;
58
59
      for (my $i = 2; $i < $counter; $i++) {
60
        if (not remainder($counter, $i)) {
61
            $isPrime = 0;
62
        }
63
      }
64
65
      if ($isPrime) { $output .= "$counter<br>\n"; }
66
67
      $counter++;
68
    }
69
  } else {
70
    $output .= pre_html_header();
71
    $output .= <<END;
72
<link rel=stylesheet href='css.pl'>
73
<body onload="go();">
74
<div id=primes style='width: 234px; height: 234px; overflow-x: hidden; overflow-y: scroll; display: inline-block;'>
75
</div>
76
77
<div id=primes style='width: 345px; height: 234px; overflow-x: hidden; overflow-y: scroll; display: inline-block;'>
78
  <table border=0 cellpadding=0 cellspacing=0 width=100%><tr><td align=center colspan=3><form style='margin: 0;'>
79
    Variables
80
    <input type=hidden name=byPerl value=0>
81
  </td></tr><tr><td class=spacery colspan=3></tr><tr><td width=1>
82
    floor
83
  </td><td class=spacerx></td><td width=1>
84
    <input name=floor id=floor type=number value='$floor'><br>
85
  </td></tr><tr><td class=spacery colspan=3></tr><tr><td>
86
    ceiling
87
  </td><td class=spacerx></td><td>
88
    <input name=ceiling id=ceiling value='$ceiling'>
89
  </td></tr><tr><td class=spacery colspan=3></tr><tr><td align=center colspan=3><form>
90
    <button>Run</button>
91
  </form></td></tr></table>
92
</div>
93
94
<script>
95
  var div = document.getElementById('primes');
96
97
  var floor = 2;
98
  var ceiling = 12345;
99
  var counter = floor;
100
101
  function go() {
102
    floor = parseInt(document.getElementById('floor').value);
103
    ceiling = parseInt(document.getElementById('ceiling').value);
104
    counter = floor;
105
106
    if ((ceiling - floor) > 15000) {
107
      div.innerHTML = (ceiling - floor) + " results would just take too long to calculate!<br>so not doing it!<br>";
108
    } else {
109
      div.innerHTML = "range is " + floor + " to " + ceiling + " (" + (ceiling - floor) + ")<br>";
110
      div.innerHTML = div.innerHTML + "counter set to " + counter + "<br>";
111
      div.innerHTML = div.innerHTML + "k, give it a sec<hr>";
112
      setTimeout(doMath, 10);
113
    }
114
  }
115
116
  function doMath() {
117
    div.innerHTML = div.innerHTML + "counter in doMath is " + counter + "<hr>";
118
    while (counter <= ceiling) {
119
      var isPrime = 1; // assume it's a prime to begin with
120
121
      for (i = 2; i < counter; i++) {
122
        if (counter % i == 0) { isPrime = 0; }
123
      }
124
125
      if (isPrime) {
126
        var output;
127
        output = document.createElement('div');
128
        output.innerHTML = output.innerHTML + counter + "<br>";
129
        div.appendChild(output);
130
      }
131
132
      counter++;
133
    }
134
135
    div.innerHTML = div.innerHTML + "<hr>Calculation complete!";
136
  }
137
</script>
138
</body>
139
END
140
  }
141
142
print $output;
143
144
exit 1;